[英]Google Spell Checker API
有人知道嗎? 我通過拼寫檢查“加速器”,這是一個非常好的詞。 我回來了“加速”? 當我在瀏覽器中打開Google並輸入“加速劑”時,它並不表示“加速”?
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;
}
}
}
有意思......我運行了你的代碼,如果我故意將“加速器”作為搜索詞,它就會正確返回“加速器”。 但是,如果我通過“加速器”,它將返回“加速”。 更改語言和文本編碼似乎沒有什么區別。
這里的替代代碼將執行相同的工作。顯然需要一些錯誤處理,但你明白了:)
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.