|
8 | 8 | namespace Demo |
9 | 9 | { |
10 | 10 |
|
11 | | - public static class Demo |
| 11 | + public class Demo |
12 | 12 | { |
13 | | - public const string API_KEY = "muGX5Bi8olr2m40WA2IPv8pEz4WNs19i"; |
| 13 | + Intento intento; |
| 14 | + IntentoAiTextTranslate translate; |
14 | 15 |
|
15 | | - public static string TranslateSync (string text, string from="en", string to="zh", string provider = null) |
| 16 | + public Demo(string apikey) |
16 | 17 | { |
17 | | - var intento = IntentoSDK.Intento.Create(API_KEY); |
18 | | - var translate = intento.Ai.Text.Translate; |
19 | | - dynamic result = translate.Fulfill(text, to, from: from, provider: provider); |
20 | | - return (string)result.results[0]; |
| 18 | + intento = Intento.Create(apikey); |
| 19 | + translate = intento.Ai.Text.Translate; |
21 | 20 | } |
22 | 21 |
|
23 | | - public static List<string> TranslateSyncList(List<string> text, string from = "en", string to = "zh", string provider = null) |
| 22 | + /// <summary> |
| 23 | + /// Translation of one text segment with waiting for the result |
| 24 | + /// </summary> |
| 25 | + /// <param name="text">text to translate</param> |
| 26 | + /// <param name="from">from language code</param> |
| 27 | + /// <param name="to">to language code</param> |
| 28 | + /// <param name="provider">provider id</param> |
| 29 | + /// <returns></returns> |
| 30 | + public string TranslateAsyncAndWaitResult(string text, string from="en", string to="zh", string provider=null) |
24 | 31 | { |
25 | | - var intento = IntentoSDK.Intento.Create(API_KEY); |
26 | | - var translate = intento.Ai.Text.Translate; |
27 | | - dynamic result = translate.Fulfill(text, to, from: from, provider: provider); |
28 | | - IEnumerable<string> res = result.results.Values<string>(); |
| 32 | + dynamic result = translate.Fulfill(text, to, from: from, provider: provider, async:true, wait_async:true); |
| 33 | + if (result.error != null) |
| 34 | + { |
| 35 | + // Error processing |
| 36 | + } |
| 37 | + return (string)result.response[0].results[0]; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Translation of a list of text segments with waiting for the result |
| 42 | + /// </summary> |
| 43 | + /// <param name="text">text to translate</param> |
| 44 | + /// <param name="from">from language code</param> |
| 45 | + /// <param name="to">to language code</param> |
| 46 | + /// <param name="provider">provider id</param> |
| 47 | + /// <returns></returns> |
| 48 | + public List<string> TranslateAsyncListAndWaitResult(List<string> text, string from = "en", string to = "zh", string provider = "ai.text.translate.google.translate_api.v3") |
| 49 | + { |
| 50 | + dynamic result = translate.Fulfill(text, to, from: from, provider: provider, async: true, wait_async: true); |
| 51 | + if (result.error != null) |
| 52 | + { |
| 53 | + // Error processing |
| 54 | + } |
| 55 | + IEnumerable<string> res = result.response[0].results.Values<string>(); |
29 | 56 | return res.ToList(); |
30 | 57 | } |
31 | 58 |
|
32 | | - public static IEnumerable<string> ProviderNames() |
| 59 | + /// <summary> |
| 60 | + /// Translation of one segment and return ID of the translation task |
| 61 | + /// </summary> |
| 62 | + /// <param name="text">text to translate</param> |
| 63 | + /// <param name="from">from language code</param> |
| 64 | + /// <param name="to">to language code</param> |
| 65 | + /// <param name="provider">provider id</param> |
| 66 | + /// <returns></returns> |
| 67 | + public string TranslateAsync(string text, string from = "en", string to = "zh", string provider=null) |
| 68 | + { |
| 69 | + dynamic result = translate.Fulfill(text, to, from: from, provider: provider, async: true, wait_async: false); |
| 70 | + return (string)result.id; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Wait for the results of the asynchronous translation job |
| 75 | + /// </summary> |
| 76 | + /// <param name="text">text to translate</param> |
| 77 | + /// <param name="from">from language code</param> |
| 78 | + /// <param name="to">to language code</param> |
| 79 | + /// <param name="provider">provider id</param> |
| 80 | + /// <returns></returns> |
| 81 | + public List<string> WaitAsyncJob(string job_id, int delay) |
| 82 | + { |
| 83 | + dynamic result = intento.WaitAsyncJob(job_id, delay: delay); |
| 84 | + if (result.done != null) |
| 85 | + { |
| 86 | + bool done = (bool)result.done; |
| 87 | + if (!done) |
| 88 | + // Result not ready yet |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + if (result.error != null) |
| 94 | + { |
| 95 | + // Error processing |
| 96 | + } |
| 97 | + IEnumerable<string> res = result.response[0].results.Values<string>(); |
| 98 | + return res.ToList(); |
| 99 | + } |
| 100 | + |
| 101 | + public IEnumerable<string> ProviderNames() |
33 | 102 | { |
34 | | - var translate = IntentoSDK.Intento.Create(API_KEY).Ai.Text.Translate; |
35 | 103 | IList<dynamic> providers_raw = translate.Providers(); |
36 | 104 | var provider_names = providers_raw.Select(i => (string)i.name); |
37 | 105 | return provider_names; |
38 | 106 | } |
39 | 107 |
|
| 108 | + public static void Main(string[] args) |
| 109 | + { |
| 110 | + // In this Demo, Intento ApiKey is taken from the environment |
| 111 | + Demo demo = new Demo(Environment.GetEnvironmentVariable("APIKEY")); |
| 112 | + |
| 113 | + // Simplest use with one segment |
| 114 | + string result1 = demo.TranslateAsyncAndWaitResult("Translation example"); |
| 115 | + |
| 116 | + // Several segments with the indication of the language pair and provider |
| 117 | + List<string> result2 = demo.TranslateAsyncListAndWaitResult(new List<string> { "Translation example", "One more example" }, to:"ru", provider: "ai.text.translate.deepl.api"); |
| 118 | + |
| 119 | + // Own waiting cycle. Please do not call WaitAsyncJob more than once every 2-3 seconds. |
| 120 | + // It is recommended to specify 0 in the delay parameter. |
| 121 | + string job_id = demo.TranslateAsync("Asynchronous translation"); |
| 122 | + List<string> res; |
| 123 | + while ((res = demo.WaitAsyncJob(job_id, -1)) == null) |
| 124 | + { } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + |
40 | 129 | } |
| 130 | + |
41 | 131 | } |
0 commit comments