WordBrain/Speller.pm

filter: Statementstmt 100% 34 / 34 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 98.0% 51 / 52 CC 6 SCAR 11.0
CC
6
Cov
98%
_load_words
5.0
new
2.0
BEGIN
1.0
CRAP 3.0
« WordBrain.pm ↑ Summary 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.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<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 11 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
58 11 my $class = shift;
59 11 my $args = shift;
60  
61 11 ▶ if( !exists $args->{word_list} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"word_list"}) 10 1 11
62 10 $args->{word_list} = 'Game::WordBrain::WordList';
63 }
64  
65 11 $args->{_words_cache} = _load_words( $args );
66  
67 11 return bless $args, $class;
68 }
69  
70 11 ▶sub _load_words {
Subroutine _load_words: called CC=5 SCAR=16.1
71 11 my $args = shift;
72  
73 11 my $words_cache = { };
74  
75 11 ▶ if( $args->{word_list} eq 'Game::WordBrain::WordList' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'word_list'} eq 'Game::WordBrain::WordList') { } 10 1 11
76 10 my $data_start = tell Game::WordBrain::WordList::DATA;
77  
78 10 ▶ while( my $word = <Game::WordBrain::WordList::DATA> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline Game::WordBrain::WordList::DATA)) 3549860 10 3549870
79 3549860 chomp $word;
80 3549860 $words_cache->{ $word } = 1;
81 }
82  
83 10 seek Game::WordBrain::WordList::DATA, $data_start, 0;
84 }
85 else {
86 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
87  
88 1 ▶ while( my $word = <$words_fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $word = readline $words_fh)) 354986 1 354987
89 354986 chomp $word;
90 354986 $words_cache->{ $word } = 1;
91 }
92  
93 1 close $words_fh;
94 }
95  
96 11 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 1288 ▶sub is_valid_word {
Subroutine is_valid_word: called CC=1 SCAR=0.0
Pod documented
115 1288 my $self = shift;
116 1288 my $word = shift;
117  
118 1288 return exists $self->{_words_cache}{$word};
119 }
120  
121 10 1;
« WordBrain.pm ↑ Summary WordBrain/Letter.pm »