forked from BotBuilderCommunity/botbuilder-community-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentimentExtensions.cs
More file actions
59 lines (51 loc) · 2.2 KB
/
Copy pathSentimentExtensions.cs
File metadata and controls
59 lines (51 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Bot.Builder.Community.Middleware.SentimentAnalysis
{
internal static class SentimentExtensions
{
internal static async Task<string> Sentiment(this string text, string apiKey)
{
if (string.IsNullOrEmpty(text))
{
return "0.0";
}
if (!string.IsNullOrEmpty(apiKey))
{
return await GetSentimentWithCognitiveService(text, apiKey);
}
else
{
return GetSentimentWithMLModel(text);
}
}
private static string GetSentimentWithMLModel(string text)
{
var prediction = SentimentAnalyzer.Sentiments.Predict(text);
return prediction.Prediction.ToString(); // true in case of Postive, false in case of Negative
}
private static async Task<string> GetSentimentWithCognitiveService(string text, string apiKey)
{
ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials(apiKey))
{
Endpoint = "https://westus.api.cognitive.microsoft.com"
}; //Replace 'westus' with the correct region for your Text Analytics subscription
Console.OutputEncoding = System.Text.Encoding.UTF8;
// Extract the language
var result = await client.DetectLanguageAsync(false, new LanguageBatchInput(new List<LanguageInput>() { new LanguageInput(id: "1", text: text) }));
var language = result.Documents?[0].DetectedLanguages?[0].Iso6391Name;
// Get the sentiment
var sentimentResult = await client.SentimentAsync(false,
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
new MultiLanguageInput(language, "0", text),
}));
return sentimentResult.Documents?[0].Score?.ToString("#.#");
}
}
}