Click here to Skip to main content
15,886,788 members
Articles / Web Development / HTML
Article

Google Suggest like Dictionary

Rate me:
Please Sign up or sign in to vote.
4.87/5 (78 votes)
26 Dec 20043 min read 307.8K   3.2K   206   70
An implementation of Google suggest using remote scripting.

Dictionary

Introduction

This is an implementation of Google Suggest like dictionary in ASP.NET.

Background

After seeing Google Suggest, I was amazed and wanted to know how it worked. This project is just an experiment using the same technique that Google uses.

Using the code

The ZIP file consists of two files. One is an HTML file that uses the XMLHttpRequest object to make requests and get back data. The other one is a server script implemented in ASP.NET that connects to a SQL Server database. If you wish to implement a similar interface, just use the HTML functions provided. A proof of concept website could be accessed here.

How does it work?

The architecture could be explained as outlined below:

  • Concept
  • The database
  • ASP.NET page
  • XMLHttpRequest object in the HTML page
  • Observations

Concept

As you type a word in the textbox, a JavaScript event fires an HTTP GET request to the ASPX page. The response from the ASPX page is simply displayed in a div tag under the textbox. The page is not refreshed/reloaded for every keystroke as everything is done by the JavaScript in the page. The main JavaScript object that allows us to do this is XMLHttpRequest. You could read about it from Apple's developer site here. This is supported by IE 5.0 +, Mozilla 1.0 + and Apple's own Safari 1.2 +.

Image 2

Database

The database contains just one table. The data comes from a freely available online dictionary (a public domain English word list dictionary, based on the public domain portion of "The Project Gutenberg Etext of Webster's Unabridged Dictionary" which is in turn based on the 1913 US Webster's Unabridged Dictionary. You could download it from here). The table is called WordList:

WordList
Wordvarchar(255)
Typevarchar (10)
Meaningtext

I created an index on "Word" column for speed. There are a total of 182696 words in the database.

ASP.NET page

The ASP.NET page is pretty straight forward. Gets the top 10 matching rows from the database and spits it out. Below is the code I am using (although SqlDataReader might be more appropriate):

C#
<%@Page Language="C#"%>
<%@Import Namespace="System.Data"%>
<%@Import Namespace="System.Data.SqlClient"%>
<%@Import Namespace="System.Configuration"%>

<script runat="server">

    public void Page_Load(object sender,EventArgs args)
    {
        string keyword=Request["k"];
        if(keyword!=null  && keyword.Trim()!="")
        {
            string sql="select top 10* from WordList where" + 
                   " word like '"+keyword.Trim().Replace("'","''")+"%'";
            SqlConnection conn=new 
               SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
            conn.Open();
            DataTable dt=new DataTable();
            SqlCommand command=new SqlCommand(sql,conn);
            SqlDataAdapter adapter=new SqlDataAdapter(command);
            adapter.Fill(dt);
            conn.Close();

            foreach(DataRow row in dt.Rows)
            {
                string meaning=row["Meaning"].ToString();
                Response.Write("<strong>"+row["Word"].ToString()+"</strong> <i>");
                  Response.Write("row["Type"].ToString()+"</i>: "+meaning+"<br>");
            }
        }


    }

</script>

XMLHttpRequest object in the HTML page

JavaScript
<html>
    <head>
        <script>
var req;

function Initialize()
{
    try
    {
        req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            req=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            req=null;
        }
    }

    if(!req&&typeof XMLHttpRequest!="undefined")
    {
        req=new XMLHttpRequest();
    }

}

function SendQuery(key)
{
    Initialize();
    var url="http://www.objectgraph.com/dictionary/dict.aspx?k="+key;

    if(req!=null)
    {
        req.onreadystatechange = Process;
        req.open("GET", url, true);
        req.send(null);

    }

}

function Process()
{
    if (req.readyState == 4)
        {
        // only if "OK"
            if (req.status == 200)
            {
                if(req.responseText=="")
                    HideDiv("autocomplete");
                else
                {
                    ShowDiv("autocomplete");
                    document.getElementById("autocomplete").innerHTML = 
                                                      req.responseText;
                }
            }
            else
            {
                document.getElementById("autocomplete").innerHTML=
                    "There was a problem retrieving data:<br>" 
                    + req.statusText;
            }
        }
}

function ShowDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="show";
   else document.getElementById(divid).style.visibility="visible";
}

function HideDiv(divid)
{
   if (document.layers) document.layers[divid].visibility="hide";
   else document.getElementById(divid).style.visibility="hidden";
}

