WordBrain/Prefix.pm

filter: Statementstmt 100% 41 / 41 Branchbran 95.0% 19 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 98.5% 68 / 69 CC 11 SCAR 17.7
CC
11
Cov
98%
_load_words
9.0
new
3.0
BEGIN
1.0
CRAP 5.9
↑ Summary WordBrain.pm »
1 package Game::WordBrain::Prefix;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Game::WordBrain::WordList;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our $VERSION = '0.2.2'; # VERSION
9 # ABSTRACT: Creates a Cache of Valid Word Prefixes
10  
11 =head1 NAME
12  
13 Game::WordBrain::Prefix - Creates a Cache of Valid Word Prefixes
14  
15 =head1 SYNOPSIS
16  
17 # Create new Prefix Cache
18 my $prefix = Game::WordBrain::Prefix->new({
19 max_prefix_length => 5, # Optional
20 word_list => '/path/to/wordlist', # Optional
21 });
22  
23 # Test if a string could be the start of a word
24 my $start_of_word = 'fla';
25 if( $prefix->is_start_of_word( $start_of_word ) ) {
26 print "Could be a word...";
27 }
28 else {
29 print "Nope, no way this is going to be a word.";
30 }
31  
32  
33 =head1 DESCRIPTION
34  
35 L<Game::WordBrain::Prefix> is the largest speedup afforded to the L<Game::WordBrain>->solve method. It works by reading in a wordlist and using it to construct a hash of valid word prefixes. As an example, let's take the word "flag"
36  
37 {
38 'f' => 1,
39 'fl' => 1,
40 'fla' => 1,
41 'flag' => 1,
42 }
43  
44 By creating this L<Game::WordBrain> is able to check if the current path being walked ( collection of L<Game::WordBrain::Letter>s ) could possibly ever be a real word. By leverage the fact that, for example, no word in the english language starts with 'flaga' we can short circuit and abandon a path that will not lead to a solution sa fast as possible.
45  
46 =head1 ATTRIBUTES
47  
48 =head2 max_prefix_length
49  
50 The length of the prefixes to build. This should equal the max L<Game::WordBrain::WordToFind>->{num_letters}. If not provided, it defaults to 8.
51  
52 Keep in mind, the larger this value the longer the I<spin up> time needed in order to run the solver.
53  
54 =head2 word_list
55  
56 Path to a new line delimited word_list. If not provided, the wordlist provided with this distrubtion will be used.
57  
58 =head1 METHODS
59  
60 =head2 new
61  
62 my $prefix = Game::WordBrain::Prefix->new({
63 max_prefix_length => 5, # Optional
64 word_list => '/path/to/wordlist', # Optional
65 });
66  
67 If the max_prefix length is not specified it will default to 8. If no word_list is specified then the bundled wordlist will be used.
68  
69 Returns an instance of L<Game::WordBrain::Prefix>
70  
71 =cut
72  
73 10 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
74 10 my $class = shift;
75 10 my $args = shift;
76  
77 10 ▶ if( !exists $args->{max_prefix_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"max_prefix_length"}) 2 8 10
78 2 $args->{max_prefix_length} = 8;
79 }
80  
81 10 ▶ if( !exists $args->{word_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"word_list"}) 9 1 10
82 9 $args->{word_list} = 'Game::WordBrain::WordList';
83  
84 }
85  
86 10 $args->{_prefix_cache} = _load_words( $args );
87  
88 10 return bless $args, $class;
89 }
90  
91 10 ▶sub _load_words {
Subroutine _load_words: called CC=9 SCAR=22.0
92 10 my $args = shift;
93  
94 10 my $prefix_cache = { };
95  
96 # Cheaper to just copy the code for the load then create a _load_word.
97 # Saves us the stackin'
98 10 ▶ if( $args->{word_list} eq 'Game::WordBrain::WordList' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'word_list'} eq 'Game::WordBrain::WordList') { } 9 1 10
99 9 my $data_start = tell Game::WordBrain::WordList::DATA;
100  
101 9 ▶ while( my $word = <Game::WordBrain::WordList::DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline Game::WordBrain::WordList::DATA)) 3194874 9 3194883
102 3194874 chomp $word;
103  
104 3194874 ▶ for( my $length = 1; $length <= $args->{max_prefix_length}; $length++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= $args->{"max_prefix_length"}) 17825055 2880516 20705571
105 17825055 ▶ if( length( $word ) >= $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word >= $length) { } 17510697 314358 17825055
106 17510697 $prefix_cache->{ substr( $word, 0, $length ) } = 1;
107 }
108 else {
109 314358 last;
110 }
111 }
112 }
113  
114 9 seek Game::WordBrain::WordList::DATA, $data_start, 0;
115 }
116 else {
117 1 ▶ open( my $words_fh, "<", $args->{word_list} ) or die "Unable to open words file";
Branch: 1 branch
BranchTrueFalseTotal
open my $words_fh, "<", $args->{"word_list"} or die "Unable to open words file" 1 0 1
118 1 ▶ while( my $word = <$words_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline $words_fh)) 354986 1 354987
119 354986 chomp $word;
120  
121 354986 ▶ for( my $length = 1; $length <= $args->{max_prefix_length}; $length++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= $args->{"max_prefix_length"}) 1419453 352434 1771887
122 1419453 ▶ if( length( $word ) >= $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word >= $length) { } 1416901 2552 1419453
123 1416901 $prefix_cache->{ substr( $word, 0, $length ) } = 1;
124 }
125 else {
126 2552 last;
127 }
128 }
129 }
130 }
131  
132 10 return $prefix_cache;
133 }
134  
135 =head2 is_start_of_word
136  
137 my $prefix = Game::WordBrain::Prefix->...;
138 my $start_of_word = 'fla';
139  
140 if( $prefix->is_start_of_word( $start_of_word ) ) {
141 print "Could be a word...";
142 }
143 else {
144 print "Nope, no way this is going to be a word.";
145 }
146  
147 Given a string, will check to seeif there are any words in the provided word_list that start with this string. If there are ( meaning this could become a real word at some point ) a truthy value is returned. If not, a falsey value is returned.
148  
149 =cut
150  
151 6529 ▶sub is_start_of_word {
Subroutine is_start_of_word: called CC=1 SCAR=0.0
Pod documented
152 6529 my $self = shift;
153 6529 my $prefix = shift;
154  
155 6529 return exists $self->{_prefix_cache}{ substr( $prefix, 0, $self->{max_prefix_length} ) };
156 }
157  
158  
159 10 1;
↑ Summary WordBrain.pm »