Skip to content

Commit 4bf9c54

Browse files
committed
feat(NaturalLanguageUnderstanding): add examples for NaturalLanguageUnderstanding
1 parent 18cb7e9 commit 4bf9c54

4 files changed

+316
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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.Watson.NaturalLanguageUnderstanding.V1;
19+
using IBM.Watson.NaturalLanguageUnderstanding.V1.Model;
20+
using IBM.Cloud.SDK.Utilities;
21+
using IBM.Cloud.SDK.Authentication;
22+
using System.Collections;
23+
using System.Collections.Generic;
24+
using UnityEngine;
25+
using IBM.Cloud.SDK;
26+
27+
28+
namespace IBM.Watson.Examples
29+
{
30+
public class ExampleNaturalLanguageUnderstandingV1 : MonoBehaviour
31+
{
32+
#region PLEASE SET THESE VARIABLES IN THE INSPECTOR
33+
[Space(10)]
34+
[Tooltip("The IAM apikey.")]
35+
[SerializeField]
36+
private string iamApikey;
37+
[Tooltip("The service URL (optional). This defaults to \"https://gateway.watsonplatform.net/natural-language-understanding/api\"")]
38+
[SerializeField]
39+
private string serviceUrl;
40+
[Tooltip("The version date with which you would like to use the service in the form YYYY-MM-DD.")]
41+
[SerializeField]
42+
private string versionDate;
43+
#endregion
44+
45+
private NaturalLanguageUnderstandingService service;
46+
private string nluText = "IBM is an American multinational technology company headquartered in Armonk, New York, United States with operations in over 170 countries.";
47+
private void Start()
48+
{
49+
LogSystem.InstallDefaultReactors();
50+
Runnable.Run(CreateService());
51+
}
52+
53+
private IEnumerator CreateService()
54+
{
55+
if (string.IsNullOrEmpty(iamApikey))
56+
{
57+
throw new IBMException("Please add IAM ApiKey to the Iam Apikey field in the inspector.");
58+
}
59+
60+
IamTokenOptions tokenOptions = new IamTokenOptions()
61+
{
62+
IamApiKey = iamApikey
63+
};
64+
Credentials credentials = new Credentials(tokenOptions, serviceUrl);
65+
66+
while (!credentials.HasTokenData())
67+
{
68+
yield return null;
69+
}
70+
71+
service = new NaturalLanguageUnderstandingService(versionDate, credentials);
72+
73+
Runnable.Run(ExampleAnalyze());
74+
Runnable.Run(ExampleListModels());
75+
Runnable.Run(ExampleDeleteModel());
76+
}
77+
78+
private IEnumerator ExampleAnalyze()
79+
{
80+
Features features = new Features()
81+
{
82+
Keywords = new KeywordsOptions()
83+
{
84+
Limit = 2,
85+
Sentiment = true
86+
},
87+
};
88+
AnalysisResults analyzeResponse = null;
89+
90+
service.Analyze(
91+
callback: (DetailedResponse<AnalysisResults> response, IBMError error) =>
92+
{
93+
Log.Debug("NaturalLanguageUnderstandingServiceV1", "Analyze result: {0}", response.Response);
94+
analyzeResponse = response.Result;
95+
},
96+
features: features,
97+
text: nluText
98+
);
99+
100+
while (analyzeResponse == null)
101+
yield return null;
102+
}
103+
104+
private IEnumerator ExampleListModels()
105+
{
106+
ListModelsResults listModelsResponse = null;
107+
108+
service.ListModels(
109+
callback: (DetailedResponse<ListModelsResults> response, IBMError error) =>
110+
{
111+
Log.Debug("NaturalLanguageUnderstandingServiceV1", "ListModels result: {0}", response.Response);
112+
listModelsResponse = response.Result;
113+
}
114+
);
115+
116+
while (listModelsResponse == null)
117+
yield return null;
118+
}
119+
120+
private IEnumerator ExampleDeleteModel()
121+
{
122+
DeleteModelResults deleteModelResponse = null;
123+
124+
service.DeleteModel(
125+
callback: (DetailedResponse<DeleteModelResults> response, IBMError error) =>
126+
{
127+
Log.Debug("NaturalLanguageUnderstandingServiceV1", "DeleteModel result: {0}", response.Response);
128+
deleteModelResponse = response.Result;
129+
},
130+
modelId: "<modelId>" // Enter model Id here
131+
);
132+
133+
while (deleteModelResponse == null)
134+
yield return null;
135+
}
136+
137+
}
138+
}

