2012-09-15 90 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;