in reply to Fast wordlist lookup for game
You could take your original idea of splitting the list across files keyed by first letter one step further and split them into subdirs named for the first letter and then files by the second. This would vastly reduce the memory overhead for the look up.
I did this with my own words file containing over 600,000 words and then wrote a small sub to read the appropriate file and a m// to check for its existance and in a thoroughly unscientific benchmark used it to look up a word in the largest subset of each of the 26 first letters and it came out approximately 20 ms/word which should be fast enough for almost any purpose.
The program to split the words file
my ($oldfirst, $oldsecond) = ( '', '' ); open WORDS, '<', $ARGV[0] or die $ARGV[0] . $1 . $/; while(<WORDS>) { chomp; next if length == 1; $_ = lc; my ($first, $second) = /^(.)(.).*/; $first = './' . $first; $second = $first . '/' . (($second eq ' ') ? '_' : $second); if ( ($first ne $oldfirst) or ($second ne $oldsecond) ) { mkdir $first if not -e $first; open SUB, '>>', $second or die $second . $! . $/; } print SUB $_, $/; ($oldfirst, $oldsecond) = ($first, $second); print "\r$second:$_ "; }
And the program for doing the lookup.
#! perl -sw use strict; use Benchmark::Timer; sub IsWord { return if not @_ or length == 1; my ($word, $first, $second) = $_[0] =~ /^((.)(.).*)$/; local $_ = do{ local (@ARGV,$/) = "./$first/$second"; <> }; return /^$word$/im; } my $timer = Benchmark::Timer->new(skip=>0); for (@ARGV) { $timer->start; my $IsWord = IsWord( $_ ); $timer->stop; print $_, $IsWord ? ' is ' : ' is not ', 'a word', $/; } $timer->report; print "@{[$timer->data('_default')]}", $/; __END__
The results and benchmark
C:\test\WORDS>../208899-2 Anonymous Baltic Confirm Delta Entity Font Gregarious Hazzard Indigo Jalopy Kilo Lascivious Matter Notorious Ovulation Prosthetic Quinine Reading Semaphore Transendental Uniform Vivisection Weather Xenophobia Yesterday Zorro Anonymous is a word Baltic is a word Confirm is a word Delta is a word Entity is a word Font is a word Gregarious is a word Hazzard is a word Indigo is a word Jalopy is a word Kilo is a word Lascivious is a word Matter is a word Notorious is a word Ovulation is a word Prosthetic is a word Quinine is a word Reading is a word Semaphore is a word Transendental is not a word Uniform is a word Vivisection is a word Weather is a word Xenophobia is a word Yesterday is a word Zorro is a word 26 trials of _default (522ms total), 20.077ms/trial 0.01 0.01 0.041 0.02 0.02 0.01 0.01 0.02 0.02 0.01 0 0.01 0.02 0.02 0.01 0.08 0.01 0.03 0.04 0.07 0.041 0.01 0.01 0 0 0 C:\test\WORDS>
I seriously doubt that any DBM solution will beat that!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Fast wordlist lookup for game
by grantm (Parson) on Oct 30, 2002 at 04:03 UTC | |
by BrowserUk (Patriarch) on Oct 30, 2002 at 04:55 UTC | |
by BrowserUk (Patriarch) on Oct 30, 2002 at 17:18 UTC | |
by grantm (Parson) on Oct 30, 2002 at 20:17 UTC | |
by BrowserUk (Patriarch) on Oct 30, 2002 at 22:29 UTC | |
by grantm (Parson) on Oct 30, 2002 at 23:29 UTC |