コード例 #1
0
ファイル: BitextExecutor.cs プロジェクト: m2pathan/SemantAPI
        public void Execute(AnalysisExecutionContext context)
        {
            _context = context;

            if (context.Results.Count <= 0)
            {
                context.OnExecutionProgress("Bitext", 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 > 8192)
                {
                    failed++;
                    document.Value.AddOutput("Bitext", 0, "failed");

                    AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed);
                    context.OnExecutionProgress("Bitext", ea);

                    if (ea.Cancel)
                    {
                        break;
                    }

                    continue;
                }

                Dictionary <string, string> parameters = new Dictionary <string, string>();
                parameters.Add("User", context.Key);
                parameters.Add("Pass", context.Secret);
                parameters.Add("OutFormat", context.Format.ToString());
                parameters.Add("Detail", "Global");
                parameters.Add("Normalized", "No");
                parameters.Add("Theme", "Gen");
                parameters.Add("ID", document.Key);
                parameters.Add("Lang", LocaleHelper.GetTripleLanguageAbbreviation(context.Language));
                parameters.Add("Text", HttpUtility.UrlEncode(document.Value.Source));

                byte[]     data    = Encoding.UTF8.GetBytes(FormatParameters(parameters));
                WebRequest request = WebRequest.Create("http://svc9.bitext.com/WS_NOps_Val/Service.aspx");
                request.ContentType   = "application/x-www-form-urlencoded";
                request.Method        = "POST";
                request.ContentLength = data.Length;

                using (Stream writer = request.GetRequestStream())
                {
                    writer.Write(data, 0, data.Length);
                    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("Bitext: 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("Bitext", 0, "failed");

                        AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed);
                        context.OnExecutionProgress("Bitext", ea);
                        response.Close();

                        if (ea.Cancel)
                        {
                            break;
                        }
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            string result = reader.ReadToEnd();
                            result = result.Replace("\r\n", string.Empty)
                                     .Replace("\r", string.Empty)
                                     .Replace("\n", string.Empty)
                                     .Replace(">\"", ">")
                                     .Replace("\"<", "<")
                                     .Replace("&", "&amp;");

                            Regex regex = new Regex(@"(?<=\bencoding="")[^""]*");
                            Match match = regex.Match(result);

                            Encoding encoding = null;
                            if (match.Success)
                            {
                                encoding = Encoding.GetEncoding(match.Value);
                            }
                            else
                            {
                                encoding = Encoding.UTF8;
                            }

                            BitextSentiment sentiment = null;
                            using (Stream stream = new MemoryStream(encoding.GetBytes(result)))
                            {
                                XmlSerializer serializer = new XmlSerializer(typeof(BitextSentiment));
                                sentiment = (BitextSentiment)serializer.Deserialize(stream);
                            }

                            processed++;
                            double score    = MergeSentimentScore(sentiment);
                            string polarity = GetSentimentPolarity(score);
                            document.Value.AddOutput("Bitext", score, polarity);
                            AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Processed, context.Results.Count, processed, failed);
                            context.OnExecutionProgress("Bitext", ea);

                            if (ea.Cancel)
                            {
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    failed++;
                    document.Value.AddOutput("Bitext", 0, "failed");
                    AnalysisExecutionProgressEventArgs ea = new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Failed, context.Results.Count, processed, failed);
                    ea.Reason = ex.Message;
                    context.OnExecutionProgress("Bitext", ea);

                    if (ea.Cancel)
                    {
                        break;
                    }
                }
            }

            context.OnExecutionProgress("Bitext", new AnalysisExecutionProgressEventArgs(AnalysisExecutionStatus.Success, context.Results.Count, processed, failed));
        }