Skip to content

Commit a09497b

Browse files
authored
Merge pull request #398 from watson-developer-cloud/nlu-examples
NLU tests and examples
2 parents 7b98d46 + e9f0ecb commit a09497b

File tree

2 files changed

+465
-3
lines changed

2 files changed

+465
-3
lines changed

examples/IBM.Watson.NaturalLanguageUnderstanding.v1.Examples/ServiceExample.cs

Lines changed: 238 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using IBM.Cloud.SDK.Core.Authentication.Iam;
1919
using IBM.Watson.NaturalLanguageUnderstanding.v1.Model;
2020
using System;
21+
using System.Collections.Generic;
2122

2223
namespace IBM.Watson.NaturalLanguageUnderstanding.v1.Examples
2324
{
@@ -44,7 +45,8 @@ static void Main(string[] args)
4445
public void Analyze()
4546
{
4647
IamAuthenticator authenticator = new IamAuthenticator(
47-
apikey: "{apikey}");
48+
apikey: "{apikey}"
49+
);
4850

4951
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
5052
service.SetServiceUrl("{serviceUrl}");
@@ -77,7 +79,8 @@ public void Analyze()
7779
public void ListModels()
7880
{
7981
IamAuthenticator authenticator = new IamAuthenticator(
80-
apikey: "{apikey}");
82+
apikey: "{apikey}"
83+
);
8184

8285
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
8386
service.SetServiceUrl("{serviceUrl}");
@@ -90,7 +93,8 @@ public void ListModels()
9093
public void DeleteModel()
9194
{
9295
IamAuthenticator authenticator = new IamAuthenticator(
93-
apikey: "{apikey}");
96+
apikey: "{apikey}"
97+
);
9498

9599
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
96100
service.SetServiceUrl("{serviceUrl}");
@@ -102,5 +106,236 @@ public void DeleteModel()
102106
Console.WriteLine(result.Response);
103107
}
104108
#endregion
109+
110+
#region Features
111+
public void AnalyzeWithCategories()
112+
{
113+
IamAuthenticator authenticator = new IamAuthenticator(
114+
apikey: "{apikey}"
115+
);
116+
117+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
118+
service.SetServiceUrl("{serviceUrl}");
119+
120+
var result = service.Analyze(
121+
url: "www.ibm.com",
122+
features: new Features()
123+
{
124+
Categories = new CategoriesOptions()
125+
{
126+
Limit = 3
127+
}
128+
}
129+
);
130+
131+
Console.WriteLine(result.Response);
132+
}
133+
134+
public void AnalyzeWithConcepts()
135+
{
136+
IamAuthenticator authenticator = new IamAuthenticator(
137+
apikey: "{apikey}"
138+
);
139+
140+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
141+
service.SetServiceUrl("{serviceUrl}");
142+
143+
var result = service.Analyze(
144+
url: "www.ibm.com",
145+
features: new Features()
146+
{
147+
Concepts = new ConceptsOptions()
148+
{
149+
Limit = 3
150+
}
151+
}
152+
);
153+
154+
Console.WriteLine(result.Response);
155+
}
156+
157+
public void AnalyzeWithEmotion()
158+
{
159+
IamAuthenticator authenticator = new IamAuthenticator(
160+
apikey: "{apikey}"
161+
);
162+
163+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
164+
service.SetServiceUrl("{serviceUrl}");
165+
166+
var result = service.Analyze(
167+
html: "<html><head><title>Fruits</title></head><body><h1>Apples and Oranges</h1><p>I love apples! I don't like oranges.</p></body></html>",
168+
features: new Features()
169+
{
170+
Emotion = new EmotionOptions()
171+
{
172+
Targets = new List<string> { "apples", "oranges" }
173+
}
174+
}
175+
);
176+
177+
Console.WriteLine(result.Response);
178+
}
179+
180+
public void AnalyzeWithEntities()
181+
{
182+
IamAuthenticator authenticator = new IamAuthenticator(
183+
apikey: "{apikey}"
184+
);
185+
186+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
187+
service.SetServiceUrl("{serviceUrl}");
188+
189+
var result = service.Analyze(
190+
url: "www.cnn.com",
191+
features: new Features()
192+
{
193+
Entities = new EntitiesOptions()
194+
{
195+
Sentiment = true,
196+
Limit = 1
197+
}
198+
}
199+
);
200+
201+
Console.WriteLine(result.Response);
202+
}
203+
204+
public void AnalyzeWithKeywords()
205+
{
206+
IamAuthenticator authenticator = new IamAuthenticator(
207+
apikey: "{apikey}"
208+
);
209+
210+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
211+
service.SetServiceUrl("{serviceUrl}");
212+
213+
var result = service.Analyze(
214+
url: "www.ibm.com",
215+
features: new Features()
216+
{
217+
Keywords = new KeywordsOptions()
218+
{
219+
Sentiment = true,
220+
Emotion = true,
221+
Limit = 2
222+
}
223+
}
224+
);
225+
226+
Console.WriteLine(result.Response);
227+
}
228+
229+
public void AnalyzeWithMetadata()
230+
{
231+
IamAuthenticator authenticator = new IamAuthenticator(
232+
apikey: "{apikey}"
233+
);
234+
235+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
236+
service.SetServiceUrl("{serviceUrl}");
237+
238+
var result = service.Analyze(
239+
url: "www.ibm.com",
240+
features: new Features()
241+
{
242+
Metadata = new MetadataOptions()
243+
}
244+
);
245+
246+
Console.WriteLine(result.Response);
247+
}
248+
249+
public void AnalyzeWithRelations()
250+
{
251+
IamAuthenticator authenticator = new IamAuthenticator(
252+
apikey: "{apikey}"
253+
);
254+
255+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
256+
service.SetServiceUrl("{serviceUrl}");
257+
258+
var result = service.Analyze(
259+
text: "Leonardo DiCaprio won Best Actor in a Leading Role for his performance.",
260+
features: new Features()
261+
{
262+
Relations = new RelationsOptions()
263+
}
264+
);
265+
266+
Console.WriteLine(result.Response);
267+
}
268+
269+
public void AnalyzeWithSemanticRoles()
270+
{
271+
IamAuthenticator authenticator = new IamAuthenticator(
272+
apikey: "{apikey}"
273+
);
274+
275+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
276+
service.SetServiceUrl("{serviceUrl}");
277+
278+
var result = service.Analyze(
279+
text: "IBM has one of the largest workforces in the world",
280+
features: new Features()
281+
{
282+
SemanticRoles = new SemanticRolesOptions()
283+
}
284+
);
285+
286+
Console.WriteLine(result.Response);
287+
}
288+
289+
public void AnalyzeWithSentiment()
290+
{
291+
IamAuthenticator authenticator = new IamAuthenticator(
292+
apikey: "{apikey}"
293+
);
294+
295+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
296+
service.SetServiceUrl("{serviceUrl}");
297+
298+
var result = service.Analyze(
299+
url: "www.wsj.com/news/markets",
300+
features: new Features()
301+
{
302+
Sentiment = new SentimentOptions()
303+
{
304+
Targets = new List<string>() { "stocks" }
305+
}
306+
}
307+
);
308+
309+
Console.WriteLine(result.Response);
310+
}
311+
312+
public void AnalyzeWithSyntax()
313+
{
314+
IamAuthenticator authenticator = new IamAuthenticator(
315+
apikey: "{apikey}"
316+
);
317+
318+
NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService("2019-07-12", authenticator);
319+
service.SetServiceUrl("{serviceUrl}");
320+
321+
var result = service.Analyze(
322+
text: "With great power comes great responsibility",
323+
features: new Features()
324+
{
325+
Syntax = new SyntaxOptions()
326+
{
327+
Sentences = true,
328+
Tokens = new SyntaxOptionsTokens()
329+
{
330+
Lemma = true,
331+
PartOfSpeech = true
332+
}
333+
}
334+
}
335+
);
336+
337+
Console.WriteLine(result.Response);
338+
}
339+
#endregion
105340
}
106341
}

0 commit comments

Comments
 (0)