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.0”encoding=”utf-8”?>
<spellrequesttextalreadyclipped=”0”ignoredups=”0”ignoredigits=”1”ignoreallcaps=”1“>
<text>Hotal</text>
</spellrequest >
<spellrequesttextalreadyclipped=”0”ignoredups=”0”ignoredigits=”1”ignoreallcaps=”1“>
<text>Hotal</text>
</spellrequest >
The folloing are the Response XML from Google API
<?
xmlversion=”1.0”encoding=”UTF-8“?>
<spellresulterror=”0”clipped=”0”charschecked=”12“>
<c o=”0”l=”5”s=”0″>Hotel Hotly Total Ital Hots </c>
</spellresult >
<spellresulterror=”0”clipped=”0”charschecked=”12“>
<c o=”0”l=”5”s=”0″>Hotel Hotly Total Ital Hots </c>
</spellresult >
Tag | Description |
o | The offset from the start of the text of the word |
l | Length of misspelled word |
s | Confidence of the suggestion |
text | Tab 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/
http://didyoumean.info/