|
| 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 | +} |
0 commit comments