views:

161

answers:

3

Suppose I have a randomly generated string s=t&^%JHGgfdteam*&HGEdfg, what is the best approach to count the number of English words in that string? (English words as defined in some dictionary file). Obviously brute-force is not a good idea... would a suffix-tri e work? Binary search? Notice that in the case of s, there are two words: "tea" and "team". Any ideas? Regards

A: 

A start:

How do I determine if a random string sounds like English?

Leniel Macaferi
Thanks... but I don't need to build a FSM for this. I have a string, and I have a list of words. This is merely a comparison task... just wondering what is the best DS to do this (space and time).
Dervin Thunk
+8  A: 

I would load the dictionary words in a Trie structure, then read the string from left to right and check if the substrings are in the trie. If they are and there are children, keep going. If they happen to be a leaf or a valid word, add to the occurence count.

In pseudo code:

Trie dict = ... // load dictionary
Dictionary occurences = {}

for i in length(string):
    j = i + 1
    # think of partial as string.Substring(i, j);
    while dict.hasChildren(partial):
        j++ 
        if isWord(partial):
            dict[partial]++

This way you'll guarantee it doesn't miss a match while still looking for all possibilities.

You can limit the minimum length of the valid words by changing what j is initialized to or by rejecting short words in the isWord() method (so a wouldn't be a "valid" word).

NullUserException
This should be more than enough to start with. Thanks!
Dervin Thunk
+6  A: 

The Aho-Corasick string matching algorithm builds the matching structure in time linear in the size of the dictionary and matches patterns at time linear in the size of the input text + number of matches found.

mcdowella
+1: A trie is good, but a trie + a good search algorithm is far better.
James McNellis
Nice complement. Upvoted.
Dervin Thunk