blib/lib/Lingua/EN/CMUDict.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Lingua::EN::CMUDict;
2  
3 1 ▶use 5.012003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use DB_File;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use File::ShareDir ':ALL';
8  
9 require Exporter;
10  
11 our @ISA = qw(Exporter);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 # This allows declaration use Lingua::EN::CMUDict ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 our %EXPORT_TAGS = ( 'all' => [ qw(
21
22 ) ] );
23  
24 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25  
26 our @EXPORT = qw(
27
28 );
29  
30 our $VERSION = '0.07';
31  
32  
33 # Preloaded methods go here.
34  
35 sub new {
36 my $class = shift;
37 my $self = {};
38  
39 bless $self,$class;
40  
41 my %args = @_;
42 foreach my $el (keys %args) {
43 $self->{$el} = $args{$el};
44 }
45  
46 $self->_read_cmu_file();
47 return $self;
48 }
49  
50 sub rhymes {
51 my $self = shift;
52 my $word = shift;
53 return undef if (!defined $self->{words});
54  
55 $word = uc($word);
56 return undef if (!defined $self->{words}->{$word});
57 my @syls = split(/ - /,$self->{words}->{$word});
58 my $syl = pop(@syls);
59 my @res;
60 foreach my $el (keys %{$self->{words}}) {
61 if ($self->{words}->{$el} =~ /$syl$/ && $el ne $word) {
62 push @res,$el;
63 }
64 }
65 return @res if wantarray;
66
67 return $res[int(rand(scalar(@res)))];
68 }
69  
70 sub number_of_syllables {
71 my $self = shift;
72 my $word = shift;
73  
74 return undef if (!defined $self->{words});
75 if ($word =~ /\s+/) {
76 my @words = split(/\s+/,$word);
77 my $sum = 0;
78 foreach my $el (@words) { $sum += $self->number_of_syllables($el); }
79 return $sum;
80 }
81 $word = uc($word);
82  
83 if (defined $self->{words}->{$word}) {
84 return scalar(split(/ - /,$self->{words}->{$word}));
85 }
86 #check for plurality
87 if ($word =~ /s$/i) {
88 $word =~ s/s$//i;
89 if (defined $self->{words}->{$word}) {
90 return scalar(split(/ - /,$self->{words}->{$word}));
91 }
92 }
93 return undef;
94 }
95  
96 sub get_word {
97 my $self = shift;
98  
99 my $word = shift;
100  
101 $word = uc($word);
102  
103 return $self->{words}->{$word};
104 }
105  
106 sub _read_cmu_file {
107 my $self = shift;
108  
109 my %words;
110 if (defined $self->{cmufile}) {
111 tie %words, 'DB_File', $self->{cmufile}, O_RDONLY, 0644, $DB_HASH || die $self->{cmufile}," ",$!;
112 }
113 else {
114 tie %words, 'DB_File', dist_file('Lingua-EN-CMUDict', 'cmusyldict.db'), O_RDONLY, 0644, $DB_HASH;
115 }
116  
117 $self->{words} = \%words;
118 }
119  
120 1;
121 __END__
↑ Summary