Skip to content

Commit c9a384e

Browse files
committed
chore(Language Translator V3): Add examples
1 parent 7745997 commit c9a384e

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed

IBM.Watson.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.Watson.NaturalLanguageU
9595
EndProject
9696
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IBM.Watson.ToneAnalyzer.v3.UnitTests", "test\ToneAnalyzer.v3.UnitTests\IBM.Watson.ToneAnalyzer.v3.UnitTests.csproj", "{28F5A68C-CF5B-4691-A5E5-9135C99470A0}"
9797
EndProject
98+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.Watson.LanguageTranslator.v3.Examples", "examples\IBM.Watson.LanguageTranslator.v3.Examples\IBM.Watson.LanguageTranslator.v3.Examples.csproj", "{03B2FA0D-14C8-485C-8EE8-16715871E813}"
99+
EndProject
98100
Global
99101
GlobalSection(SolutionConfigurationPlatforms) = preSolution
100102
Debug|Any CPU = Debug|Any CPU
@@ -233,6 +235,10 @@ Global
233235
{28F5A68C-CF5B-4691-A5E5-9135C99470A0}.Debug|Any CPU.Build.0 = Debug|Any CPU
234236
{28F5A68C-CF5B-4691-A5E5-9135C99470A0}.Release|Any CPU.ActiveCfg = Release|Any CPU
235237
{28F5A68C-CF5B-4691-A5E5-9135C99470A0}.Release|Any CPU.Build.0 = Release|Any CPU
238+
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
239+
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Debug|Any CPU.Build.0 = Debug|Any CPU
240+
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Release|Any CPU.ActiveCfg = Release|Any CPU
241+
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Release|Any CPU.Build.0 = Release|Any CPU
236242
EndGlobalSection
237243
GlobalSection(SolutionProperties) = preSolution
238244
HideSolutionNode = FALSE
@@ -270,6 +276,7 @@ Global
270276
{57C7F28F-822A-4C52-909A-F9DB6A0CFA51} = {65D78D49-E54F-4952-A1D4-7C8D0EDAA1B3}
271277
{415AADD4-CC05-4B77-8E57-C1B079421875} = {89D715BF-7464-45FB-8DA0-9D7B46AB8C7F}
272278
{28F5A68C-CF5B-4691-A5E5-9135C99470A0} = {C7873F44-7188-49BE-84C5-532BE4E07147}
279+
{03B2FA0D-14C8-485C-8EE8-16715871E813} = {65D78D49-E54F-4952-A1D4-7C8D0EDAA1B3}
273280
EndGlobalSection
274281
GlobalSection(ExtensibilityGlobals) = postSolution
275282
SolutionGuid = {B9D9D17B-1C17-402F-B701-DC671528690A}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="IBM.Cloud.SDK.Core" Version="0.7.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\IBM.Watson.LanguageTranslator.v3\IBM.Watson.LanguageTranslator.v3.csproj" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update="appsettings.json;.\glossary.tmx">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/**
2+
* Copyright 2019 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.Cloud.SDK.Core.Http;
19+
using IBM.Cloud.SDK.Core.Util;
20+
using IBM.Watson.LanguageTranslator.v3.Model;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.IO;
24+
25+
namespace IBM.Watson.LanguageTranslator.v3.Examples
26+
{
27+
public class ServiceExample
28+
{
29+
string apikey = "{apikey}";
30+
string url = "{url}";
31+
string versionDate = "{versionDate}";
32+
private string glossaryPath = "glossary.tmx";
33+
private string modelId;
34+
35+
static void Main(string[] args)
36+
{
37+
ServiceExample example = new ServiceExample();
38+
39+
example.Translate();
40+
example.ListIdentifiableLanguages();
41+
example.IdentifyLanguage();
42+
example.ListModels();
43+
example.CreateModel();
44+
example.GetModel();
45+
example.DeleteModel();
46+
47+
Console.WriteLine("Examples complete. Press any key to close the application.");
48+
Console.ReadKey();
49+
}
50+
51+
#region Translation
52+
public void Translate()
53+
{
54+
TokenOptions tokenOptions = new TokenOptions()
55+
{
56+
IamApiKey = apikey,
57+
ServiceUrl = url
58+
};
59+
60+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
61+
62+
var result = service.Translate(
63+
text: new List<string>() { "I'm sorry, Dave. I'm afraid I can't do that." },
64+
modelId: "en-fr"
65+
);
66+
67+
Console.WriteLine(result.Response);
68+
}
69+
#endregion
70+
71+
#region Identification
72+
public void ListIdentifiableLanguages()
73+
{
74+
TokenOptions tokenOptions = new TokenOptions()
75+
{
76+
IamApiKey = apikey,
77+
ServiceUrl = url
78+
};
79+
80+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
81+
82+
var result = service.ListIdentifiableLanguages();
83+
84+
Console.WriteLine(result.Response);
85+
}
86+
87+
public void IdentifyLanguage()
88+
{
89+
TokenOptions tokenOptions = new TokenOptions()
90+
{
91+
IamApiKey = apikey,
92+
ServiceUrl = url
93+
};
94+
95+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
96+
97+
var result = service.Identify(
98+
text: "I'm sorry, Dave. I'm afraid I can't do that."
99+
);
100+
101+
Console.WriteLine(result.Response);
102+
}
103+
#endregion
104+
105+
#region Models
106+
public void ListModels()
107+
{
108+
TokenOptions tokenOptions = new TokenOptions()
109+
{
110+
IamApiKey = apikey,
111+
ServiceUrl = url
112+
};
113+
114+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
115+
116+
var result = service.ListModels();
117+
118+
Console.WriteLine(result.Response);
119+
}
120+
121+
public void CreateModel()
122+
{
123+
TokenOptions tokenOptions = new TokenOptions()
124+
{
125+
IamApiKey = apikey,
126+
ServiceUrl = url
127+
};
128+
129+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
130+
131+
DetailedResponse<TranslationModel> result;
132+
133+
using (FileStream fs = File.OpenRead(glossaryPath))
134+
{
135+
using (MemoryStream ms = new MemoryStream())
136+
{
137+
fs.CopyTo(ms);
138+
service.WithHeader("X-Watson-Test", "1");
139+
result = service.CreateModel(
140+
baseModelId: "en-fr",
141+
forcedGlossary: ms,
142+
name: "dotnetExampleModel"
143+
);
144+
}
145+
}
146+
147+
Console.WriteLine(result.Response);
148+
modelId = result.Result.ModelId;
149+
}
150+
151+
public void GetModel()
152+
{
153+
TokenOptions tokenOptions = new TokenOptions()
154+
{
155+
IamApiKey = apikey,
156+
ServiceUrl = url
157+
};
158+
159+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
160+
161+
var result = service.GetModel(
162+
modelId: modelId
163+
);
164+
165+
Console.WriteLine(result.Response);
166+
}
167+
168+
public void DeleteModel()
169+
{
170+
TokenOptions tokenOptions = new TokenOptions()
171+
{
172+
IamApiKey = apikey,
173+
ServiceUrl = url
174+
};
175+
176+
LanguageTranslatorService service = new LanguageTranslatorService(tokenOptions, versionDate);
177+
178+
var result = service.DeleteModel(
179+
modelId: modelId
180+
);
181+
182+
Console.WriteLine(result.Response);
183+
}
184+
#endregion
185+
}
186+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<tmx version="1.4">
3+
<header creationtool="" creationtoolversion=""
4+
segtype="sentence" o-tmf="" adminlang="EN"
5+
srclang="en" datatype="rtf" o-encoding="UTF-8" />
6+
<body>
7+
<tu>
8+
<tuv xml:lang="en">
9+
<seg>International Business Machines</seg>
10+
</tuv>
11+
<tuv xml:lang="fr">
12+
<seg>International Business Machines</seg>
13+
</tuv>
14+
</tu>
15+
<tu>
16+
<tuv xml:lang="en">
17+
<seg>patent</seg>
18+
</tuv>
19+
<tuv xml:lang="fr">
20+
<seg>brevent</seg>
21+
</tuv>
22+
</tu>
23+
</body>
24+
</tmx>

0 commit comments

Comments
 (0)