示例#1
0
        public IEnumerable <AnalyzedTweet> AnalyzeTweets(IEnumerable <Tweet> tweets)
        {
            if (_httpClient == null)
            {
                return(FakeAnalysis(tweets));
            }

            var request = new SentimentRequest
            {
                Documents = tweets.Distinct(new TweetEqualityComparer()).Select(tweet => new SentimentRequestDocument {
                    Id = tweet.Id, Text = tweet.Text
                }).ToArray()
            };

            if (!request.Documents.Any())
            {
                return(Enumerable.Empty <AnalyzedTweet>());
            }

            SentimentResponse response = GetAnalysisResponseAsync(request).GetAwaiter().GetResult();

            var lookup = request.Documents.ToDictionary(doc => doc.Id, doc => doc.Text);

            return(response.Documents.Select(doc => new AnalyzedTweet
            {
                Id = doc.Id,
                Text = lookup[doc.Id],
                Sentiment = GetSentiment(doc.Score)
            }));
        }
示例#2
0
 public CognitiveTextAnalysis()
 {
     LinkAnalysis       = new List <LinkAnalysisResult>();
     SentimentAnalysis  = new SentimentResponse();
     KeyPhraseAnalysis  = new List <KeyPhraseAnalysisResult>();
     LinguisticAnalysis = new List <LinguisticAnalysisResult>();
 }
            public static async Task <SentimentResponse> SentimentPreviewPredictAsync(TextAnalyticsBatchInput inputDocuments)
            {
                //Uri newuri = new Uri(textAnalyticsUrl);

                //WebRequest objwebRequest = WebRequest.Create(newuri);
                //objwebRequest.Headers.Add("Ocp-Apim-Subscription-Key", textAnalyticsKey);
                //HttpWebResponse objwebResponse = (HttpWebResponse)objwebRequest.GetResponse();
                //StreamReader objStreamReader = new StreamReader(objwebResponse.GetResponseStream());
                //string sResponse = objStreamReader.ReadToEnd();

                //List<SentimentResponse> dataList = JsonConvert.DeserializeObject<List<SentimentResponse>>(sResponse);

                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", textAnalyticsKey);
                    var httpContent = new StringContent(JsonConvert.SerializeObject(inputDocuments), Encoding.UTF8, "application/json");

                    var httpResponse = await httpClient.PostAsync(new Uri(textAnalyticsUrl), httpContent);

                    var responseContent = await httpResponse.Content.ReadAsStringAsync();

                    if (!httpResponse.StatusCode.Equals(HttpStatusCode.OK) || httpResponse.Content == null)
                    {
                        throw new Exception(responseContent);
                    }
                    var test = JsonConvert.DeserializeObject(responseContent);


                    JObject jObject = JObject.Parse(responseContent);

                    string score = (string)jObject.SelectToken("documents[0].score");



                    //JArray signInNames = (JArray)jObject.SelectToken("documents");
                    //foreach (JToken signInName in signInNames)
                    //{
                    //    type = (string)signInName.SelectToken("score");

                    //}

                    SentimentResponse sR = JsonConvert.DeserializeObject <SentimentResponse>(responseContent, new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });
                    double scoreCompare = Convert.ToDouble(score);
                    if (scoreCompare > .5)
                    {
                        sR.Documents[0].Sentiment = "Positive";
                    }

                    else
                    {
                        sR.Documents[0].Sentiment = "Negative";
                    }

                    return(sR);
                }
            }
 public SentimentTweet MapFor(SentimentResponse sentiment, Tweet tweet)
 {
     return(new SentimentTweet
     {
         Sentiment = sentiment,
         Tweet = tweet
     });
 }
