Google Spell Checker Api Asp.Net C#

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
>

TagDescription
oThe offset from the start of the text of the word
lLength of misspelled word
sConfidence of the suggestion
textTab 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/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值