|
using System;
using System.IO;
using System.Collections;
using nullpointer.Metaphone;
namespace CSWordLookup
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Main
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Build a HashTable of ArrayLists. The hash table is keyed
//by a Metaphone phonetic key, and the arraylist associated with each key
//is a list of words having that phonetic key as their primary or alternate
//double metaphone key
Hashtable wordsMap = new Hashtable();
FileStream wordsFile = File.OpenRead("..\\..\\..\\namelist.txt");
StreamReader reader = new StreamReader(wordsFile);
String word = reader.ReadLine();
while (word != null) {
DoubleMetaphone mp = new DoubleMetaphone(word);
//Associate word with primary key
ArrayList words = (ArrayList)wordsMap[mp.PrimaryKey];
if (words == null) {
words = new ArrayList();
wordsMap[mp.PrimaryKey] = words;
}
words.Add(word);
//Associate with with alternate key also
if (mp.AlternateKey != null) {
ArrayList words = (ArrayList)wordsMap[mp.AlternateKey];
if (words == null) {
words = new ArrayList();
wordsMap[mp.AlternateKey] = words;
}
words.Add(word);
}
//Read the next word
word = reader.ReadLine();
}
//Begin prompting for search words
while (1) {
System.Console.Write("\nEnter search term (q to quit): ");
String searchWord = System.Console.ReadLine().Trim();
if (searchWord.Length == 0 || searchWord == "q") {
break;
}
DoubleMetaphone searchMphone = new DoubleMetaphone(searchWord);
//Search for matches to the primary key
ArrayList matches = (ArrayList)wordsMap[searchMphone.PrimaryKey];
foreach (String matchingWord in matches) {
System.Console.WriteLine("\tFound: {0}", matchingWord);
}
//Search for matches to the alt, if present
if (searchMphone.AlternateKey != null) {
matches = (ArrayList)wordsMap[searchMphone.AlternateKey];
foreach (String matchingWord in matches) {
System.Console.WriteLine("\tFound: {0}", matchingWord);
}
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
My name is Adam Nelson. I've been a professional programmer since 1996, working on everything from database development, early first-generation web applications, modern n-tier distributed apps, high-performance wireless security tools, to my last job as a Senior Consultant at BearingPoint posted in Baghdad, Iraq training Iraqi developers in the wonders of C# and ASP.NET. I am currently an Engineering Director at Dell.
I have a wide range of skills and interests, including cryptography, image processing, computational linguistics, military history, 3D graphics, database optimization, and mathematics, to name a few.