function BodyLoad()
{
    HideDiv("autocomplete");
    document.form1.keyword.focus();

}
</script>
    </head>
    <body onload="BodyLoad();">
        <form name="form1">
        <input name="keyword" onKeyUp="SendQuery(this.value)" 
          style="WIDTH:500px" autocomplete="off">
            <div align="left" class="box" id="autocomplete" 
              style="WIDTH:500px;BACKGROUND-COLOR:#ccccff"></div>
        </form>

    </body>
</html>

The KeyUp event on the textbox triggers the SendQuery method. Note that we are disabling the auto-complete feature so it does not overlap with our div tag. The Initialize method creates the XMLHttpRequest object. In Mozilla and Safari, you could simply do this by using:

C#
req=new XMLHttpRequest();

In IE, you could create the object based on the user's installation of MSXML libraries. The Process method acts as an event handler and displays the response text once the response is complete from the web server.

Observations

  • Google Suggest is probably running by having all the suggestion words in main memory and a custom web server that does nothing else (although this concept is very easy to implement in C#).
  • Roundtrips from the web server should be minimal (2K max) as it might eat up bandwidth pretty fast.

History

  • December 24, 2004
    • Initial version 1.0.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: How about the performance? Pin
Spiff Dog20-Feb-06 14:58
Spiff Dog20-Feb-06 14:58 
Generalaspx page returns blanks Pin
libajgl13-Jan-05 10:27
libajgl13-Jan-05 10:27 
GeneralVery cool Pin
Mike Ellison5-Jan-05 7:43
Mike Ellison5-Jan-05 7:43 
GeneralASP.NET 2.0 Callbacks will ease this implementation Pin
Anonymous5-Jan-05 6:38
Anonymous5-Jan-05 6:38 
GeneralRe: ASP.NET 2.0 Callbacks will ease this implementation Pin
Gavi Narra5-Jan-05 6:58
Gavi Narra5-Jan-05 6:58 
GeneralRe: ASP.NET 2.0 Callbacks will ease this implementation Pin
Thushan Fernando5-Aug-05 16:51
Thushan Fernando5-Aug-05 16:51 
GeneralFantastic Work Pin
Albert Pascual4-Jan-05 13:54
sitebuilderAlbert Pascual4-Jan-05 13:54 
Generalsql dictionary database link Pin
Quinton Viljoen4-Jan-05 1:04
Quinton Viljoen4-Jan-05 1:04 
Hi. The link to the database is pointing to a zip file which contains the whole english dictionary in html files, is that correct? If so, how do I import that into a table, as I am unaware that it is possible importing from html to a table.

Is there a alternate link to a csv file or the database itself perhaps?

Great article!

Thanks
GeneralRe: sql dictionary database link Pin
Gavi Narra4-Jan-05 6:42
Gavi Narra4-Jan-05 6:42 
GeneralRe: sql dictionary database link Pin
smiling4ever6-Jun-05 1:27
smiling4ever6-Jun-05 1:27 
QuestionGoogle Suggest? Pin
Ashley van Gerven28-Dec-04 12:10
Ashley van Gerven28-Dec-04 12:10 
AnswerRe: Google Suggest? Pin
Gavi Narra28-Dec-04 12:24
Gavi Narra28-Dec-04 12:24 
GeneralRe: Google Suggest? Pin
Ashley van Gerven28-Dec-04 12:33
Ashley van Gerven28-Dec-04 12:33 
GeneralMy 5 Pin
Kant28-Dec-04 3:58
Kant28-Dec-04 3:58 
GeneralRe: My 5 Pin
Ameen AboDabash9-Jun-05 12:25
Ameen AboDabash9-Jun-05 12:25 
GeneralGood work and Congratulation Pin
Pham Ngoc Quyen27-Dec-04 23:48
Pham Ngoc Quyen27-Dec-04 23:48 
GeneralRe: Good work and Congratulation Pin
Gavi Narra28-Dec-04 12:25
Gavi Narra28-Dec-04 12:25 
GeneralBueno Pin
Narendar Vuyyuru27-Dec-04 2:58
Narendar Vuyyuru27-Dec-04 2:58 
GeneralGreat Work Pin
ilias mavreas27-Dec-04 2:41
ilias mavreas27-Dec-04 2:41 
GeneralRe: Great Work Pin
Eng. Jalal4-Jun-05 0:39
Eng. Jalal4-Jun-05 0:39 
Generalexcellent Pin
Rui Dias Lopes27-Dec-04 0:52
Rui Dias Lopes27-Dec-04 0:52 
GeneralGreat Pin
Paul Watson27-Dec-04 0:12
sitebuilderPaul Watson27-Dec-04 0:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.