Google拼写检查API

有人知道吗? 我通过拼写检查“加速器”,这是一个非常好的词。 我回来了“加速”? 当我在浏览器中打开Goog​​le并输入“加速剂”时,它不建议“加速”?

using System; using System.Net; using System.Text; using System.Text.RegularExpressions; namespace SpellCheck { class googel_SP { public string word; public void run_G() { string retValue = string.Empty; string Iword = "accelerants"; try { string uri = "https://www.google.com/tbproxy/spell?lang=en:"; using (WebClient webclient = new WebClient()) { string postData = string.Format(" " + "{0}", Iword); webclient.Headers.Add("Content-Type", "application/x-www-form- urlencoded"); byte[] bytes = Encoding.ASCII.GetBytes(postData); byte[] response = webclient.UploadData(uri, "POST", bytes); string data = Encoding.ASCII.GetString(response); if (data != string.Empty) { retValue = Regex.Replace(data, @"", string.Empty).Split('\t')[0]; Console.WriteLine(" word in -> " + word + " word out -> " + retValue); } } } catch (Exception exp) { } //return retValue; } } } 

有意思……我运行了你的代码,如果我故意将“加速器”作为搜索词,它就会正确地返回“加速器”。 但是,如果我通过“加速器”,它将返回“加速”。 更改语言和文本编码似乎没有什么区别。

这里的替代代码将执行相同的工作。显然需要一些error handling,但你明白了:)

 string retValue = string.Empty; word = "accelerants"; string uri = string.Format( "http://www.google.com/complete/search?output=toolbar&q={0}", word ); HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create( uri ); HttpWebResponse response = ( HttpWebResponse ) request.GetResponse( ); using ( StreamReader sr = new StreamReader( response.GetResponseStream( ) ) ) { retValue = sr.ReadToEnd( ); } XDocument doc = XDocument.Parse( retValue ); XAttribute attr = doc.Root.Element( "CompleteSuggestion" ).Element( "suggestion" ).Attribute( "data" ); string correctedWord = attr.Value;