-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathDetect.cs
102 lines (93 loc) · 4.47 KB
/
Detect.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// This sample requires C# 7.1 or later for async/await.
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
// Install Newtonsoft.Json with NuGet
using Newtonsoft.Json;
namespace DetectSample
{
/// <summary>
/// The C# classes that represents the JSON returned by the Translator Text API.
/// </summary>
public class DetectResult
{
public string Language { get; set; }
public float Score { get; set; }
public bool IsTranslationSupported { get; set; }
public bool IsTransliterationSupported { get; set; }
public AltTranslations[] Alternatives { get; set; }
}
public class AltTranslations
{
public string Language { get; set; }
public float Score { get; set; }
public bool IsTranslationSupported { get; set; }
public bool IsTransliterationSupported { get; set; }
}
class Program
{
private const string key_var = "TRANSLATOR_TEXT_SUBSCRIPTION_KEY";
private static readonly string subscriptionKey = Environment.GetEnvironmentVariable(key_var);
private const string endpoint_var = "TRANSLATOR_TEXT_ENDPOINT";
private static readonly string endpoint = Environment.GetEnvironmentVariable(endpoint_var);
static Program()
{
if (null == subscriptionKey)
{
throw new Exception("Please set/export the environment variable: " + key_var);
}
if (null == endpoint)
{
throw new Exception("Please set/export the environment variable: " + endpoint_var);
}
}
// Async call to the Translator Text API
static public async Task DetectTextRequest(string subscriptionKey, string endpoint, string route, string inputText)
{
object[] body = new object[] { new { Text = inputText } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
string result = await response.Content.ReadAsStringAsync();
DetectResult[] deserializedOutput = JsonConvert.DeserializeObject<DetectResult[]>(result);
//Iterate through the response.
foreach (DetectResult o in deserializedOutput)
{
Console.WriteLine("The detected language is '{0}'. Confidence is: {1}.\nTranslation supported: {2}.\nTransliteration supported: {3}.\n",
o.Language, o.Score, o.IsTranslationSupported, o.IsTransliterationSupported);
int counter = 0;
// Iterate through alternatives. Use counter for alternative number.
foreach (AltTranslations a in o.Alternatives)
{
counter++;
Console.WriteLine("Alternative {0}", counter);
Console.WriteLine("The detected language is '{0}'. Confidence is: {1}.\nTranslation supported: {2}.\nTransliteration supported: {3}.\n",
a.Language, a.Score, a.IsTranslationSupported, a.IsTransliterationSupported);
}
}
}
}
static async Task Main(string[] args)
{
// This is our main function.
// Output languages are defined in the route.
// For a complete list of options, see API reference.
// https://docs.microsoft.com/azure/cognitive-services/translator/reference/v3-0-detect
string route = "/detect?api-version=3.0";
string detectSentenceText = @"How are you doing today? The weather is pretty pleasant. Have you been to the movies lately?";
await DetectTextRequest(subscriptionKey, endpoint, route, detectSentenceText);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
}