Examples/ExampleNaturalLanguageUnderstandingV1.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!29 &1
4+
OcclusionCullingSettings:
5+
m_ObjectHideFlags: 0
6+
serializedVersion: 2
7+
m_OcclusionBakeSettings:
8+
smallestOccluder: 5
9+
smallestHole: 0.25
10+
backfaceThreshold: 100
11+
m_SceneGUID: 00000000000000000000000000000000
12+
m_OcclusionCullingData: {fileID: 0}
13+
--- !u!104 &2
14+
RenderSettings:
15+
m_ObjectHideFlags: 0
16+
serializedVersion: 9
17+
m_Fog: 0
18+
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
19+
m_FogMode: 3
20+
m_FogDensity: 0.01
21+
m_LinearFogStart: 0
22+
m_LinearFogEnd: 300
23+
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
24+
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
25+
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
26+
m_AmbientIntensity: 1
27+
m_AmbientMode: 0
28+
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
29+
m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0}
30+
m_HaloStrength: 0.5
31+
m_FlareStrength: 1
32+
m_FlareFadeSpeed: 3
33+
m_HaloTexture: {fileID: 0}
34+
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
35+
m_DefaultReflectionMode: 0
36+
m_DefaultReflectionResolution: 128
37+
m_ReflectionBounces: 1
38+
m_ReflectionIntensity: 1
39+
m_CustomReflection: {fileID: 0}
40+
m_Sun: {fileID: 0}
41+
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074, b: 0.35872698, a: 1}
42+
m_UseRadianceAmbientProbe: 0
43+
--- !u!157 &3
44+
LightmapSettings:
45+
m_ObjectHideFlags: 0
46+
serializedVersion: 11
47+
m_GIWorkflowMode: 0
48+
m_GISettings:
49+
serializedVersion: 2
50+
m_BounceScale: 1
51+
m_IndirectOutputScale: 1
52+
m_AlbedoBoost: 1
53+
m_EnvironmentLightingMode: 0
54+
m_EnableBakedLightmaps: 1
55+
m_EnableRealtimeLightmaps: 1
56+
m_LightmapEditorSettings:
57+
serializedVersion: 10
58+
m_Resolution: 2
59+
m_BakeResolution: 40
60+
m_AtlasSize: 1024
61+
m_AO: 0
62+
m_AOMaxDistance: 1
63+
m_CompAOExponent: 1
64+
m_CompAOExponentDirect: 0
65+
m_Padding: 2
66+
m_LightmapParameters: {fileID: 0}
67+
m_LightmapsBakeMode: 1
68+
m_TextureCompression: 1
69+
m_FinalGather: 0
70+
m_FinalGatherFiltering: 1
71+
m_FinalGatherRayCount: 256
72+
m_ReflectionCompression: 2
73+
m_MixedBakeMode: 2
74+
m_BakeBackend: 1
75+
m_PVRSampling: 1
76+
m_PVRDirectSampleCount: 32
77+
m_PVRSampleCount: 500
78+
m_PVRBounces: 2
79+
m_PVRFilterTypeDirect: 0
80+
m_PVRFilterTypeIndirect: 0
81+
m_PVRFilterTypeAO: 0
82+
m_PVRFilteringMode: 1
83+
m_PVRCulling: 1
84+
m_PVRFilteringGaussRadiusDirect: 1
85+
m_PVRFilteringGaussRadiusIndirect: 5
86+
m_PVRFilteringGaussRadiusAO: 2
87+
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
88+
m_PVRFilteringAtrousPositionSigmaIndirect: 2
89+
m_PVRFilteringAtrousPositionSigmaAO: 1
90+
m_ShowResolutionOverlay: 1
91+
m_LightingDataAsset: {fileID: 0}
92+
m_UseShadowmask: 1
93+
--- !u!196 &4
94+
NavMeshSettings:
95+
serializedVersion: 2
96+
m_ObjectHideFlags: 0
97+
m_BuildSettings:
98+
serializedVersion: 2
99+
agentTypeID: 0
100+
agentRadius: 0.5
101+
agentHeight: 2
102+
agentSlope: 45
103+
agentClimb: 0.4
104+
ledgeDropHeight: 0
105+
maxJumpAcrossDistance: 0
106+
minRegionArea: 2
107+
manualCellSize: 0
108+
cellSize: 0.16666667
109+
manualTileSize: 0
110+
tileSize: 256
111+
accuratePlacement: 0
112+
debug:
113+
m_Flags: 0
114+
m_NavMeshData: {fileID: 0}
115+
--- !u!1 &800506884
116+
GameObject:
117+
m_ObjectHideFlags: 0
118+
m_CorrespondingSourceObject: {fileID: 0}
119+
m_PrefabInstance: {fileID: 0}
120+
m_PrefabAsset: {fileID: 0}
121+
serializedVersion: 6
122+
m_Component:
123+
- component: {fileID: 800506887}
124+
- component: {fileID: 800506885}
125+
m_Layer: 0
126+
m_Name: ExampleNaturalLanguageUnderstandingV1
127+
m_TagString: Untagged
128+
m_Icon: {fileID: 0}
129+
m_NavMeshLayer: 0
130+
m_StaticEditorFlags: 0
131+
m_IsActive: 1
132+
--- !u!114 &800506885
133+
MonoBehaviour:
134+
m_ObjectHideFlags: 0
135+
m_CorrespondingSourceObject: {fileID: 0}
136+
m_PrefabInstance: {fileID: 0}
137+
m_PrefabAsset: {fileID: 0}
138+
m_GameObject: {fileID: 800506884}
139+
m_Enabled: 1
140+
m_EditorHideFlags: 0
141+
m_Script: {fileID: 11500000, guid: dfdc93e9c48b5475fbceaff19b33d951, type: 3}
142+
m_Name:
143+
m_EditorClassIdentifier:
144+
iamApikey:
145+
serviceUrl:
146+
versionDate:
147+
--- !u!4 &800506887
148+
Transform:
149+
m_ObjectHideFlags: 0
150+
m_CorrespondingSourceObject: {fileID: 0}
151+
m_PrefabInstance: {fileID: 0}
152+
m_PrefabAsset: {fileID: 0}
153+
m_GameObject: {fileID: 800506884}
154+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
155+
m_LocalPosition: {x: 0, y: 0, z: 0}
156+
m_LocalScale: {x: 1, y: 1, z: 1}
157+
m_Children: []
158+
m_Father: {fileID: 0}
159+
m_RootOrder: 0
160+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Examples/ExampleNaturalLanguageUnderstandingV1.unity.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)