blib/lib/Game/WordBrain/Prefix.pm

filter: Statementstmt 82.9% 34 / 41 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 75.3% 52 / 69 SCAR 26.1
CC
11
Cov
72%
_load_words
16.7
new
3.0
BEGIN
1.0
CRAP 13.6
« blib/lib/Game/WordBrain.pm ↑ Summary Build »
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.0'; # 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 sub new {
74 5 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
75 5 my $args = shift;
76  
77 5 ▶ if( !exists $args->{max_prefix_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"max_prefix_length"}) 1 4 5
78 1 $args->{max_prefix_length} = 8;
79 }
80  
81 5 ▶ if( !exists $args->{word_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"word_list"}) 5 0 5
82 5 $args->{word_list} = 'Game::WordBrain::WordList';
83  
84 }
85  
86 5 $args->{_prefix_cache} = _load_words( $args );
87  
88 5 return bless $args, $class;
89 }
90  
91 sub _load_words {
92 5 ▶ my $args = shift;
Subroutine _load_words: called CC=9 SCAR=28.2
93  
94 5 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 5 ▶ if( $args->{word_list} eq 'Game::WordBrain::WordList' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'word_list'} eq 'Game::WordBrain::WordList') { } 5 0 5
99 5 my $data_start = tell Game::WordBrain::WordList::DATA;
100  
101 5 ▶ while( my $word = <Game::WordBrain::WordList::DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline Game::WordBrain::WordList::DATA)) 1774930 5 1774935
102 1774930 chomp $word;
103  
104 1774930 ▶ for( my $length = 1; $length <= $args->{max_prefix_length}; $length++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= $args->{"max_prefix_length"}) 9485459 1651524 11136983
105 9485459 ▶ if( length( $word ) >= $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word >= $length) { } 9362053 123406 9485459
106 9362053 $prefix_cache->{ substr( $word, 0, $length ) } = 1;
107 }
108 else {
109 123406 last;
110 }
111 }
112 }
113  
114 5 seek Game::WordBrain::WordList::DATA, $data_start, 0;
115 }
116 else {
117 0 ▶ 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" 0 0 0
118 0 ▶ while( my $word = <$words_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline $words_fh)) 0 0 0
119 0 chomp $word;
120  
121 0 ▶ for( my $length = 1; $length <= $args->{max_prefix_length}; $length++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length <= $args->{"max_prefix_length"}) 0 0 0
122 0 ▶ if( length( $word ) >= $length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word >= $length) { } 0 0 0
123 0 $prefix_cache->{ substr( $word, 0, $length ) } = 1;
124 }
125 else {
126 0 last;
127 }
128 }
129 }
130 }
131  
132 5 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 sub is_start_of_word {
152 6491 ▶ my $self = shift;
Subroutine is_start_of_word: called CC=1 SCAR=0.0
Pod documented
153 6491 my $prefix = shift;
154  
155 6491 return exists $self->{_prefix_cache}{ substr( $prefix, 0, $self->{max_prefix_length} ) };
156 }
157  
158  
159 10 1;
« blib/lib/Game/WordBrain.pm ↑ Summary Build »