root/Swishetest/trunk/GetDictionaryWords.pm

Revision 2095, 1.4 kB (checked in by joshr, 7 months ago)

show the program name in our TEST_VERBOSE output.

Line 
1 package GetDictionaryWords;
2
3 use strict;
4 use warnings;
5
6 # get_dictonary_words( $dict, [$case_sensitive_bool], [max_words] )
7 #  given a /usr/dict/words type file, whether or not to be case-sensitive,
8 #  and a max amount of words (0 means no limit)
9 #  reads word-like lines (one word per line)
10 # returns ref to array of the read words and ref to hash of word->count
11 sub get_dictionary_words {
12     my $dict = shift;
13     my ($case_sensitive) = (shift || 0);       
14     my ($max_words) = (shift || 0);     # 0 means don't limit
15     my @words;
16     my %word_count;
17     # Load words. Repeats are OK
18     print STDERR "$0: Loading dictionary...\n" if $ENV{TEST_VERBOSE};
19     open (my $fh, "<", $dict)|| die "$0: Couldn't open $dict: $!";
20     #for($num_words = 0; $words[$num_words] = <$fh>; ) {
21     while( defined( my $word = <$fh> ) && ($max_words == 0 || scalar(@words) < $max_words)) {
22         $word =~ s/[-',.<>]//g;     # strip stuff
23         chomp $word;            # strip newline
24         $word =~ s/^\s+//;
25         $word =~ s/\s+$//;
26         if($word =~ /^$/) {
27             warn "Skipping empty non-word '$word'\n";
28             next;
29         }
30         push(@words, $word);
31         my ($counted_word) = ($case_sensitive ? $word : lc($word));
32         $word_count{$counted_word}++;
33     }
34     close $fh || die "$0: Couldn't close $dict: $!";
35     return (\@words, \%word_count);
36 }
37
38 1;
39
40
Note: See TracBrowser for help on using the browser.