line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Game::WordBrain::Speller; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
32090
|
use strict; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
198
|
|
4
|
8
|
|
|
8
|
|
24
|
use warnings; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
141
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
22582
|
use Game::WordBrain::WordList; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
1823
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.2.2'; # VERSION |
9
|
|
|
|
|
|
|
# ABSTRACT: Checks Spelling of Words |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Game::WordBrain::Speller - Spell Checks Words |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 SYNOPSIS |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Create new Spell Checker |
18
|
|
|
|
|
|
|
my $speller = Game::WordBrain::Speller->new({ |
19
|
|
|
|
|
|
|
word_list => '/path/to/wordlist', # Optional |
20
|
|
|
|
|
|
|
}); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Test if a word is valid |
23
|
|
|
|
|
|
|
my $word = 'nerds'; |
24
|
|
|
|
|
|
|
if( $speller->is_valid_word( $word ) ) { |
25
|
|
|
|
|
|
|
print "Looks like a valid word"; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
else { |
28
|
|
|
|
|
|
|
print "Nope, not a real word"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Originally L made use of L as a speller. The problem was that L provided much more functionalty (and many more dependencies) then what L really needed. Hence, Game::WordBrain::Speller was born. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
This module loads a wordlist into memory and exposes a method to spellcheck words. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head2 word_list |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Path to a new line delimited word list. If not provided, the wordlist provided with this distrubtion will be used. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 METHODS |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 new |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
my $speller = Game::WordBrain::Speller->new({ |
48
|
|
|
|
|
|
|
word_list => '/path/to/wordlist', # Optional |
49
|
|
|
|
|
|
|
}); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
If the word_list is not specified the bundled wordlist will be used. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Returns an instance of L. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new { |
58
|
11
|
|
|
11
|
1
|
372135
|
my $class = shift; |
59
|
11
|
|
|
|
|
18
|
my $args = shift; |
60
|
|
|
|
|
|
|
|
61
|
11
|
100
|
|
|
|
50
|
if( !exists $args->{word_list} ) { |
62
|
10
|
|
|
|
|
563
|
$args->{word_list} = 'Game::WordBrain::WordList'; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
11
|
|
|
|
|
33
|
$args->{_words_cache} = _load_words( $args ); |
66
|
|
|
|
|
|
|
|
67
|
11
|
|
|
|
|
127
|
return bless $args, $class; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _load_words { |
71
|
11
|
|
|
11
|
|
17
|
my $args = shift; |
72
|
|
|
|
|
|
|
|
73
|
11
|
|
|
|
|
21
|
my $words_cache = { }; |
74
|
|
|
|
|
|
|
|
75
|
11
|
100
|
|
|
|
38
|
if( $args->{word_list} eq 'Game::WordBrain::WordList' ) { |
76
|
10
|
|
|
|
|
62
|
my $data_start = tell Game::WordBrain::WordList::DATA; |
77
|
|
|
|
|
|
|
|
78
|
10
|
|
|
|
|
87
|
while( my $word = ) { |
79
|
3549860
|
|
|
|
|
1970706
|
chomp $word; |
80
|
3549860
|
|
|
|
|
7186150
|
$words_cache->{ $word } = 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
10
|
|
|
|
|
51
|
seek Game::WordBrain::WordList::DATA, $data_start, 0; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
1
|
50
|
|
|
|
96
|
open( my $words_fh, "<", $args->{word_list} ) or die "Unable to open words file"; |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
17
|
while( my $word = <$words_fh> ) { |
89
|
354986
|
|
|
|
|
196614
|
chomp $word; |
90
|
354986
|
|
|
|
|
705921
|
$words_cache->{ $word } = 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
56
|
close $words_fh; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
11
|
|
|
|
|
62
|
return $words_cache; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 is_valid_word |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my $speller = Game::WordBrain::Speller->...; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
if( $speller->is_valid_word( 'nerds' ) ) { |
104
|
|
|
|
|
|
|
print 'This is a real word'; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
else { |
107
|
|
|
|
|
|
|
print 'Nope, not really a word.'; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Spell checks a word. Returns a truthy value if the provided word is valid, falsey if it does not. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=cut |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub is_valid_word { |
115
|
1288
|
|
|
1288
|
1
|
942
|
my $self = shift; |
116
|
1288
|
|
|
|
|
773
|
my $word = shift; |
117
|
|
|
|
|
|
|
|
118
|
1288
|
|
|
|
|
1726
|
return exists $self->{_words_cache}{$word}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |