Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Article

A universal sentence checking tool

Rate me:
Please Sign up or sign in to vote.
3.43/5 (8 votes)
31 Oct 2006CPOL6 min read 48.8K   594   18   8
A tool to quickly verify a sentence's correctness.

Sample Image

Introduction

Have you already been in a situation in which you, while working on a document, didn’t know exactly how to organize a certain sentence, or, that you doubted the order of some words in a certain expression? Even if the sentence or expression is syntactically and grammatically correct, it might make no sense in the language you’re using. You might just have put some words in an order that your spelling and grammar control doesn’t complain about but that nobody will ever use in that way.

WCheck is a tool that helps you where your spelling and grammar control fails. As far as I can see, the tool can be used for any language.

How does it work

In case you doubt a certain expression, you offer WCheck the two expressions you think make the most sense, after which WCheck will give you an indication about which expression has the highest probability to be correct. This probability is based upon the number of hits WCheck finds on the World Wide Web.

For instance, in the previous picture you see two expressions: the hand that gives and the hand which gives. WCheck found out that the first expression has 29.200 hits on the WWW while the second only has 141 hits. An important difference that might make you decide that the first expression is probably correct. If you’re still not convinced you can click the blue underlined number after which WCheck will start you’re default browser and show you the results of its search on the WWW.

Of course, the more data WCheck can search, the better the prediction will be. This means, for instance, that in case you have WCheck searching for English expressions, the results will be more reliable than in case you enter a Swahili expression. The reason is simple: on the Web there is much more information available in English than it is the case for the Swahili language.

However, never forget that this is not the golden bullet tool. All statements about correctness depend on accidental numbers and are therefore not necessarily true.

Preferences

WCheck can choose between two methods to find the number of hits: via the Google API or via a simple Google search request. In case you push the Control key the text of the Reset button will change to 'Setting'. When you then click the button the Preferences dialog pops up.

Image 2

As you can see in the picture above, the Search string resembles the URL that is generated when you execute a Google search in your browser. The Search string contains a place holder that is filled with the phrase or word you want to check. If you want to, e.g. in case you're using another search engine, you can enter a new search string. Beware not to forget adding the place holder.

The Proxy field is currently disabled. Maybe something for a next release.

In case you want to use the Google API you first have to obtain a license key from the Google site (see this link). Also, notice that the number of hits the Google API and the Google Search string return for a certain search argument, differ. Although it's not quite clear to me why this is, I don't care since for a particular search argument not the absolute number of hits is important but the difference between the hits.

At the bottom of the dialog you can optionally check the 'Always on top' checkbox. This will make the tool float on top of all other windows.

Using the code

This is not a rocket science application. I will therefore not discuss the whole application but merely highlight some interesting aspects that learned me something new. These new things are:

  1. How to make Preferences persistent
  2. Parsing the result returned by a Google search
  3. Determine a window's position on the desktop
  4. Using a Web Service (Google)

How to make Preferences persistent

In the past, I already used several methods to make user preferences persistent. INI files, Registry entries and MS Application Building blocks were the most popular. About a year ago I ran into this CodeProject article from Chad Z. Hower titled 'Application settings the .Net way. INI, Registry, or XML". It describes how you can use XML to save your settings. It's really easy! Just add a new DataSet to your application, update the DataSet with the preference fields you need and compile. Don't forget to specify MSDataSetGenerator as the transformation tool for your DataSet definition. Visual Studio will use this tool to make a set of classes out of the DataSet that you can use to manage your preferences.

In the WCheck application, I use three classes: frmPreferences, PreferenceData and Config. The frmPreferences class is a simple Form class defining the Preference Dialog. The PreferenceData class represents the preference data as seen by the application and contains methods such as LoadFromDisk and SaveToDisk. Finally, Config is the class generated based upon the Config.xsd DataSet. In fact, the Config class represents the Preference Data on disk.

Parsing the result returned by a Google search

Looking at the result returned by a Google Search you see the number of hits somewhere at the top of the page. Inside the source of this HTML page the number of hits can be found by searching for the '&swrnum' phrase: the number is located right of it. Therefore, to get the number into my WCheck window, the HTML code is parsed using Regular Expressions. Here is how it goes:

C#
// numberOfHits will contain the result at the end of the method
ulong numberOfHits = 0;
...

// Get the HTML source out of a stream
string returnedValue = readStream.ReadToEnd();

Regex r = new Regex("&swrnum");

// Find a single match in the string.
Match m = r.Match(returnedValue); 
if (m.Success) 
{
    int i = m.Index + 1;
    while(returnedValue[i++] != '=');

    int start = i;
    while(returnedValue[i++] != '>');

    string numberStr = returnedValue.Substring(start,i-start-1);
    numberOfHits = Convert.ToUInt64(numberStr);
}

Determine a window's position on the desktop

I wanted the application to remember its last position on the screen. The getFormRect method is implemented in such a way that the WCheck Form will always be completely visible on the primary screen. Whenever a part of the main form falls outside the working area, the default location is used.

C#
private Rectangle getFormRect ()
{
    // Get Form Bounds
    Rectangle newFormRect = this.DesktopBounds;

    // Update the current position with the last known position
    newFormRect.X = m_Preferences.MainFrameX;
    newFormRect.Y = m_Preferences.MainFrameY;

    // Get maximum drawing area
    System.Drawing.Rectangle workingRectangle = 
              Screen.PrimaryScreen.WorkingArea;
            
    // Check whether the Form is completely visible
    if( ((newFormRect.X < 0) || (newFormRect.Y < 0)) || 
        (((newFormRect.X + newFormRect.Width) > workingRectangle.Width) || 
         ((newFormRect.Y + newFormRect.Height) > workingRectangle.Height)
        )
      )
    {
        // Set default position
        newFormRect.X = 1;
        newFormRect.Y = workingRectangle.Height - newFormRect.Height - 1;
    }

    return newFormRect;
}

Using a Web Service

To use the Google Web Service you have to add a Web Reference to your project. The URL of the Web Service is available here. Once the reference is in the project you can take a deeper look using the Object Browser.

Take a look at this site to find out that the Google API still carries at least one problem.

Known problem

Sometimes the WCheck returns an error upon hitting the Check button. The problem disappears once I start Explorer. I don't know why this happens, but frankly I didn't pay that much attention to it yet.

Points of Interest

The things I found interesting while doing this little application are:

  1. Using the Google API
  2. Learning some .Net details

Ideas

You know the Babylon tool? It's one of my very favorite tools on PC. What I would like to see is the tool being extended with the WCheck feature. Hey guys this is a tip. ;-)

History

  • 28 October 2006 - Version 1.0.2.0, initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
Generalusage Pin
padravi12-Feb-07 6:53
padravi12-Feb-07 6:53 
Generalcorrectness of the sentence Pin
padravi12-Feb-07 6:50
padravi12-Feb-07 6:50 
GeneralNice Pin
carlop()10-Nov-06 6:19
carlop()10-Nov-06 6:19 
GeneralBut Pin
GerhardKreuzer6-Nov-06 19:27
GerhardKreuzer6-Nov-06 19:27 
GeneralCorrectness Pin
GerhardKreuzer6-Nov-06 19:24
GerhardKreuzer6-Nov-06 19:24 
Nice idea, but only empowers incorrect phrases.

By the way, AN Universal ......., do you check your article with your own tool?

gerhardHmmm | :|
GeneralRe: Correctness Pin
Dominique Bijnens7-Nov-06 2:49
Dominique Bijnens7-Nov-06 2:49 
GeneralRe: Correctness Pin
leppie12-Jul-07 21:34
leppie12-Jul-07 21:34 
QuestionCorrectness or popularity? Pin
Peter Ritchie6-Nov-06 9:26
Peter Ritchie6-Nov-06 9:26 

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.