Skip to content

Commit e4dcfd4

Browse files
authored
Ver 1.4.5 (#3)
1 parent 2efebc9 commit e4dcfd4

File tree

6 files changed

+122
-21
lines changed

6 files changed

+122
-21
lines changed

Demo/Demo.cs

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,124 @@
88
namespace Demo
99
{
1010

11-
public static class Demo
11+
public class Demo
1212
{
13-
public const string API_KEY = "muGX5Bi8olr2m40WA2IPv8pEz4WNs19i";
13+
Intento intento;
14+
IntentoAiTextTranslate translate;
1415

15-
public static string TranslateSync (string text, string from="en", string to="zh", string provider = null)
16+
public Demo(string apikey)
1617
{
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;
2120
}
2221

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)
2431
{
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>();
2956
return res.ToList();
3057
}
3158

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()
33102
{
34-
var translate = IntentoSDK.Intento.Create(API_KEY).Ai.Text.Translate;
35103
IList<dynamic> providers_raw = translate.Providers();
36104
var provider_names = providers_raw.Select(i => (string)i.name);
37105
return provider_names;
38106
}
39107

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+
40129
}
130+
41131
}

Demo/Demo.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{2A9CFF8D-AC5B-40BC-9111-04D54A47017D}</ProjectGuid>
8-
<OutputType>Library</OutputType>
8+
<OutputType>Exe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Demo</RootNamespace>
1111
<AssemblyName>Demo</AssemblyName>
@@ -29,6 +29,9 @@
2929
<ErrorReport>prompt</ErrorReport>
3030
<WarningLevel>4</WarningLevel>
3131
</PropertyGroup>
32+
<PropertyGroup>
33+
<StartupObject />
34+
</PropertyGroup>
3235
<ItemGroup>
3336
<Reference Include="System" />
3437
<Reference Include="System.Core" />

Intento.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@ async public Task<dynamic> WaitAsyncJobAsync(string asyncId, int delay = 0)
167167
List<int> delays;
168168
int n = 0;
169169

170-
if (delay == 0)
170+
if (delay == -1)
171+
delays = new List<int> { 0 };
172+
else if (delay == 0)
171173
delays = CalcDelays(400);
172174
else
173175
delays = CalcDelays(delay);
174176

175177
delay = delays[0];
176-
while (DateTime.Now < dt.AddSeconds(delay))
178+
do
177179
{
178180
Log(string.Format("WaitAsyncJobAsync-loop: {0} - {1}ms", asyncId, delay));
179181
Thread.Sleep(delay);
@@ -188,7 +190,7 @@ async public Task<dynamic> WaitAsyncJobAsync(string asyncId, int delay = 0)
188190
if (n < delays.Count)
189191
delay = delays[n];
190192
Log(string.Format("WaitAsyncJobAsync-loop2: {0} - {1}ms", asyncId, delay));
191-
}
193+
} while (DateTime.Now < dt.AddSeconds(delay));
192194

193195
// Timeout
194196
dynamic json = new JObject();

IntentoSDK.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@
8181
<Content Include="ILMerge\License.txt" />
8282
<Content Include="Newtonsoft\Newtonsoft.Json.dll" />
8383
</ItemGroup>
84-
<ItemGroup>
85-
<Folder Include="Demo\" />
86-
</ItemGroup>
84+
<ItemGroup />
8785
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
8886
<PropertyGroup>
8987
<PostBuildEvent>

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ To get more information check out [the site](https://inten.to/).
1010
[API User Manual](https://github.com/intento/intento-api)
1111

1212
In case you don't have a key to use Intento API, please register here [console.inten.to](https://console.inten.to)
13+
14+
To compile the project, you may need to turn off the signing of the resulting DLL.
15+
16+
You will find a demo of using this SDK in the Demo project of this solution.
17+

RevisionHistory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@ namespace IntentoSDK
4747
// - bug in Fulfill parameter
4848
// 1.4.4: 2020-04-15
4949
// - special_headers parameter in Fulfill
50+
// 1.4.5: 2020-08-11
51+
// - Extended demo
52+
// - -1 as a special value for delay parameter of WaitAsyncJob
5053

5154
}

0 commit comments

Comments
 (0)