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.
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:
- How to make Preferences persistent
- Parsing the result returned by a Google search
- Determine a window's position on the desktop
- 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:
ulong numberOfHits = 0;
...
string returnedValue = readStream.ReadToEnd();
Regex r = new Regex("&swrnum");
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.
private Rectangle getFormRect ()
{
Rectangle newFormRect = this.DesktopBounds;
newFormRect.X = m_Preferences.MainFrameX;
newFormRect.Y = m_Preferences.MainFrameY;
System.Drawing.Rectangle workingRectangle =
Screen.PrimaryScreen.WorkingArea;
if( ((newFormRect.X < 0) || (newFormRect.Y < 0)) ||
(((newFormRect.X + newFormRect.Width) > workingRectangle.Width) ||
((newFormRect.Y + newFormRect.Height) > workingRectangle.Height)
)
)
{
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:
- Using the Google API
- 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.