line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Game::WordBrain::Speller; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
43100
|
use strict; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
185
|
|
4
|
7
|
|
|
7
|
|
26
|
use warnings; |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
169
|
|
5
|
|
|
|
|
|
|
|
6
|
7
|
|
|
7
|
|
28221
|
use Game::WordBrain::WordList; |
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
2534
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.2.1'; # 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
|
2
|
|
|
2
|
1
|
3
|
my $class = shift; |
59
|
2
|
|
|
|
|
4
|
my $args = shift; |
60
|
|
|
|
|
|
|
|
61
|
2
|
50
|
|
|
|
8
|
if( !exists $args->{word_list} ) { |
62
|
2
|
|
|
|
|
11
|
$args->{word_list} = 'Game::WordBrain::WordList'; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
2
|
|
|
|
|
5
|
$args->{_words_cache} = _load_words( $args ); |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
22
|
return bless $args, $class; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _load_words { |
71
|
2
|
|
|
2
|
|
4
|
my $args = shift; |
72
|
|
|
|
|
|
|
|
73
|
2
|
|
|
|
|
6
|
my $words_cache = { }; |
74
|
|
|
|
|
|
|
|
75
|
2
|
50
|
|
|
|
8
|
if( $args->{word_list} eq 'Game::WordBrain::WordList' ) { |
76
|
2
|
|
|
|
|
13
|
my $data_start = tell Game::WordBrain::WordList::DATA; |
77
|
|
|
|
|
|
|
|
78
|
2
|
|
|
|
|
9
|
while( my $word = ) { |
79
|
709972
|
|
|
|
|
398472
|
chomp $word; |
80
|
709972
|
|
|
|
|
1630404
|
$words_cache->{ $word } = 1; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
2
|
|
|
|
|
11
|
seek Game::WordBrain::WordList::DATA, $data_start, 0; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
0
|
0
|
|
|
|
0
|
open( my $words_fh, "<", $args->{word_list} ) or die "Unable to open words file"; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
0
|
while( my $word = <$words_fh> ) { |
89
|
0
|
|
|
|
|
0
|
chomp $word; |
90
|
0
|
|
|
|
|
0
|
$words_cache->{ $word } = 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
close $words_fh; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
2
|
|
|
|
|
14
|
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
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
116
|
0
|
|
|
|
|
|
my $word = shift; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
return exists $self->{_words_cache}{$word}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |