Skip to content

Commit 3908546

Browse files
authored
Merge pull request #370 from watson-developer-cloud/6672-natural-language-classifier-v1-examples
Add Natural Langauge Classifier V1 examples
2 parents 23f063e + 7aee4b2 commit 3908546

File tree

5 files changed

+269
-0
lines changed

5 files changed

+269
-0
lines changed

IBM.Watson.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.Watson.Discovery.v1.Exa
105105
EndProject
106106
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}"
107107
EndProject
108+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IBM.Watson.NaturalLangaugeClassifier.v1.Examples", "examples\IBM.Watson.NaturalLangaugeClassifier.v1.Examples\IBM.Watson.NaturalLangaugeClassifier.v1.Examples.csproj", "{B74E72B5-E2C9-43EE-88D2-34315AF3AD33}"
109+
EndProject
108110
Global
109111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
110112
Debug|Any CPU = Debug|Any CPU
@@ -263,6 +265,10 @@ Global
263265
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Debug|Any CPU.Build.0 = Debug|Any CPU
264266
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Release|Any CPU.ActiveCfg = Release|Any CPU
265267
{03B2FA0D-14C8-485C-8EE8-16715871E813}.Release|Any CPU.Build.0 = Release|Any CPU
268+
{B74E72B5-E2C9-43EE-88D2-34315AF3AD33}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
269+
{B74E72B5-E2C9-43EE-88D2-34315AF3AD33}.Debug|Any CPU.Build.0 = Debug|Any CPU
270+
{B74E72B5-E2C9-43EE-88D2-34315AF3AD33}.Release|Any CPU.ActiveCfg = Release|Any CPU
271+
{B74E72B5-E2C9-43EE-88D2-34315AF3AD33}.Release|Any CPU.Build.0 = Release|Any CPU
266272
EndGlobalSection
267273
GlobalSection(SolutionProperties) = preSolution
268274
HideSolutionNode = FALSE
@@ -305,6 +311,7 @@ Global
305311
{58A819F6-2F99-4A79-95ED-2DED2F9D5DB4} = {B368DCF7-FB57-4302-8467-4EB142394A29}
306312
{D3FBC964-AF1B-4416-9FE8-8F0A511908D5} = {BE946662-3884-45E9-A3B1-B28F3081AD1B}
307313
{03B2FA0D-14C8-485C-8EE8-16715871E813} = {65D78D49-E54F-4952-A1D4-7C8D0EDAA1B3}
314+
{B74E72B5-E2C9-43EE-88D2-34315AF3AD33} = {5917402E-D9A2-4308-8A1A-9C0692939420}
308315
EndGlobalSection
309316
GlobalSection(ExtensibilityGlobals) = postSolution
310317
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.NaturalLanguageClassifier.v1\IBM.Watson.NaturalLanguageClassifier.v1.csproj" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<None Update=".\NaturalLanguageClassifierTestData\**\*;">
18+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"language":"en",
3+
"name":"dotnet-standard-classifier-delete"
4+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
How hot is it today?,temperature
2+
Is it hot outside?,temperature
3+
Will it be uncomfortably hot?,temperature
4+
Will it be sweltering?,temperature
5+
How cold is it today?,temperature
6+
Is it cold outside?,temperature
7+
Will it be uncomfortably cold?,temperature
8+
Will it be frigid?,temperature
9+
What is the expected high for today?,temperature
10+
What is the expected temperature?,temperature
11+
Will high temperatures be dangerous?,temperature
12+
Is it dangerously cold?,temperature
13+
When will the heat subside?,temperature
14+
Is it hot?,temperature
15+
Is it cold?,temperature
16+
How cold is it now?,temperature
17+
Will we have a cold day today?,temperature
18+
When will the cold subside?,temperature
19+
What highs are we expecting?,temperature
20+
What lows are we expecting?,temperature
21+
Is it warm?,temperature
22+
Is it chilly?,temperature
23+
What's the current temp in Celsius?,temperature
24+
What is the temperature in Fahrenheit?,temperature
25+
Is it windy?,conditions
26+
Will it rain today?,conditions
27+
What are the chances for rain?,conditions
28+
Will we get snow?,conditions
29+
Are we expecting sunny conditions?,conditions
30+
Is it overcast?,conditions
31+
Will it be cloudy?,conditions
32+
How much rain will fall today?,conditions
33+
How much snow are we expecting?,conditions
34+
Is it windy outside?,conditions
35+
How much snow do we expect?,conditions
36+
Is the forecast calling for snow today?,conditions
37+
Will we see some sun?,conditions
38+
When will the rain subside?,conditions
39+
Is it cloudy?,conditions
40+
Is it sunny now?,conditions
41+
Will it rain?,conditions
42+
Will we have much snow?,conditions
43+
Are the winds dangerous?,conditions
44+
What is the expected snowfall today?,conditions
45+
Will it be dry?,conditions
46+
Will it be breezy?,conditions
47+
Will it be humid?,conditions
48+
What is today's expected humidity?,conditions
49+
Will the blizzard hit us?,conditions
50+
Is it drizzling?,conditions
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.NaturalLanguageClassifier.v1;
21+
using IBM.Watson.NaturalLanguageClassifier.v1.Model;
22+
using System;
23+
using System.Collections.Generic;
24+
using System.IO;
25+
26+
namespace IBM.Watson.NaturalLangaugeClassifier.v1.Examples
27+
{
28+
public class ServiceExample
29+
{
30+
string apikey = "{apikey}";
31+
string url = "{url}";
32+
string classifierId;
33+
private string classifierDataFilePath = @"NaturalLanguageClassifierTestData/weather-data.csv";
34+
private string metadataDataFilePath = @"NaturalLanguageClassifierTestData/metadata.json";
35+
36+
static void Main(string[] args)
37+
{
38+
ServiceExample example = new ServiceExample();
39+
40+
example.ListClassifiers();
41+
example.Classify();
42+
example.ClassifyCollection();
43+
example.CreateClassifier();
44+
example.GetClassifier();
45+
example.DeleteClassifier();
46+
47+
Console.WriteLine("Examples complete. Press any key to close the application.");
48+
Console.ReadKey();
49+
}
50+
51+
#region Classify Text
52+
public void Classify()
53+
{
54+
TokenOptions tokenOptions = new TokenOptions()
55+
{
56+
IamApiKey = apikey,
57+
ServiceUrl = url
58+
};
59+
60+
NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);
61+
62+
var result = service.Classify(
63+
classifierId: classifierId,
64+
text: "Will it be hot today?"
65+
);
66+
67+
Console.WriteLine(result.Response);
68+
}
69+
70+
public void ClassifyCollection()
71+
{
72+
TokenOptions tokenOptions = new TokenOptions()
73+
{
74+
IamApiKey = apikey,
75+
ServiceUrl = url
76+
};
77+
78+
NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);
79+
80+
var collection = new List<ClassifyInput>()
81+
{
82+
new ClassifyInput()
83+
{
84+
Text = "Will it be hot today?"
85+
},
86+
new ClassifyInput()
87+
{
88+
Text = "Is it raining?"
89+
}
90+
};
91+
92+
var result = service.ClassifyCollection(
93+
classifierId: classifierId,
94+
collection: collection
95+
);
96+
97+
Console.WriteLine(result.Response);
98+
}
99+
#endregion
100+
101+
#region Manage Classifiers
102+
public void ListClassifiers()
103+
{
104+
TokenOptions tokenOptions = new TokenOptions()
105+
{
106+
IamApiKey = apikey,
107+
ServiceUrl = url
108+
};
109+
110+
NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);
111+
112+
var result = service.ListClassifiers();
113+
114+
Console.WriteLine(result.Response);
115+
116+
if(result.Result.Classifiers != null && result.Result.Classifiers.Count > 0)
117+
{
118+
classifierId = result.Result.Classifiers[0].ClassifierId;
119+
}
120+
}
121+
122+
public void CreateClassifier()
123+
{
124+
TokenOptions tokenOptions = new TokenOptions()
125+
{
126+
IamApiKey = apikey,
127+
ServiceUrl = url
128+
};
129+
130+
NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);
131+
132+
DetailedResponse<Classifier> result = null;
133+
using (FileStream trainingDataFile = File.OpenRead(classifierDataFilePath), metadataFile = File.OpenRead(metadataDataFilePath))
134+
{
135+
using (MemoryStream trainingData = new MemoryStream(), metadata = new MemoryStream())
136+
{
137+
trainingDataFile.CopyTo(trainingData);
138+
metadataFile.CopyTo(metadata);
139+
result = service.CreateClassifier(
140+
metadata: metadata,
141+
trainingData: trainingData
142+
);
143+
}
144+
}
145+
146+
Console.WriteLine(result.Response);
147+
148+
classifierId = result.Result.ClassifierId;
149+
}
150+
151+
public void GetClassifier()
152+
{
153+
TokenOptions tokenOptions = new TokenOptions()
154+
{
155+
IamApiKey = apikey,
156+
ServiceUrl = url
157+
};
158+
159+
NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);
160+
161+
var result = service.GetClassifier(
162+
classifierId: classifierId
163+
);
164+
165+
Console.WriteLine(result.Response);
166+
}
167+
168+
public void DeleteClassifier()
169+
{
170+
TokenOptions tokenOptions = new TokenOptions()
171+
{
172+
IamApiKey = apikey,
173+
ServiceUrl = url
174+
};
175+
176+
NaturalLanguageClassifierService service = new NaturalLanguageClassifierService(tokenOptions);
177+
178+
var result = service.DeleteClassifier(
179+
classifierId: classifierId
180+
);
181+
182+
Console.WriteLine(result.Response);
183+
}
184+
#endregion
185+
}
186+
}

0 commit comments

Comments
 (0)