Wednesday, September 30, 2009

 

String Search & IntelliSense/Suggestion in .Net/C#

Problem: Given a set of keywords (or key phrases), provide a mechanism to suggest to user whether s/he is typing a valid word or phrase (i.e. word or phrase from the given list).

Example: Suppose, you wanted to maintain a list of commonly used English words like :

brake, boiling, driving, error, even, event

Then if user :
Types “e”   --> System should suggest –> even, event
Types “br” --> System should suggest –> brake

I had this problem with an additional caveat – the list of words/phrases were actually description of features & with every suggestion i wanted to maintain the association with the feature object. It was close to a generic dictionary in C# only that the key-search was based on “starts with” instead of an exact key match.

So, here’s a simple solution that worked for me. The idea similar to a Definite Finite Automata, only that I’ve  exploited the dictionary generics of C# to maintain the transitions. Here’s how the main classes look like:

smartStoreCD

The generic type T represents the type of the object to be associated with every keyword. A the library and a sample client is shared in my Aha World site. The project is called the SmartTextStore.

Here’s a snap of the sample client with a list of about 850 English words (the word list is included in the solution zip).

TestClient

The solution was developed in Visual Studio 2008 as that’s what i had in my machine. However, the underlying principle for SmartTextStore is simple enough to be ported in Visual Studio 2005 or lesser as long as there is support for generics.


This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]