示例#5
0
        public void Setup()
        {
            Initialise();

            _sentiment = SentimentResponseBuilder.Build.AnInstance();
            _tweet     = TweetBuilder.Build.AnInstance();

            _result = SentimentTweetMapper.MapFor(_sentiment, _tweet);
        }
        public int FindEmoticonForSentiment(SentimentResponse sentiment)
        {
            if (sentiment.score > 0.3)
            {
                return(Resource.Drawable.Happy);
            }

            if (sentiment.score < -0.3)
            {
                return(Resource.Drawable.Sad);
            }

            return(Resource.Drawable.Neutral);
        }
示例#7
0
        /// <summary>
        /// Call Azure Cognitive Services' Sentiment Analysis Api
        /// </summary>
        /// <param name="texts">List of strings to score</param>
        /// <returns>List of strings with their sentiment scores</returns>
        internal async Task <List <ScoredText> > BulkSentiment(IEnumerable <string> texts)
        {
            var toReturn = new List <ScoredText>();

            var i       = 1;
            var docList = string.Empty;

            foreach (var t in texts)
            {
                // scrub user content so the api call won't fail
                var cleanText = t.Replace("\r", "").Replace("\n", "").Replace('"', '\'');

                // build the request body - Cognitive services requires a unique integer per item
                docList += $"{{\"language\":\"en\",\"id\":\"{i}\",\"text\":\"{cleanText}\"}},";
                i++;
            }
            docList = docList.Substring(0, docList.Length - 1);
            var message = new HttpRequestMessage
            {
                RequestUri = new Uri(_sentimentUrl),
                Method     = HttpMethod.Post,
                Headers    =
                {
                    { "Ocp-Apim-Subscription-Key", _cognitiveKey      },
                    { "Accept",                    "application/json" }
                },
                Content = new StringContent($"{{\"documents\":[{docList}]}}", Encoding.UTF8, "application/json")
            };
            var sentimentHttpResponse = await _httpClient.SendAsync(message);

            SentimentResponse sentimentResponseData = await sentimentHttpResponse.Content.ReadAsJsonAsync <SentimentResponse>();

            i = 0;
            foreach (var r in sentimentResponseData.Documents)
            {
                var sentimentParsible = r.TryGetValue("score", out string scoreResponse);
                var sentiment         = double.Parse(scoreResponse);
                toReturn.Add(new ScoredText {
                    TextInput = texts.ElementAt(i), Sentiment = sentiment
                });
                i++;
            }

            return(toReturn);
        }
示例#8
0
        public void Setup()
        {
            Initialise();

            _tweet = TweetBuilder.Build.AnInstance();

            _sentimentReponse = SentimentResponseBuilder.Build.AnInstance();
            SentimentApiAdapter
            .Setup(x => x.GetSentiment(_tweet))
            .Returns(Task.FromResult(_sentimentReponse));

            _sentimentTweet = SentimentTweetBuilder.Build.AnInstance();
            SentimentTweetMapper
            .Setup(x => x.MapFor(_sentimentReponse, _tweet))
            .Returns(_sentimentTweet);

            _result = Controller.GetSentiment(_tweet).Result;
        }
示例#9
0
 static private void RecordSentiment(IODStatus status, SentimentResponse response, List <string> negativeTopics, List <string> positiveTopics)
 {
     if (status == IODStatus.finished)
     {
         foreach (Sentiment n in response.negative)
         {
             negativeTopics.Add(n.normalized_text);
         }
         foreach (Sentiment p in response.positive)
         {
             positiveTopics.Add(p.normalized_text);
         }
     }
     else
     {
         Console.WriteLine("Sentiment request: {0}", status);
     }
 }
示例#10
0
 public SentimentResponse getSentimentScore(string text)
 {
     try
     {
         var client  = new RestClient("https://api.aylien.com/api/v1/sentiment?mode=tweet&text=" + text);
         var request = new RestRequest(Method.GET);
         addHeaders(request);
         IRestResponse     response = client.Execute(request);
         SentimentResponse reply    = SimpleJson.DeserializeObject <SentimentResponse>(response.Content);
         return(reply);
     }
     catch (System.Exception e) {
         SentimentResponse reply = new SentimentResponse();
         reply.polarity            = "positive";
         reply.polarity_confidence = (float)0.5;
         return(reply);
     }
 }
