Repository navigation
Expand file tree
/
Copy pathDetectLanguageClient.cs
More file actions
76 lines (66 loc) · 3.22 KB
/
Copy pathDetectLanguageClient.cs
File metadata and controls
76 lines (66 loc) · 3.22 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.Threading.Tasks;
namespace DetectLanguage {
public class DetectLanguageClient {
public DetectLanguageConfiguration configuration;
public DetectLanguageHttpClient httpClient;
/// <summary>
/// Initializes new instance of the API client class.
/// </summary>
/// <param name="apiKey">The API key to use for the client connection. Get it for free at https://detectlanguage.com</param>
public DetectLanguageClient(string apiKey) : this(new DetectLanguageConfiguration(apiKey)) { }
/// <summary>
/// Initializes new instance of the API client class.
/// </summary>
/// <param name="configuration">Configuration class instance</param>
public DetectLanguageClient(DetectLanguageConfiguration configuration) {
this.configuration = configuration;
httpClient = new DetectLanguageHttpClient(configuration);
}
/// <summary>
/// Detect text language
/// </summary>
/// <param name="text">Text for language detection</param>
/// <returns>The list of detected languages with scores.</returns>
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<DetectResult[]> DetectAsync(string text) {
var request = new DetectRequest{ q = text };
return await httpClient.PostAsync<DetectResult[]>("detect", request);
}
/// <summary>
/// Detect text language code
/// </summary>
/// <param name="text">Text for language detection</param>
/// <returns>Language code or null if language can't be detected</returns>
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<string> DetectCodeAsync(string text) {
DetectResult[] results = await this.DetectAsync(text);
if (results.Length == 0)
return null;
return results[0].language;
}
/// <summary>
/// Detect languages for multiple texts via single request
/// </summary>
/// <param name="texts">Texts for language detection</param>
/// <returns>List of detection results</returns>
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<DetectResult[][]> BatchDetectAsync(string[] texts) {
var request = new BatchDetectRequest{ q = texts };
return await httpClient.PostAsync<DetectResult[][]>("detect-batch", request);
}
/// <summary>
/// Get account status
/// </summary>
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<AccountStatus> GetAccountStatusAsync() {
return await httpClient.GetAsync<AccountStatus>("account/status");
}
/// <summary>
/// Get supported languages list
/// </summary>
/// <exception cref="DetectLanguageException">Thrown if the request fails.</exception>
public async Task<Language[]> GetLanguagesAsync() {
return await httpClient.GetAsync<Language[]>("languages");
}
}
}