Hashtable word finder

February 19, 2008

So this week, in preparation for a Morse code translator and parser, I developed some code that takes in a file of gibberish characters (i.e. “afhefbufbeyioruoehfhajsofvvnmx”) and finds words by cross referencing the substrings with a hashtable of real words.

The idea is that I will find a way to turn tapping into Morse code into a string of characters, and from there this code will handle the job of finding words from them.

Here’s the code:

import java.util.*;
import java.io.IOException;

import a2z.A2ZFileReader;

public class Blm272_week4 {
public static void main(String[] args) throws IOException {

Hashtable dictionary = new Hashtable();

A2ZFileReader fr = new A2ZFileReader(“warandpeace.txt”); //Change to dictionary later

// Read the content and break up into words
String content = fr.getContent();

String regex = “\\b”;
String[] words = content.split(regex);

for (int i = 0; i < words.length; i++) {
if (!dictionary.containsKey(words[i])) {
//String result = words[i];
dictionary.put(words[i], words[i]);
}
}

A2ZFileReader fr2 = new A2ZFileReader(“stuff.txt”); //Morse Source
String word = fr2.getContent();
System.out.println(“Morse:” + word);

//String word = “andthenasIwasstandingthereamongthetreesInoticedahummingbirdinthedistance”;

for (int i = 0; i < word.length(); i++) {
for (int j = i+1; j <= word.length(); j++) {
String subword = word.substring(i,j);
if (dictionary.contains(subword)&&subword.length()>3) {
System.out.println(“found: ” + subword);
}
}
}
}
}

Leave a Reply

You must be logged in to post a comment.