|
| 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