标签:urlencode seconds operation val nts response 技术 await style
WPF中,对单词拼写错误,textbox有相应的附加属性可以设置。
<TextBox SpellCheck.IsEnabled="True" />
但是此属性只在WPF 4,即.netFramework 4.0,才有效。
并且只支持English、Spanish、French 和German
单词纠错,有一个第三方开源资源可以使用。
1. 下载最新版本(桌面离线版)
2. 解压后,点击languagetool-server.jar,启动服务器模式
启动前提:java环境 下载JavaSetup链接
如需要静默安装java环境,可以使用以下bat命令行:
1 @echo off 2 cls 3 4 set jdkPath=JavaSetup8u211.exe 5 rem 设置jdk安装路径,jre安装路径 6 set commonPath=C:\Program Files (x86) 7 set jreinstallPath="%commonPath%\jre1.8.0_211" 8 9 echo. 10 echo 正在安装jre,需要二、三分钟,请不要执行其他操作 11 echo. 12 start /w %jdkPath% /L "%commonPath%\installjava.log" /s 13 ADDLOCAL="ToolsFeature,SourceFeature,PublicjreFeature" 14 INSTALLDIR=%jreinstallPath% 15 WEB_JAVA=0 AUTO_UPDATE=0 16 echo 安装完成,%jreinstallPath% 17 18 pause
启动服务模式的方式:以命令行的方式启动
java -cp languagetool-server.jar org.languagetool.server.HTTPServer --port 8081
如果需要静默启动,可以新建一个bat文件,放在languageTool离线包文件的外面,使用以下bat脚本:
1 @echo off 2 3 echo. 4 echo 正在启动language-tool-server... 5 cd LanguageTool-4.5 6 echo java -cp languagetool-server.jar org.languagetool.server.HTTPServer --port 8081 7 java -cp languagetool-server.jar org.languagetool.server.HTTPServer --port 8081 8 9 pause
拼接访问地址
访问的端口,是上面环境准备时设置的,也可以使用其它的端口。
接口必填参数:语言、请求文本
请求文本,需要转换为Url编码字符串。
1 private static string GetRequestUrl(string queryText, string language = "en-US") 2 { 3 var requestUrl = "http://localhost:8081/v2/check?" + 4 $"language={language}&text={WebUtility.UrlEncode(queryText)}"; 5 6 return requestUrl; 7 }
请求Api,返回纠错结果。(返回结果,相对应的,也需要将Url编码字符串,转换回文本字符串)
1 public static async Task<CheckEnglishSentenceResponse> CheckEnglishSentenceAsync(string queryText) 2 { 3 var requestUrl = GetRequestUrl(queryText); 4 var result = await RequestUrlAsync(requestUrl); 5 6 var response = JsonConvert.DeserializeObject<CheckEnglishSentenceResponse>(result); 7 return response; 8 }
辅助方法:
1 protected static async Task<string> RequestUrlAsync(string requestUrl) 2 { 3 if (string.IsNullOrWhiteSpace(requestUrl)) 4 { 5 return string.Empty; 6 } 7 8 try 9 { 10 return await RequestDataAsync(requestUrl); 11 } 12 catch (Exception e) 13 { 14 if (e.Message.Contains("502")) 15 { 16 try 17 { 18 await Task.Delay(TimeSpan.FromSeconds(10)); 19 return await RequestUrlAsync(requestUrl); 20 } 21 catch (Exception exception) 22 { 23 } 24 } 25 return string.Empty; 26 } 27 } 28 29 private static async Task<string> RequestDataAsync(string requestUrl) 30 { 31 WebRequest translationWebRequest = WebRequest.Create(requestUrl); 32 33 var response = await translationWebRequest.GetResponseAsync(); 34 35 using (Stream stream = response.GetResponseStream()) 36 { 37 using (StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException(), 38 Encoding.GetEncoding("utf-8"))) 39 { 40 string result = reader.ReadToEnd(); 41 var decodeResult = Unicode2String(result); 42 return decodeResult; 43 } 44 } 45 } 46 47 /// <summary> 48 /// Unicode转字符串 49 /// </summary> 50 /// <param name="source">经过Unicode编码的字符串</param> 51 /// <returns>正常字符串</returns> 52 protected static string Unicode2String(string source) 53 { 54 return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace( 55 source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16))); 56 }
数据类:
1 [DataContract] 2 public class CheckEnglishSentenceResponse 3 { 4 [DataMember(Name = "matches")] 5 public List<EnglishSentenceCheckMatchInfo> MatchInfos { get; set; } 6 } 7 [DataContract] 8 public class EnglishSentenceCheckMatchInfo 9 { 10 [DataMember(Name = "message")] 11 public string Message { get; set; } 12 [DataMember(Name = "shortMessage")] 13 public string ShortMessage { get; set; } 14 15 [DataMember(Name = "context")] 16 public CheckMatchContext CheckMatchContext { get; set; } 17 18 [DataMember(Name = "replacements")] 19 public List<CheckMatchReplacement> Replacements { get; set; } 20 } 21 [DataContract] 22 public class CheckMatchContext 23 { 24 [DataMember(Name = "offset")] 25 public int StartIndex { get; set; } 26 [DataMember(Name = "length")] 27 public int Length { get; set; } 28 [DataMember(Name = "text")] 29 public string Text { get; set; } 30 } 31 32 [DataContract] 33 public class CheckMatchReplacement 34 { 35 [DataMember(Name = "value")] 36 public string Replacement { get; set; } 37 }
纠错展示:
1 string matchString = string.Empty; 2 int index = 1; 3 if (MatchInfos != null) 4 { 5 foreach (var checkMatchInfo in MatchInfos) 6 { 7 var context = checkMatchInfo.CheckMatchContext; 8 var message = string.IsNullOrEmpty(checkMatchInfo.ShortMessage)? checkMatchInfo.Message: checkMatchInfo.ShortMessage; 9 matchString += $"{index++}. " + message + " : " + context.Text.Substring(context.StartIndex, context.Length) + "\r\n"; 10 if (checkMatchInfo.Replacements != null && checkMatchInfo.Replacements.Count > 0) 11 { 12 matchString += "Suggest : " + checkMatchInfo.Replacements[0].Replacement + "\r\n\r\n"; 13 } 14 } 15 } 16 17 return matchString;
以下是案例:
请求本地的后台接口时,会有访问记录:
此案例的源代码,可参考Github-ErrorCorrection
进入文件夹LanguageTool,点击打开InstallJavaEnvironment.bat安装Java
进入文件夹LanguageTool,点击打开StartLanguageToolServer.bat,启动本地离线英语纠错服务。
访问方式:http://localhost:8081/v2/check?language=en-Us&text=buttton
标签:urlencode seconds operation val nts response 技术 await style
原文地址:https://www.cnblogs.com/kybs0/p/11113359.html