介绍 Azure 认识服务中 Language 分类中的文本翻译服务(Translator Text API
。
TEXT_TRANSLATION_API_ENDPOINT = BING_SPELL_CHECK_API_ENDPOINT = ;
WebRequest.Headers.Add(,);
TEXT_TRANSLATION_API_SUBSCRIPTION_KEY = BING_SPELL_CHECK_API_SUBSCRIPTION_KEY = ;
。
uri = TEXT_TRANSLATION_API_ENDPOINT + ;
uri = TEXT_TRANSLATION_API_ENDPOINT + =
WebRequest.Headers.Add(=
</span><span style="color: #008000">//</span><span style="color: #008000"> 把返回的 xml 信息抽取到数组中</span>
response =<span style="color: #000000"> WebRequest.GetResponse();
</span><span style="color: #0000ff">using</span> (Stream stream =<span style="color: #000000"> response.GetResponseStream())
{
DataContractSerializer dcs </span>= <span style="color: #0000ff">new</span> DataContractSerializer(<span style="color: #0000ff">typeof</span>(List<<span style="color: #0000ff">string</span>><span style="color: #000000">));
List</span><<span style="color: #0000ff">string</span>> languagesForTranslate = (List<<span style="color: #0000ff">string</span>><span style="color: #000000">)dcs.ReadObject(stream);
languageCodes </span>=<span style="color: #000000"> languagesForTranslate.ToArray();
}
}
SortedDictionary<,> languageCodesAndTitles =
SortedDictionary<,>(Comparer<>.Create((a,b) => .Compare(a,b,
uri = TEXT_TRANSLATION_API_ENDPOINT + == = = DataContractSerializer(Type.GetType( (Stream stream =
</span><span style="color: #008000">//</span><span style="color: #008000"> 把返回的 xml 信息抽取到数组中</span>
<span style="color: #0000ff">var</span> response =<span style="color: #000000"> request.GetResponse();
</span><span style="color: #0000ff">string</span><span style="color: #000000">[] languageNames;
</span><span style="color: #0000ff">using</span> (Stream stream =<span style="color: #000000"> response.GetResponseStream())
{
languageNames </span>= (<span style="color: #0000ff">string</span><span style="color: #000000">[])dcs.ReadObject(stream);
}
</span><span style="color: #008000">//</span><span style="color: #008000"> 把支持的语言列表及其友好名称保存到字典数据结构中,
</span><span style="color: #008000">//</span><span style="color: #008000"> 随后会把它们绑定给 combo box 控件进行显示</span>
<span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = <span style="color: #800080">0</span>; i < languageNames.Length; i++<span style="color: #000000">)
{
languageCodesAndTitles.Add(languageNames[i],languageCodes[i]);
}
}
count = ( menuItem
</span><span style="color: #008000">//</span><span style="color: #008000"> 设置默认的源语言和目标语言</span>
FromLanguageComboBox.SelectedItem = <span style="color: #800000">"</span><span style="color: #800000">英语</span><span style="color: #800000">"</span><span style="color: #000000">;
ToLanguageComboBox.SelectedItem </span>= <span style="color: #800000">"</span><span style="color: #800000">简体中文</span><span style="color: #800000">"</span><span style="color: #000000">;
}
TEXT_TRANSLATION_API_ENDPOINT + + 待翻译文本 + + 源语言 + + 目标语言
TranslateButton_Click( textToTranslate = fromLanguage = fromLanguageCode = toLanguageCode =
</span><span style="color: #008000">//</span><span style="color: #008000"> 如果要翻译的文本是英语,还可以进行拼写检查</span>
<span style="color: #0000ff">if</span> (fromLanguageCode == <span style="color: #800000">"</span><span style="color: #800000">en</span><span style="color: #800000">"</span><span style="color: #000000">)
{
textToTranslate </span>=<span style="color: #000000"> CorrectSpelling(textToTranslate);
</span><span style="color: #008000">//</span><span style="color: #008000"> 把更新后的文本保存到 UI 控件上</span>
TextToTranslate.Text =<span style="color: #000000"> textToTranslate;
}
</span><span style="color: #008000">//</span><span style="color: #008000"> 处理文本为空和不需要翻译的情况</span>
<span style="color: #0000ff">if</span> (textToTranslate == <span style="color: #800000">""</span> || fromLanguageCode ==<span style="color: #000000"> toLanguageCode)
{
TranslatedText.Text </span>=<span style="color: #000000"> textToTranslate;
</span><span style="color: #0000ff">return</span><span style="color: #000000">;
}
</span><span style="color: #008000">//</span><span style="color: #008000"> 通过 http 请求执行翻译任务</span>
<span style="color: #0000ff">string</span> uri = <span style="color: #0000ff">string</span>.Format(TEXT_TRANSLATION_API_ENDPOINT + <span style="color: #800000">"</span><span style="color: #800000">Translate?text=</span><span style="color: #800000">"</span> +<span style="color: #000000">
System.Web.HttpUtility.UrlEncode(textToTranslate) </span>+ <span style="color: #800000">"</span><span style="color: #800000">&from={0}&to={1}</span><span style="color: #800000">"</span><span style="color: #000000">,fromLanguageCode,toLanguageCode);
</span><span style="color: #0000ff">var</span> translationWebRequest =<span style="color: #000000"> HttpWebRequest.Create(uri);
translationWebRequest.Headers.Add(</span><span style="color: #800000">"</span><span style="color: #800000">Ocp-Apim-Subscription-Key</span><span style="color: #800000">"</span><span style="color: #000000">,TEXT_TRANSLATION_API_SUBSCRIPTION_KEY);
WebResponse response </span>= <span style="color: #0000ff">null</span><span style="color: #000000">;
response </span>=<span style="color: #000000"> translationWebRequest.GetResponse();
</span><span style="color: #008000">//</span><span style="color: #008000"> 把返回的翻译结果抽取到 UI 控件中</span>
Stream stream =<span style="color: #000000"> response.GetResponseStream();
StreamReader translatedStream </span>= <span style="color: #0000ff">new</span> StreamReader(stream,Encoding.GetEncoding(<span style="color: #800000">"</span><span style="color: #800000">utf-8</span><span style="color: #800000">"</span><span style="color: #000000">));
System.Xml.XmlDocument xmlResponse </span>= <span style="color: #0000ff">new</span><span style="color: #000000"> System.Xml.XmlDocument();
xmlResponse.LoadXml(translatedStream.ReadToEnd());
TranslatedText.Text </span>=<span style="color: #000000"> xmlResponse.InnerText;
}
uri = .Format(TEXT_TRANSLATION_API_ENDPOINT + ++ ,toLanguageCode);
(fromLanguageCode == =
TextToTranslate.Text =
CorrectSpelling( uri = BING_SPELL_CHECK_API_ENDPOINT +
HttpWebRequest spellCheckWebRequest == = ;
</span><span style="color: #008000">//</span><span style="color: #008000"> 把文本内容放在请求的 body 中</span>
<span style="color: #0000ff">string</span> body = <span style="color: #800000">"</span><span style="color: #800000">text=</span><span style="color: #800000">"</span> +<span style="color: #000000"> System.Web.HttpUtility.UrlEncode(text);
</span><span style="color: #0000ff">byte</span>[] data =<span style="color: #000000"> Encoding.UTF8.GetBytes(body);
spellCheckWebRequest.ContentLength </span>=<span style="color: #000000"> data.Length;
</span><span style="color: #0000ff">using</span> (<span style="color: #0000ff">var</span> requestStream =<span style="color: #000000"> spellCheckWebRequest.GetRequestStream())
requestStream.Write(data,</span><span style="color: #800080">0</span><span style="color: #000000">,data.Length);
HttpWebResponse response </span>=<span style="color: #000000"> (HttpWebResponse)spellCheckWebRequest.GetResponse();
</span><span style="color: #008000">//</span><span style="color: #008000"> 从返回中取出 json 格式的拼写检查结果</span>
<span style="color: #0000ff">var</span> serializer = <span style="color: #0000ff">new</span><span style="color: #000000"> System.Web.Script.Serialization.JavaScriptSerializer();
</span><span style="color: #0000ff">var</span> responseStream =<span style="color: #000000"> response.GetResponseStream();
</span><span style="color: #0000ff">var</span> jsonString = <span style="color: #0000ff">new</span> StreamReader(responseStream,Encoding.GetEncoding(<span style="color: #800000">"</span><span style="color: #800000">utf-8</span><span style="color: #800000">"</span><span style="color: #000000">)).ReadToEnd();
</span><span style="color: #0000ff">dynamic</span> jsonResponse =<span style="color: #000000"> serializer.DeserializeObject(jsonString);
</span><span style="color: #0000ff">var</span> flaggedTokens = jsonResponse[<span style="color: #800000">"</span><span style="color: #800000">flaggedTokens</span><span style="color: #800000">"</span><span style="color: #000000">];
</span><span style="color: #008000">//</span><span style="color: #008000"> 我们定义一个规则来应用拼写检查的结果,
</span><span style="color: #008000">//</span><span style="color: #008000"> 比如:当拼写检查的权值大于 0.7 时就用建议的值替换掉文本中的值。</span>
<span style="color: #0000ff">var</span> corrections = <span style="color: #0000ff">new</span> SortedDictionary<<span style="color: #0000ff">int</span>,<span style="color: #0000ff">string</span>[]>(Comparer<<span style="color: #0000ff">int</span>>.Create((a,b) =><span style="color: #000000"> b.CompareTo(a)));
</span><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = <span style="color: #800080">0</span>; i < flaggedTokens.Length; i++<span style="color: #000000">)
{
</span><span style="color: #0000ff">var</span> correction =<span style="color: #000000"> flaggedTokens[i];
</span><span style="color: #0000ff">var</span> suggestion = correction[<span style="color: #800000">"</span><span style="color: #800000">suggestions</span><span style="color: #800000">"</span>][<span style="color: #800080">0</span><span style="color: #000000">];
</span><span style="color: #0000ff">if</span> (suggestion[<span style="color: #800000">"</span><span style="color: #800000">score</span><span style="color: #800000">"</span>] > (<span style="color: #0000ff">decimal</span>)<span style="color: #800080">0.7</span><span style="color: #000000">)
corrections[(</span><span style="color: #0000ff">int</span>)correction[<span style="color: #800000">"</span><span style="color: #800000">offset</span><span style="color: #800000">"</span>]] = <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span><span style="color: #000000">[]
{ correction[</span><span style="color: #800000">"</span><span style="color: #800000">token</span><span style="color: #800000">"</span>],suggestion[<span style="color: #800000">"</span><span style="color: #800000">suggestion</span><span style="color: #800000">"</span><span style="color: #000000">] };
}
</span><span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">int</span> i <span style="color: #0000ff">in</span><span style="color: #000000"> corrections.Keys)
{
</span><span style="color: #0000ff">var</span> oldtext = corrections[i][<span style="color: #800080">0</span><span style="color: #000000">];
</span><span style="color: #0000ff">var</span> newtext = corrections[i][<span style="color: #800080">1</span><span style="color: #000000">];
</span><span style="color: #0000ff">if</span> (text.Substring(i,oldtext.Length).All(<span style="color: #0000ff">char</span>.IsUpper)) newtext =<span style="color: #000000"> newtext.ToUpper();
</span><span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (<span style="color: #0000ff">char</span>.IsUpper(text[i])) newtext = newtext[<span style="color: #800080">0</span>].ToString().ToUpper() + newtext.Substring(<span style="color: #800080">1</span><span style="color: #000000">);
text </span>= text.Substring(<span style="color: #800080">0</span>,i) + newtext + text.Substring(i +<span style="color: #000000"> oldtext.Length);
}
</span><span style="color: #0000ff">return</span><span style="color: #000000"> text;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。