public void Execute(AnalysisExecutionContext context) { _context = context; if (context.Results.Count <= 0) { context.OnExecutionProgress("Repustate", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, 0, 0, 0)); return; } int processed = 0; int failed = 0; bool isTrial = false; try { string lang = LocaleHelper.GetDoubleLanguageAbbreviation(context.Language); RepustateClient Repustate = new RepustateClient(context.Key); Dictionary <string, string> TotalScoreDataMap = new Dictionary <string, string>(); int count = 0; foreach (KeyValuePair <string, ResultSet> document in context.Results) { count++; string ky = "text" + count; TotalScoreDataMap.Add(ky, document.Value.ToString()); } int BatchSize = 500; int processedBatches = 0; if (TotalScoreDataMap.Count < BatchSize) { Dictionary <string, string> scoreDataMap = new Dictionary <string, string>(); scoreDataMap = Repustate.GetDocumentsQueue(processedBatches, BatchSize, TotalScoreDataMap); scoreDataMap.Add("lang", lang); JavaScriptSerializer serializer = new JavaScriptSerializer(); Array RepustateSentiments; if (context.UseDebugMode) { TimeSpan time = TimeSpan.Zero; RepustateSentiments = (Array)BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state) { string response = Repustate.GetSentimentBulk(scoreDataMap); IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>; return(deserializedResponse["results"] as Array); }), null, out time); Console.WriteLine("\tRepustate: Batch of {0} documents has been recieved. Eexecution time is: {1} ms", scoreDataMap.Count - 1, time.TotalMilliseconds); } else { string response = Repustate.GetSentimentBulk(scoreDataMap); IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>; RepustateSentiments = deserializedResponse["results"] as Array; } SortedDictionary <int, string> rs = new SortedDictionary <int, string>(); for (int i = 0; i < RepustateSentiments.Length; i++) { int id = Int32.Parse(((Dictionary <string, object>)RepustateSentiments.GetValue(i))["id"].ToString()); string score = ((Dictionary <string, object>)RepustateSentiments.GetValue(i))["score"].ToString(); rs.Add(id, score); } int c = 1; foreach (KeyValuePair <string, ResultSet> document in context.Results) { double score = double.Parse(rs[c]); processed++; if (score <= -0.05) { document.Value.AddOutput("Repustate", score, "negative"); } if (score >= 0.05) { document.Value.AddOutput("Repustate", score, "positive"); } else { document.Value.AddOutput("Repustate", score, "neutral"); } AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed); context.OnExecutionProgress("Repustate", ea); c++; if (ea.Cancel) { break; } } } else { int totalBatches = (TotalScoreDataMap.Count / BatchSize) + 1; processedBatches = 0; for (int i = 0; i < totalBatches; i++) { Dictionary <string, string> scoreDataMap = new Dictionary <string, string>(); scoreDataMap = Repustate.GetDocumentsQueue(processedBatches, BatchSize, TotalScoreDataMap); scoreDataMap.Add("lang", lang); JavaScriptSerializer serializer = new JavaScriptSerializer(); Array RepustateSentiments; if (context.UseDebugMode) { TimeSpan time = TimeSpan.Zero; RepustateSentiments = (Array)BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state) { string response = Repustate.GetSentimentBulk(scoreDataMap); IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>; return(deserializedResponse["results"] as Array); }), null, out time); Console.WriteLine("\tRepustate: Batch of {0} documents has been recieved. Eexecution time is: {1} ms", scoreDataMap.Count - 1, time.TotalMilliseconds); } else { string response = Repustate.GetSentimentBulk(scoreDataMap); IDictionary <string, object> deserializedResponse = serializer.Deserialize <IDictionary <string, object> >(response) as IDictionary <string, object>; RepustateSentiments = deserializedResponse["results"] as Array; } SortedDictionary <int, string> rs = new SortedDictionary <int, string>(); for (int j = 0; j < RepustateSentiments.Length; j++) { int id = Int32.Parse(((Dictionary <string, object>)RepustateSentiments.GetValue(j))["id"].ToString()); string score = ((Dictionary <string, object>)RepustateSentiments.GetValue(j))["score"].ToString(); rs.Add(id, score); } int key = 1; int UpperCounter = processedBatches; int LowerCounter = BatchSize; foreach (KeyValuePair <string, ResultSet> document in context.Results) { if (UpperCounter > 0) { key++; UpperCounter--; continue; } if (LowerCounter < 1) { break; } LowerCounter--; double score = double.Parse(rs[key]); processed++; if (score <= -0.05) { document.Value.AddOutput("Repustate", score, "negative"); } if (score >= 0.05) { document.Value.AddOutput("Repustate", score, "positive"); } else { document.Value.AddOutput("Repustate", score, "neutral"); } AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed); context.OnExecutionProgress("Repustate", ea); key++; if (ea.Cancel) { break; } } processedBatches = processedBatches + BatchSize; } } } catch (WebException ex) { if (!(ex.Response.ContentLength == -1)) { isTrial = true; AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, context.Results.Count, 0, 0); ea.Reason = "Your Repustate account doesn’t support " + context.Language + " language"; context.OnExecutionProgress("Repustate", ea); } else { isTrial = false; foreach (KeyValuePair <string, ResultSet> document in context.Results) { failed++; document.Value.AddOutput("Repustate", 0, "failed"); AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed); ea.Reason = ex.Message; context.OnExecutionProgress("Repustate", ea); if (ea.Cancel) { break; } } } } if (!isTrial) { context.OnExecutionProgress("Repustate", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Success, context.Results.Count, processed, failed)); } }
public void Execute(AnalysisExecutionContext context) { _context = context; if (context.Results.Count <= 0) { context.OnExecutionProgress("Chatterbox", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, 0, 0, 0)); return; } int processed = 0; int failed = 0; foreach (KeyValuePair <string, ResultSet> document in context.Results) { if (document.Value.Source.Length > 300) { failed++; document.Value.AddOutput("Chatterbox", 0, "failed"); AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed); context.OnExecutionProgress("Chatterbox", ea); if (ea.Cancel) { break; } continue; } Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("lang", LocaleHelper.GetDoubleLanguageAbbreviation(context.Language)); parameters.Add("text", Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(document.Value.Source))); WebRequest request = WebRequest.Create("https://chatterbox-analytics-sentiment-analysis-free.p.mashape.com/sentiment/current/classify_text/"); request.Headers.Add("X-Mashape-Authorization", context.Key); request.ContentType = "application/x-www-form-urlencoded"; request.Method = "POST"; using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(FormatParameters(parameters)); writer.Flush(); } try { HttpWebResponse response = null; if (context.UseDebugMode) { TimeSpan time = TimeSpan.Zero; response = BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state) { return(request.GetResponse()); }), null, out time) as HttpWebResponse; Console.WriteLine("\tChatterbox: Sentiment for the document {0} has been retreived. Execution time is: {1}", document.Key, time.TotalMilliseconds); } else { response = request.GetResponse() as HttpWebResponse; } if (response.StatusCode != HttpStatusCode.Accepted && response.StatusCode != HttpStatusCode.OK) { failed++; document.Value.AddOutput("Chatterbox", 0, "failed"); AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed); context.OnExecutionProgress("Chatterbox", ea); response.Close(); if (ea.Cancel) { break; } } else { using (Stream stream = response.GetResponseStream()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ChatterboxSentiment)); ChatterboxSentiment sentiment = (ChatterboxSentiment)serializer.ReadObject(stream); processed++; string polarity = GetSentimentPolarity(sentiment.Value); document.Value.AddOutput("Chatterbox", sentiment.Value, polarity); AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed); context.OnExecutionProgress("Chatterbox", ea); if (ea.Cancel) { break; } } } } catch (Exception ex) { failed++; document.Value.AddOutput("Chatterbox", 0, "failed"); AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed); ea.Reason = ex.Message; context.OnExecutionProgress("Chatterbox", ea); if (ea.Cancel) { break; } } } context.OnExecutionProgress("Chatterbox", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Success, context.Results.Count, processed, failed)); }
public void Execute(AnalysisExecutionContext context) { _context = context; if (context.Results.Count <= 0) { context.OnExecutionProgress("Skyttle", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Canceled, 0, 0, 0)); return; } int processed = 0; int failed = 0; foreach (KeyValuePair <string, ResultSet> document in context.Results) { try { Dictionary <string, string> scoreDataMap = new Dictionary <string, string>(); scoreDataMap.Add("text", document.Value.ToString()); string url = "https://sentinelprojects-skyttle20.p.mashape.com/"; string text = document.Value.ToString(); string lang = LocaleHelper.GetDoubleLanguageAbbreviation(context.Language); string keywords = "1"; string sentiment = "1"; string annotate = "0"; string param = "text=" + HttpUtility.UrlEncode(text, System.Text.Encoding.UTF8) + "&lang=" + HttpUtility.UrlEncode(lang, System.Text.Encoding.UTF8) + "&keywords=" + HttpUtility.UrlEncode(keywords, System.Text.Encoding.UTF8) + "&sentiment=" + HttpUtility.UrlEncode(sentiment, System.Text.Encoding.UTF8) + "&annotate=" + HttpUtility.UrlEncode(annotate, System.Text.Encoding.UTF8); string response = ""; if (context.UseDebugMode) { TimeSpan time = TimeSpan.Zero; response = BenchmarkHelper.Invoke(new InvokeBenchmarkHandler(delegate(object state) { return(MakeCall(url, param, context.Key)); }), null, out time) as string; Console.WriteLine("\tSkyttle: Sentiment for the document {0} has been retreived. Execution time is: {1}", document.Key, time.TotalMilliseconds); } else { response = MakeCall(url, param, context.Key); } JavaScriptSerializer serializer = new JavaScriptSerializer(); IDictionary <string, object> dict = serializer.Deserialize <IDictionary <string, object> >(response); processed++; IDictionary <string, object> obj = ((object[])dict["docs"])[0] as IDictionary <string, object>; string language = obj["language"] as string; IDictionary <string, object> sentiments = obj["sentiment_scores"] as IDictionary <string, object>; decimal negScore = (decimal)sentiments["neg"]; decimal posScore = (decimal)sentiments["pos"]; decimal neuScore = (decimal)sentiments["neu"]; if (neuScore > 50) { document.Value.AddOutput("Skyttle", 0.0, "neutral"); } else if (negScore > posScore) { document.Value.AddOutput("Skyttle", Convert.ToDouble(-1 * negScore / 100), "negative"); } else if (posScore > negScore) { document.Value.AddOutput("Skyttle", Convert.ToDouble(posScore / 100), "positive"); } AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed); context.OnExecutionProgress("Skyttle", ea); if (ea.Cancel) { break; } } catch (Exception ex) { failed++; document.Value.AddOutput("Skyttle", 0, "failed"); AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed); ea.Reason = ex.Message; context.OnExecutionProgress("Skyttle", ea); if (ea.Cancel) { break; } } } context.OnExecutionProgress("Skyttle", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Success, context.Results.Count, processed, failed)); }