Google Spell Checker Api Asp.Net C#

来源:互联网 发布:kakakaoo的淘宝网店 编辑:程序博客网 时间:2024/06/08 09:49

In this article you will learn how to use the Google Spell Checker API in Asp.Net C# apps

The API is very simple,  spell checking is done through a XML http post to the following url

https://www.google.com/tbproxy/spell?lang=en:

Request XML structure

<?xmlversion=”1.0encoding=”utf-8?>
<spellrequesttextalreadyclipped=”0ignoredups=”0ignoredigits=”1ignoreallcaps=”1>
<text>Hotal</text>
</spellrequest
>

The folloing are the Response XML from Google API

<?xmlversion=”1.0encoding=”UTF-8?>
<spellresulterror=”0clipped=”0charschecked=”12>
<c o=”0l=”5s=”0″>
Hotel Hotly Total Ital Hots</c>
<
/spellresult
>

TagDescriptionoThe offset from the start of the text of the wordlLength of misspelled wordsConfidence of the suggestiontextTab delimited list of suggestions

See the complete code here


public static string DidYouMean(string aWord){    string retValue = string.Empty;    try    {        string uri = "http://www.google.com/tbproxy/spell?";        using (WebClient webClient = new WebClient())        {            WebProxy myProxy = new WebProxy(@"127.0.0.1:48100", true);            webClient.Proxy = myProxy;            string postData = string.Format("<?xml version=\"1.0\" encoding=\"utf-8\" ?><spellrequest textalreadyclipped=\"0\" ignoredups=\"0\" ignoredigits=\"1\" "                + "ignoreallcaps=\"1\"><text>{0}</text></spellrequest>", aWord);            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];            }        }    }    catch (Exception ex)    { }    return retValue;}private void btnSearch_Click(object sender, EventArgs e){    string word = DidYouMean(this.textBox1.Text);    if (word != string.Empty)    {        this.label1.Text = word;    }}

http://www.sxlist.com/techref/spell.asp
http://didyoumean.info/

原创粉丝点击