2012-09-15 136 views
3

有谁知道吗?我通过拼写检查“促进剂”,这是一个非常好的词。我回来“加速”?当我在浏览器中打开Goog​​le并输入“accelerants”时,它不会显示“加速”?Google拼写检查程序API

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("<?xml version=\"1.0\"  encoding=\"utf-8\" ?> " 
        + "<spellrequest textalreadyclipped=\"0\" ignoredups=\"0\"  ignoredigits=\"1\" " 
        + "ignoreallcaps=\"1\"><text>{0}</text></spellrequest>", 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, @"<(.|\n)*?>",  string.Empty).Split('\t')[0]; 
         Console.WriteLine(" word in -> " + word + " word out -> " +  retValue); 
        } 
       } 
      } 
      catch (Exception exp) 
      { 

      } 
      //return retValue; 
     } 
    } 

} 

回答

0

有趣......我跑你的代码,如果我故意通过“accelrants”作为搜索词,它正确返回“促进剂”。但是,如果我通过“促进剂”,它会返回“加速”。改变语言和文本编码似乎没有什么区别。

这里的备用代码,将做同样的job..obviously需要一些错误处理的,但你的想法:)

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;