In this article you will learn how to use the Google Spell Checker API in Asp.Net C# apps
Download the complete source code from here
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
<spellrequest textalreadyclipped=”0” ignoredups=”0” ignoredigits=”1” ignoreallcaps=”1“>
<text>Hotal</text>
</spellrequest>
The folloing are the Response XML from Google API
<spellresult error=”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
using System; using System.Net; using System.Text; using System.Text.RegularExpressions; public static class SpellChecker { public static String DidYouMean(string word) { string retValue = string.Empty; 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>",word); 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 exp) { } return retValue; } } protected void Page_Load(object sender, EventArgs e) { string word = SpellChecker.DidYouMean("Hotal"); if(word != string.Empty) { labMessage.Text = "<font style='font-size:12px;color:red;'>Did you mean </font><b>" + retValue + "</b>"; } }
You can Download the complete source code from here
Hope this help and If you have any comments, please feel free to write your feedback.
Thanks
Deepu