blib/lib/Game/WordBrain/Speller.pm

filter: Statementstmt 85.2% 29 / 34 Branchbran 40.0% 4 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 78.8% 41 / 52 SCAR 18.8
CC
6
Cov
75%
_load_words
7.3
new
2.0
BEGIN
1.0
CRAP 6.6
« Build ↑ Summary blib/lib/Game/WordBrain/Letter.pm »
1 package Game::WordBrain::Speller;
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.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<Game::WordBrain> made use of L<Text::Aspell> as a speller. The problem was that L<Text:Aspell> provided much more functionalty (and many more dependencies) then what L<Game::WordBrain> 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<Game::WordBrain::Speller>.
54  
55 =cut
56  
57 sub new {
58 4 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
59 4 my $args = shift;
60  
61 4 ▶ if( !exists $args->{word_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"word_list"}) 4 0 4
62 4 $args->{word_list} = 'Game::WordBrain::WordList';
63 }
64  
65 4 $args->{_words_cache} = _load_words( $args );
66  
67 4 return bless $args, $class;
68 }
69  
70 sub _load_words {
71 4 ▶ my $args = shift;
Subroutine _load_words: called CC=5 SCAR=19.9
72  
73 4 my $words_cache = { };
74  
75 4 ▶ if( $args->{word_list} eq 'Game::WordBrain::WordList' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'word_list'} eq 'Game::WordBrain::WordList') { } 4 0 4
76 4 my $data_start = tell Game::WordBrain::WordList::DATA;
77  
78 4 ▶ while( my $word = <Game::WordBrain::WordList::DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline Game::WordBrain::WordList::DATA)) 1419944 4 1419948
79 1419944 chomp $word;
80 1419944 $words_cache->{ $word } = 1;
81 }
82  
83 4 seek Game::WordBrain::WordList::DATA, $data_start, 0;
84 }
85 else {
86 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
87  
88 0 ▶ while( my $word = <$words_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline $words_fh)) 0 0 0
89 0 chomp $word;
90 0 $words_cache->{ $word } = 1;
91 }
92  
93 0 close $words_fh;
94 }
95  
96 4 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 1286 ▶ my $self = shift;
Subroutine is_valid_word: called CC=1 SCAR=0.0
Pod documented
116 1286 my $word = shift;
117  
118 1286 return exists $self->{_words_cache}{$word};
119 }
120  
121 10 1;
« Build ↑ Summary blib/lib/Game/WordBrain/Letter.pm »