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

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

<?xml version=”1.0encoding=”utf-8?>
<spellrequest textalreadyclipped=”0ignoredups=”0ignoredigits=”1ignoreallcaps=”1>
<text>Hotal</text>
</spellrequest
>

The folloing are the Response XML from Google API

<?xml version=”1.0encoding=”UTF-8?>
<spellresult error=”0clipped=”0charschecked=”12>
<c o=”0l=”5s=”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

Advertisements

15 thoughts on “Google Spell Checker Api Asp.Net C#

    1. No, It will check one word at a time, so the result would be “dpg” to “dog”, but u can accomplish this by calling multiple times the same service.

      Hope this helps

      Thanks
      Deepu

  1. Hi Deepu,
    It seems specifying “en” as the language means american english. How can I specify UK english. en-gb does not seem to work.

    Thanks

    Leke

    1. public static class SpellChecker
      {
      public static String[] DidYouMean(string word)
      {
      string[] retValue = { “” };
      try
      {
      string uri = “https://www.google.com/tbproxy/spell?lang=en:”;
      using (WebClient webclient = new WebClient())
      {
      string postData = string.Format(“{0}”, 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, @””, string.Empty).Split(‘\t’);
      }
      }
      }
      catch (Exception exp)
      {
      }
      return retValue;
      }
      }

      protected void btnSearch_Click(object sender, EventArgs e)
      {
      labMessage.Visible = true;
      string[] word = SpellChecker.DidYouMean(txtBox.Text);
      for (int i = 0; i < word.Length; i++)
      {
      if (i == 0)
      lblOutPut.Text = word[i];
      else
      lblOutPut.Text += "–" + word[i];
      }
      }

  2. your sample code was working very well but today I got this message in System.Net.WebException (The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel)

    please tell me, how can i solve the problem?

  3. after searching i found it out, this exception is happened because of not valid licensee key. it seems, i need to get a licensee key from google API.
    I do not know how can i get the licensee key? and later where can i use licensee key in your code?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s