public async Task <IDictionary <string, SentimentResult> > GetBatchSentimentAsync(Dictionary <string, string> textBatch) { this.ValidateBatchRequest(textBatch); if (!textBatch.Any()) { return(new Dictionary <string, SentimentResult>()); } string content; using (var response = await this._requestor.PostAsync(Constants.SentimentBatchRequest, BuildInputString(textBatch))) { content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(textBatch.ToDictionary(r => r.Key, r => SentimentResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content)))); } } var result = JsonConvert.DeserializeObject <AzureSentimentBatchResult>(content); var parsedResults = result.SentimentBatch.ToDictionary(sr => sr.Id, sr => SentimentResult.Build(sr.Score)); foreach (var error in result.Errors) { parsedResults.Add(error.Id, SentimentResult.Build(error.Message)); } return(parsedResults); }
public static async Task Main() { string text = "SQLSaturday is Awesome!"; string[] endpoints = new string[] { "<endpointGoesHere>", "http://localhost:5000" }; string key = "<apiKeyGoesHere>"; ApiKeyServiceClientCredentials credentials = new ApiKeyServiceClientCredentials(key); foreach (string endpoint in endpoints) { using (TextAnalyticsClient client = new TextAnalyticsClient(credentials) { Endpoint = endpoint }) { SentimentResult sentiment = await client.SentimentAsync(text); Console.WriteLine($"Sentiment for text: '{text}'\n On endpoint: '{endpoint}'\n is: {sentiment.Score}\n"); } } Console.ReadLine(); }
private async Task ShowSentimentScore(SentimentResult score, [CanBeNull] string quote = null) { var embed = new EmbedBuilder() .WithTitle(quote) .AddField("Positive", $"{score.PositiveScore:#0.##}") .AddField("Neutral", $"{score.NeutralScore:#0.##}") .AddField("Negative", $"{score.NegativeScore:#0.##}") .AddField("Delay", score.ClassificationTime.Humanize(2)); switch (score.Classification) { case Moe.Services.Sentiment.Sentiment.Negative: embed = embed.WithColor(Color.Red); break; case Moe.Services.Sentiment.Sentiment.Positive: embed = embed.WithColor(Color.Blue); break; case Moe.Services.Sentiment.Sentiment.Neutral: embed = embed.WithColor(Color.DarkGrey); break; default: throw new ArgumentOutOfRangeException(); } await ReplyAsync(embed); }
public void AnalyseText(string text) { if (string.IsNullOrWhiteSpace(text)) { return; } SentimentResult sentiment = textAnalyticsClient.Sentiment(text); KeyPhraseResult keywords = textAnalyticsClient.KeyPhrases(text); foreach (string keyword in keywords.KeyPhrases.Select(x => x.ToLower().Trim())) { if (!keywordCounts.ContainsKey(keyword)) { keywordCounts.Add(keyword, 1); } else { keywordCounts[keyword]++; } } string sentimentWord = sentiment.ToDescription(); string commonKeywords = string.Join(", ", keywordCounts.OrderByDescending(x => x.Value) .Take(4) .Select(x => $"{x.Key} [{x.Value}]")); Console.WriteLine($"Sentiment is {sentimentWord} [{sentiment.Score}]"); Console.WriteLine($" Common keywords are: {commonKeywords}"); Console.WriteLine(); }
public static int SentimentValue(string inputText, HttpClient httpClient) { // get sentiment string inputTextEncoded = HttpUtility.UrlEncode(inputText); string sentimentRequest = "data.ashx/amla/text-analytics/v1/GetSentiment?Text=" + inputTextEncoded; var responseTask = httpClient.GetAsync(sentimentRequest); responseTask.Wait(); var response = responseTask.Result; var contentTask = response.Content.ReadAsStringAsync(); contentTask.Wait(); var content = contentTask.Result; if (!response.IsSuccessStatusCode) { throw new Exception("Call to get sentiment failed with HTTP status code: " + response.StatusCode + " and contents: " + content); } SentimentResult sentimentResult = JsonConvert.DeserializeObject <SentimentResult>(content); Console.WriteLine("Sentiment score: " + sentimentResult.Score); Console.WriteLine("Tweet: " + inputText); int sentimentScore = (int)(sentimentResult.Score * 100); if (sentimentScore <= 0) { sentimentScore = 0; } return(sentimentScore); }
private async Task AnalyzeTextAsync() { try { if (!string.IsNullOrEmpty(this.speechRecognitionTextBox.Text)) { SentimentResult textAnalysisResult = await TextAnalyticsHelper.GetTextSentimentAsync(new string[] { this.speechRecognitionTextBox.Text }); double score = textAnalysisResult.Scores.ElementAt(0); this.sentimentControl.Sentiment = score; } else { this.sentimentControl.Sentiment = 0.5; } this.OnSpeechRecognitionAndSentimentProcessed(new SpeechRecognitionAndSentimentResult { SpeechRecognitionText = this.speechRecognitionTextBox.Text, TextAnalysisSentiment = this.sentimentControl.Sentiment }); } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Error during Text Analytics call."); } }
private void cmdCheck_Click(object sender, EventArgs e) { SentimentResult result = _analyzer.Predict(txtSentiment.Text); lblProbabilities.Text = string.Format("Negative: {0:P02}, Positive: {1:P02}", result.NegativeProbability, result.PositiveProbability); lblScores.Text = string.Format("Negative: {0:E2}, Positive: {1:E2}", result.NegativeScore, result.PositiveScore); if (result.Polarity == Polarity.Positive) { lblSentimentResult.Text = "Positive"; lblSentimentResult.BackColor = Color.FromArgb(35, 168, 109); } else { lblSentimentResult.Text = "Negative"; lblSentimentResult.BackColor = Color.FromArgb(168, 35, 35); } // add log var log = new SentimentLog { Sentiment = txtSentiment.Text, Polarity = result.Polarity.ToString(), NegativeProbability = Math.Round(result.NegativeProbability, 3), PositiveProbability = Math.Round(result.PositiveProbability, 3), NegativeScore = Math.Round(result.NegativeScore, 3), PositiveScore = Math.Round(result.PositiveScore, 3) }; _logs.Add(log); UpdateStatus("READY", DisplayPart.Status); }
public SentimentResult ProcessSentiment(string documentid, string text) { TextAnalyticsClient client = AuthenticateTextAnalytics(_azureRegion, _textAnalyticsKey); SentimentResult results = client.Sentiment(text, "en"); return(results); }
private static void ProcessSentiment(string documentid, string text) { TextAnalyticsClient client = AuthenticateTextAnalytics(_azureRegion, _textAnalyticsKey); SentimentResult results = client.Sentiment(text, "en"); Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", documentid, results.Score.ToString()); }
public async Task DecodeResponse() { var expected = SentimentResult.Build(1M); var sut = TextAnalyticsTestHelper.BuildSut(GetMessage()); var result = await sut.GetSentimentAsync(Input); Assert.AreEqual(expected, result); }
public async Task ReturnBadRequestIfEmptyInput() { var expected = SentimentResult.Build(Constants.SentimentNullInputErrorText); var sut = TextAnalyticsTestHelper.BuildSut(GetMessage()); var result = await sut.GetSentimentAsync(null); Assert.AreEqual(expected, result); }
public async Task TransmitImageSavedAsync(SentimentResult result) { var serialized = JsonConvert.SerializeObject(result); var eventData = new EventData(Encoding.UTF8.GetBytes(serialized)); _exceptionHandler.Run(() => _eventHubClient.Send(eventData)); await Task.FromResult <object>(null); }
public SentimentResult Analyse(string text, string lan) { var client = CognitvieServicesHelper.AuthenticateClient(); SentimentResult result = client.Sentiment(text, lan); Console.WriteLine($"Sentiment Score: {result.Score:0.00}"); return(result); }
public async Task ReturnFailureOnBadResult() { var error = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error); var expected = SentimentResult.Build(error); var sut = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error)); var result = await sut.GetSentimentAsync(Input); Assert.AreEqual(expected, result); }
public async Task GetResultFromAzure() { var expected = SentimentResult.Build(0.9742637M); const string Input = "This is very positive text because I love this service"; var sut = ServiceFactory.Build(); var result = await sut.GetSentimentAsync(Input); Assert.AreEqual(expected, result); }
static void GetChanges(ClientContext SPClientContext, string ListId, TraceWriter log) { // Get the List Web spWeb = SPClientContext.Site.RootWeb; List changedList = spWeb.Lists.GetById(new Guid(ListId)); SPClientContext.Load(changedList); SPClientContext.ExecuteQuery(); // Create the ChangeToken and Change Query ChangeToken lastChangeToken = new ChangeToken(); lastChangeToken.StringValue = string.Format("1;3;{0};{1};-1", ListId, DateTime.Now.AddMinutes(-1).ToUniversalTime().Ticks.ToString()); ChangeToken newChangeToken = new ChangeToken(); newChangeToken.StringValue = string.Format("1;3;{0};{1};-1", ListId, DateTime.Now.ToUniversalTime().Ticks.ToString()); ChangeQuery myChangeQuery = new ChangeQuery(false, false); myChangeQuery.Item = true; // Get only Item changes myChangeQuery.Add = true; // Get only the new Items myChangeQuery.ChangeTokenStart = lastChangeToken; myChangeQuery.ChangeTokenEnd = newChangeToken; // Get all Changes var allChanges = changedList.GetChanges(myChangeQuery); SPClientContext.Load(allChanges); SPClientContext.ExecuteQuery(); foreach (Change oneChange in allChanges) { if (oneChange is ChangeItem) { // Get what is changed ListItem changedListItem = changedList.GetItemById((oneChange as ChangeItem).ItemId); SPClientContext.Load(changedListItem); SPClientContext.ExecuteQuery(); // Create a Text Analytics client ApiKeyServiceClientCredentials myCredentials = new ApiKeyServiceClientCredentials(serviceKey); TextAnalyticsClient myClient = new TextAnalyticsClient(myCredentials) { Endpoint = serviceEndpoint }; // Call the service SentimentResult myScore = myClient.Sentiment(changedListItem["Comments"].ToString(), "en"); // Insert the values back in the Item changedListItem["Sentiment"] = myScore.Score.ToString(); changedListItem.Update(); SPClientContext.ExecuteQuery(); } } }
public async Task SentimentAsync() { using (MockContext context = MockContext.Start(this.GetType())) { HttpMockServer.Initialize(this.GetType(), "SentimentAsync"); ITextAnalyticsClient client = GetClient(HttpMockServer.CreateInstance()); SentimentResult result = await client.SentimentAsync("I love my team mates"); Assert.True(result.Score > 0); } }
public void Sentiment() { using (MockContext context = MockContext.Start(this.GetType().FullName)) { HttpMockServer.Initialize(this.GetType().FullName, "Sentiment"); ITextAnalyticsClient client = GetClient(HttpMockServer.CreateInstance()); SentimentResult result = client.Sentiment("I love my team mates"); Assert.True(result.Score > 0); } }
public async Task DecodeResponse() { var expected = new Dictionary <string, SentimentResult> { { "1", SentimentResult.Build(0.9549767M) }, { "2", SentimentResult.Build(0.7767222M) }, { "3", SentimentResult.Build(0.8988889M) }, { "4", SentimentResult.Build("Record cannot be null/empty") } }; var sut = TextAnalyticsTestHelper.BuildSut(GetMessage()); var result = await sut.GetBatchSentimentAsync(this._input); CollectionAssert.AreEquivalent(expected, result.ToList()); }
public async Task ReturnAllFailureOnBadResult() { var err = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error); var expected = new Dictionary <string, SentimentResult> { { "1", SentimentResult.Build(err) }, { "2", SentimentResult.Build(err) }, { "3", SentimentResult.Build(err) }, { "4", SentimentResult.Build(err) } }; var sut = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error)); var result = await sut.GetBatchSentimentAsync(this._input); CollectionAssert.AreEqual(expected, result.ToList()); }
public static string ToDescription(this SentimentResult sentiment) { if (!sentiment.Score.HasValue) { return("Unknown"); } if (sentiment.Score > 0.6) { return("Positive"); } if (sentiment.Score < 0.4) { return("Negative"); } return("Neutral"); }
private async Task ShowSentimentScore(SentimentResult score, string?quote = null) { var embed = new EmbedBuilder() .WithTitle(quote) .AddField("Positive", $"{score.PositiveScore:#0.##}") .AddField("Neutral", $"{score.NeutralScore:#0.##}") .AddField("Negative", $"{score.NegativeScore:#0.##}") .AddField("Delay", score.ClassificationTime.Humanize(2)); embed = score.Classification switch { Moe.Services.Sentiment.Sentiment.Negative => embed.WithColor(Color.Red), Moe.Services.Sentiment.Sentiment.Positive => embed.WithColor(Color.Blue), Moe.Services.Sentiment.Sentiment.Neutral => embed.WithColor(Color.DarkGrey), _ => throw new ArgumentOutOfRangeException() }; await ReplyAsync(embed); }
public static async Task Main() { string endpoint = "<endpointGoesHere>"; string key = "<apiKeyGoesHere>"; ApiKeyServiceClientCredentials credentials = new ApiKeyServiceClientCredentials(key); using (TextAnalyticsClient client = new TextAnalyticsClient(credentials) { Endpoint = endpoint }) { while (true) { // Read the users input Console.Write("\n--------------------\nEnter some text: "); string input = Console.ReadLine(); // Detect the language LanguageResult languageResult = await client.DetectLanguageAsync(input); Console.WriteLine("\n> Language: " + languageResult.ToDescription()); string languageCode = languageResult.HighestLanguageCode(); // Detect the sentiment SentimentResult sentimentResult = await client.SentimentAsync(input, languageCode); Console.WriteLine($"> Sentiment: {sentimentResult.Score} - {sentimentResult.ToDescription()}"); // Detect the key phrases KeyPhraseResult keyPhraseResult = await client.KeyPhrasesAsync(input, languageCode); Console.WriteLine("> Key Phrases: " + string.Join(", ", keyPhraseResult.KeyPhrases)); } } }
static void Main(string[] args) { if (!string.IsNullOrEmpty(subscriptionKey) && !string.IsNullOrEmpty(endpoint)) { string textToAnalyze = "今年最強クラスの台風が週末3連休を直撃か...影響とその対策は?"; ApiKeyServiceClientCredentials credentials = new ApiKeyServiceClientCredentials(subscriptionKey); TextAnalyticsClient client = new TextAnalyticsClient(credentials) { Endpoint = endpoint }; OutputEncoding = System.Text.Encoding.UTF8; LanguageResult languageResult = client.DetectLanguage(textToAnalyze); Console.WriteLine($"Language: {languageResult.DetectedLanguages[0].Name}"); SentimentResult sentimentResult = client.Sentiment(textToAnalyze, languageResult.DetectedLanguages[0].Iso6391Name); Console.WriteLine($"Sentiment Score: {sentimentResult.Score:0.00}"); Write("Press any key to exit."); ReadKey(); } else { throw new Exception("You must set both TEXT_ANALYTICS_SUBSCRIPTION_KEY and TEXT_ANALYTICS_ENDPOINT:: as environment variables."); } }
public async Task GetResultFromAzure() { var input = new Dictionary <string, string> { { "1", "This is very positive text because I love this service" }, { "2", "Test is very bad because I hate this service" }, { "3", "The service was OK, nothing special, I've had better" }, { "4", "" } }; var expected = new Dictionary <string, SentimentResult> { { "1", SentimentResult.Build(0.9742637M) }, { "2", SentimentResult.Build(0.03089833M) }, { "3", SentimentResult.Build(0.4267564M) }, { "4", SentimentResult.Build("Record cannot be null/empty") } }; var sut = ServiceFactory.Build(); var result = await sut.GetBatchSentimentAsync(input); CollectionAssert.AreEquivalent(expected, result.ToList()); }
public TextAnalyticsInsight GetInsights(string documentid, string text) { // create the TextAnalyticsInsight object if (!string.IsNullOrEmpty(text)) { TextAnalyticsInsight textAnalyticsInsight = new TextAnalyticsInsight(); EntitiesResult entitiesResult = this.ProcessEntities(documentid, text); KeyPhraseResult keyPhraseResult = this.ProcessKeyPhrases(documentid, text); SentimentResult sentimentResult = this.ProcessSentiment(documentid, text); foreach (EntityRecord record in entitiesResult.Entities) { textAnalyticsInsight.EntityRecords.Add(new Entity.CognitiveServices.EntityRecord { Name = record.Name, SubType = record.SubType, Type = record.Type, WikipediaId = record.WikipediaId, WikipediaLanguage = record.WikipediaLanguage, WikipediaUrl = record.WikipediaUrl }); } foreach (string keyPhrase in keyPhraseResult.KeyPhrases) { textAnalyticsInsight.KeyPhrases.Add(keyPhrase); } textAnalyticsInsight.SentimentScore = sentimentResult.Score.Value; // map the CogServices models to our custom model TextAnalyticsInsight which // contains all TextAnalytics insights for the paramter Text return(textAnalyticsInsight); } return(new TextAnalyticsInsight()); }
public async Task <SentimentResult> GetSentimentAsync(string text) { if (string.IsNullOrWhiteSpace(text)) { return(SentimentResult.Build(Constants.SentimentNullInputErrorText)); } var request = $"{Constants.SentimentRequest}{HttpUtility.UrlEncode(text)}"; string content; using (var response = await this._requestor.GetAsync(request)) { content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(SentimentResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content))); } } var result = JsonConvert.DeserializeObject <AzureSentimentResult>(content); return(SentimentResult.Build(result.Score)); }
private async Task AnalyzeTextAsync() { try { if (!string.IsNullOrEmpty(this.speechRecognitionTextBox.Text)) { SentimentResult textAnalysisResult = await TextAnalyticsHelper.GetTextSentimentAsync(this.translatedText, this.detectedLanguage[0]); this.sentimentControl.Sentiment = textAnalysisResult.Score; } else { this.sentimentControl.Sentiment = 0.5; } this.OnSpeechRecognitionAndSentimentProcessed(new SpeechRecognitionAndSentimentResult { SpeechRecognitionText = this.speechRecognitionTextBox.Text, TextAnalysisSentiment = this.sentimentControl.Sentiment, DetectedLanguage = this.detectedLanguage[1], TranslatedText = this.translatedText }); } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Error during Text Analytics 'Sentiment' call."); } }
public SentimentResultContainer(SentimentResult result) { Result = result; }
/// <summary> /// Generates the sentiments. /// </summary> /// <param name="start">The start.</param> /// <param name="end">The end.</param> /// <param name="companies">The companies.</param> /// <returns>List<SentimentResult>.</returns> public List <SentimentResult> GenerateSentiments(DateTime start, DateTime end, IEnumerable <string> companies) { start = new DateTime(start.Year, start.Month, start.Day); end = new DateTime(end.Year, end.Month, end.Day); var result = new List <SentimentResult>(); using (var context = this.GetContext()) { foreach (var company in companies) { var location = new SentimentResult(); var filter = this.keywordManager.GetFilters(company); location.Name = filter.UserName; var pattern = this.indexHelper.BuildPatternFromFilter(filter); var tableName = TableNameHelper.GetNewsStreamTableName(this.currentClientUser.GetProfile().Postfix); try { var cluster = context.Database.SqlQuery <string>( $@"select ClusterId0 from {tableName} where Contains(keywords, {{0}})", pattern).ToList(); }catch (Exception e) { continue; } var clusters = context.Database.SqlQuery <string>( $@"select ClusterId0 from {tableName} where Contains(keywords, {{0}})", pattern).ToList(); var idList = clusters.ToList(); var vs = from vi in context.NewsSentiments where vi.Date >= start && vi.Date <= end && idList.Contains(vi.ClusterId0) group vi by vi.Date into s select new { date = s.Key, value = s.Sum( x => x.Attitude == "Positive" ? (x.Vote == 0 ? 1 : x.Vote) : (x.Vote == 0 ? -1 : -x.Vote)) }; var visitCount = vs.ToDictionary(i => i.date); foreach (var item in visitCount) { var detail = new SentimentDetail { Date = item.Key.ToShortDateString(), Value = (int)item.Value.value }; location.Details.Add(detail); } result.Add(location); } } return(result); }