예제 #1
0
        public async System.Threading.Tasks.Task <string> SuggestEntitySentimentAsync(Models.Sentiment doc)
        {
            Dictionary <string, double> values = new Dictionary <string, double>();
            // we need to call the sentiment web service here.  the location should be configured externally
            var url = ConfigurationManager.AppSettings["SentimentServiceUrl"];

            using (var client = new HttpClient())
            {
                //set up client
                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.Timeout = TimeSpan.FromMinutes(10);

                var nvc        = new List <KeyValuePair <string, string> >();
                var texts      = doc.RawText;
                var annotators = doc.Annotators;
                var mode       = doc.Mode;
                nvc.Add(new KeyValuePair <string, string>("texts", texts));
                nvc.Add(new KeyValuePair <string, string>("preset", "all"));
                nvc.Add(new KeyValuePair <string, string>("mode", mode));

                // we need to eventually pass in which annotator we are using
                string fullUrl = url + "/" + annotators;

                var req = new HttpRequestMessage(HttpMethod.Post, fullUrl)
                {
                    Content = new FormUrlEncodedContent(nvc)
                };

                var response = await client.SendAsync(req);

                if (!response.IsSuccessStatusCode)
                {
                    // Nothing for this case
                }
                else if ("charlstm" == annotators)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    values = JsonConvert.DeserializeObject <Dictionary <string, double> >(result);
                }
            }
            // now once we have values, we will return them to the annotation view.
            return(JsonConvert.SerializeObject(values));
        }
예제 #2
0
        public async System.Threading.Tasks.Task <string> SuggestSentimentAsync(Models.Sentiment doc)
        {
            List <double> values = new List <double>();
            // we need to call the sentiment web service here.  the location should be configured externally
            var url = ConfigurationManager.AppSettings["SentimentServiceUrl"];

            using (var client = new HttpClient()) {
                //set up client
                client.BaseAddress = new Uri(url);
                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.Timeout = TimeSpan.FromMinutes(10);

                var nvc        = new List <KeyValuePair <string, string> >();
                var texts      = doc.RawText;
                var annotators = doc.Annotators;
                nvc.Add(new KeyValuePair <string, string>("texts", texts));
                if (annotators.StartsWith("composite"))
                {
                    // We need to adjust this as necessary for the correct variety of composite
                    if ("composite_c" == annotators)
                    {
                        nvc.Add(new KeyValuePair <string, string>("preset", "no_lstm"));
                    }
                    else if ("composite_b" == annotators)
                    {
                        nvc.Add(new KeyValuePair <string, string>("preset", "rule_based"));
                    }
                    else
                    {
                        // this is really composite_a, but it should also serve as the catch-all
                        nvc.Add(new KeyValuePair <string, string>("preset", "all"));
                    }
                    annotators = "composite";
                }
                else
                {
                    nvc.Add(new KeyValuePair <string, string>("preset", "all"));
                }
                // we need to eventually pass in which annotator we are using
                string fullUrl = url + "/" + annotators;

                var req = new HttpRequestMessage(HttpMethod.Post, fullUrl)
                {
                    Content = new FormUrlEncodedContent(nvc)
                };

                var response = await client.SendAsync(req);

                if (!response.IsSuccessStatusCode)
                {
                    values.Add(System.Double.NaN);
                }
                else if ("finance" == annotators)
                {
                    // We get back a pair, positive and negative.  We could try to report both back or we could try to generate a single score
                    // Right now we choose option 2
                    var final_value = 0.0;
                    var result      = await response.Content.ReadAsStringAsync();

                    var parts = result.Split('\t');
                    final_value += double.Parse(parts[0]) * 0.05 - double.Parse(parts[1]) * 0.05;
                    if (final_value > 1.0)
                    {
                        final_value = 1.0;
                    }
                    else if (final_value < -1.0)
                    {
                        final_value = -1.0;
                    }
                    values.Add(final_value);
                }
                else if ("google" == annotators || "stanford" == annotators || "aylien" == annotators || "charlstm" == annotators ||
                         "composite_a" == annotators || "composite_b" == annotators || "composite_c" == annotators)
                {
                    var result = await response.Content.ReadAsStringAsync();

                    List <double> valueList = JsonConvert.DeserializeObject <List <double> >(result);
                    values.Add(valueList[0]);
                }
                else
                {
                    var result = await response.Content.ReadAsStringAsync();

                    values.Add(double.Parse(result));
                }
            }
            // now once we have values, we will return them to the annotation view.
            return(JsonConvert.SerializeObject(values));
        }