示例#11
0
        /// <summary>
        /// Example code to call Rosette API to get a document's sentiment
        /// Requires Nuget Package:
        /// rosette_api
        /// </summary>
        static void Main(string[] args)
        {
            //To use the C# API, you must provide an API key
            string apikey = "Your API key";
            string alturl = string.Empty;

            //You may set the API key via command line argument:
            //sentiment yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }

            try
            {
                CAPI         SentimentCAPI       = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                var          newFile             = Path.GetTempFileName();
                StreamWriter sw                  = new StreamWriter(newFile);
                string       sentiment_file_data = @"<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>";
                sw.WriteLine(sentiment_file_data);
                sw.Flush();
                sw.Close();
                //Rosette API provides File upload options (shown here)
                //Simply create a new RosetteFile using the path to a file
                //The results of the API call will come back in the form of a Dictionary
                SentimentResponse response = SentimentCAPI.Sentiment(new RosetteFile(newFile, @"application/octet-stream", "{\"language\":\"eng\"}"));
                foreach (KeyValuePair <string, string> h in response.Headers)
                {
                    Console.WriteLine(string.Format("{0}:{1}", h.Key, h.Value));
                }
                Console.WriteLine(response.ToString());

                if (File.Exists(newFile))
                {
                    File.Delete(newFile);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
        public void Setup()
        {
            Initialise();

            _tweet = TweetBuilder.Build.WithText("1").AnInstance();

            _httpRequest = new Mock <HttpWebRequest>();
            SentimentRequestFactory
            .Setup(x => x.CreateSentimentForTweetRequest(_tweet))
            .Returns(_httpRequest.Object);

            _httpResponse = new Mock <HttpWebResponse>();
            HttpHelper
            .Setup(x => x.GetResponse(_httpRequest.Object))
            .Returns(Task.FromResult(_httpResponse.Object));

            _sentimentResponse = SentimentResponseBuilder.Build.WithScore(1.0).AnInstance();
            ObjectSerializer
            .Setup(x => x.DeserializeJson <SentimentResponse>(_httpResponse.Object))
            .Returns(_sentimentResponse);

            _result = SentimentApiAdapter.GetSentiment(_tweet).Result;
        }
        internal static TasksStateTasksSentimentAnalysisTasksItem DeserializeTasksStateTasksSentimentAnalysisTasksItem(JsonElement element)
        {
            Optional <SentimentResponse> results = default;
            DateTimeOffset    lastUpdateDateTime = default;
            Optional <string> taskName           = default;
            State             status             = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("results"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    results = SentimentResponse.DeserializeSentimentResponse(property.Value);
                    continue;
                }
                if (property.NameEquals("lastUpdateDateTime"))
                {
                    lastUpdateDateTime = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("taskName"))
                {
                    taskName = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("status"))
                {
                    status = property.Value.GetString().ToState();
                    continue;
                }
            }
            return(new TasksStateTasksSentimentAnalysisTasksItem(lastUpdateDateTime, taskName.Value, status, results.Value));
        }
示例#14
0
        public String Post([FromBody] JObject json)
        {
            res = json;
            String bot_id = json.SelectToken("event.bot_id") != null?json.SelectToken("event.bot_id").ToString() : "";

            if (bot_id == "")
            {
                String            text              = json.SelectToken("event.text").ToString();
                String            channel           = json.SelectToken("event.channel").ToString();
                SentimentResponse sentimentResponse = _aylienService.getSentimentScore(text);
                String            sentiment         = sentimentResponse.polarity;
                if (sentimentResponse.polarity_confidence >= 0.7)
                {
                    string msg      = GetMessageString(ref sentiment);
                    String giphyUrl = _giphyService.GetGiphyUrl(sentiment);
                    String response = _slackService.SendResponseToSlack(channel, giphyUrl, msg);
                    Thread.Sleep(1000);
                    pres = response;
                    return(response);
                }
            }
            pres = "No response as polarity was too low";
            return("No response as polarity was too low");
        }
示例#15
0
        public async Task <SentimentResponse> GetSentiment(MSCognitiveServices.Sentiment.SentimentRequest request)
        {
            //Check https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c9
            //Check https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-text-analytics-api-migration
            string getSentimentUrl = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment";

            System.Net.Http.HttpClient objHttpClient =
                new System.Net.Http.HttpClient();
            //Check http://stackoverflow.com/questions/14627399/setting-authorization-header-of-httpclient
            string jsonRequest = Newtonsoft.Json.JsonConvert.SerializeObject(request);

            System.Net.Http.StringContent objStrcontent =
                new StringContent(jsonRequest);
            objStrcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            objStrcontent.Headers.Add("Ocp-Apim-Subscription-Key", AccessToken);
            var result = await objHttpClient.PostAsync(getSentimentUrl, objStrcontent);

            var resultString = await result.Content.ReadAsStringAsync();

            SentimentResponse objSentimentResponse =
                Newtonsoft.Json.JsonConvert.DeserializeObject <SentimentResponse>(resultString);

            return(objSentimentResponse);
        }
        public static async Task Run([QueueTrigger("messagesqueue", Connection = "AzureWebJobsStorage")] string myQueueItem, TraceWriter log)
        {
            log.Info($"Processing: {myQueueItem}");

            //variables
            var azureWebJobsStorage        = CloudConfigurationManager.GetSetting("AzureWebJobsStorage");
            var messagesTableContainerName = CloudConfigurationManager.GetSetting("MessagesTableContainerName");
            var messagesTablePartitionKey  = CloudConfigurationManager.GetSetting("MessagesTablePartitionKey");
            var alertsTableContainerName   = CloudConfigurationManager.GetSetting("AlertsTableContainerName");
            var alertsTablePartitionKey    = CloudConfigurationManager.GetSetting("AlertsTablePartitionKey");
            var textAnalyticsApiKey        = CloudConfigurationManager.GetSetting("TextAnalyticsApiKey");
            var textAnalyticsApiUrl        = CloudConfigurationManager.GetSetting("TextAnalyticsApiUrl");
            var luisApiKey     = CloudConfigurationManager.GetSetting("LuisApiKey");
            var luisApiUrl     = CloudConfigurationManager.GetSetting("LuisApiUrl");
            var bullyingApiKey = CloudConfigurationManager.GetSetting("BullyingApiKey");
            var bullyingApiUrl = CloudConfigurationManager.GetSetting("BullyingApiUrl");
            var storageAccount = CloudStorageAccount.Parse(azureWebJobsStorage);
            var tableClient    = storageAccount.CreateCloudTableClient();

            //parse the message JSON to a dynamic object, then entity
            dynamic queueItem         = JObject.Parse(myQueueItem);
            var     chatMessageEntity = new ChatMessageEntity(messagesTablePartitionKey, Guid.NewGuid().ToString());

            chatMessageEntity.Message = queueItem.Message;
            chatMessageEntity.Time    = queueItem.Time;
            chatMessageEntity.UserId  = queueItem.UserId;
            chatMessageEntity.ChatId  = queueItem.ChatId;

            //Get Text Analytics Sentiment data and add to entity
            var sentimentData = await GetTextAnalyticsData(textAnalyticsApiUrl, textAnalyticsApiKey, queueItem.Message.ToString(), "sentiment");

            SentimentResponse sentiment = JsonConvert.DeserializeObject <SentimentResponse>(sentimentData);

            chatMessageEntity.Sentiment = sentiment.documents[0].score;
            log.Info($"Sentiment: {chatMessageEntity.Sentiment}");

            //Get Text Analytics key phrase data and add to entity
            var keyPhrasesData = await GetTextAnalyticsData(textAnalyticsApiUrl, textAnalyticsApiKey, queueItem.Message.ToString(), "keyPhrases");

            KeyPhrasesResponse keyPhrases = JsonConvert.DeserializeObject <KeyPhrasesResponse>(keyPhrasesData);

            chatMessageEntity.KeyPhrases = string.Join(",", keyPhrases.documents[0].keyPhrases);
            log.Info($"Key Phrases: {chatMessageEntity.KeyPhrases }");

            //Do LUIS entity and intent extraction here
            var luisData = await GetLUISData(luisApiUrl, luisApiKey, queueItem.Message.ToString());

            LuisResponse luis = JsonConvert.DeserializeObject <LuisResponse>(luisData);

            if (luis.topScoringIntent.intent != "None")
            {
                //create an alert
                var alertEntity = new AlertEntity(alertsTablePartitionKey, Guid.NewGuid().ToString());
                alertEntity.AlertCategory = ResolveCategory(luis.topScoringIntent.intent);
                alertEntity.AlertText     = queueItem.Message.ToString();
                alertEntity.ChatMessageId = chatMessageEntity.RowKey.ToString();
                alertEntity.StartIndex    = -1;
                alertEntity.EndIndex      = -1;
                alertEntity.ChatId        = chatMessageEntity.ChatId;

                await LogAlert(tableClient, alertsTableContainerName, alertEntity);
            }
            if (luis.entities.Count > 0)
            {
                //create an alert for each entity
                foreach (var entity in luis.entities)
                {
                    //create an alert
                    var alertEntity = new AlertEntity(alertsTablePartitionKey, Guid.NewGuid().ToString());
                    alertEntity.AlertCategory = ResolveCategory(entity.type);
                    alertEntity.AlertText     = entity.entity;
                    alertEntity.ChatMessageId = chatMessageEntity.RowKey.ToString();
                    alertEntity.StartIndex    = entity.startIndex;
                    alertEntity.EndIndex      = entity.endIndex;
                    alertEntity.ChatId        = chatMessageEntity.ChatId;

                    await LogAlert(tableClient, alertsTableContainerName, alertEntity);
                }
            }

            //bullying detection
            var bullyingData = await GetBullyingData(bullyingApiUrl, bullyingApiKey, queueItem.Message.ToString());

            // Create the TableOperation object that inserts the entity
            var messagesTable          = tableClient.GetTableReference(messagesTableContainerName);
            var messageInsertOperation = TableOperation.Insert(chatMessageEntity);

            messagesTable.Execute(messageInsertOperation);

            log.Info($"Processed: {myQueueItem}");
        }
        internal static AnalyzeSentimentResultCollection ConvertToAnalyzeSentimentResultCollection(SentimentResponse results, IDictionary <string, int> idToIndexMap)
        {
            var analyzedSentiments = new List <AnalyzeSentimentResult>();

            //Read errors
            foreach (DocumentError error in results.Errors)
            {
                analyzedSentiments.Add(new AnalyzeSentimentResult(error.Id, ConvertToError(error.Error)));
            }

            //Read sentiments
            foreach (DocumentSentimentInternal docSentiment in results.Documents)
            {
                analyzedSentiments.Add(new AnalyzeSentimentResult(docSentiment.Id, docSentiment.Statistics ?? default, new DocumentSentiment(docSentiment)));
            }

            analyzedSentiments = SortHeterogeneousCollection(analyzedSentiments, idToIndexMap);

            return(new AnalyzeSentimentResultCollection(analyzedSentiments, results.Statistics, results.ModelVersion));
        }
示例#18
0
 internal SentimentTaskResult(SentimentResponse results)
 {
     Results = results;
 }
示例#19
0
 internal Components1C6O47FSchemasTasksstatePropertiesTasksPropertiesSentimentanalysistasksItemsAllof1(SentimentResponse results)
 {
     Results = results;
 }
 internal TasksStateTasksSentimentAnalysisTasksItem(DateTimeOffset lastUpdateDateTime, string taskName, State status, SentimentResponse results) : base(lastUpdateDateTime, taskName, status)
 {
     Results = results;
 }