blib/lib/Text/Language/Guess.pm

filter: Statementstmt 97.8% 89 / 91 Branchbran 71.4% 10 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 17 / 17 Podpod 62.5% 5 / 8 totaltotal 90.7% 127 / 140 CC 16 SCAR 10.9
CC
16
Cov
93%
scores_string
6.1
language_guess_string
4.0
slurp
3.0
CRAP 3.0
↑ Summary
1 ###########################################
2 # Text::Language::Guess
3 # 2005, Mike Schilli <cpan@perlmeister.com>
4 ###########################################
5  
6 ###########################################
7 package Text::Language::Guess;
8 ###########################################
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars qw(%STOPMAPS $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Text::ExtractWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Lingua::StopWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 %STOPMAPS = ();
21 1 $VERSION = "0.02";
22  
23 ###########################################
24 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
25 ###########################################
26 2 my($class, @options) = @_;
27  
28 2 my $self = {
29 languages => languages(),
30 @options,
31 };
32  
33 2 bless $self, $class;
34  
35 # To avoid re-initializing the stopmap (which is fairly expensive)
36 # on every new(), hold all stopmaps for pre-computed language
37 # combinations in a class variable.
38 2 ▶ if(exists $STOPMAPS{"@{$self->{languages}}"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Text::Language::Guess::STOPMAPS{join $", @{$self->{'languages'};}}) { } 0 2 2
39 0 $self->{stopmap} = $STOPMAPS{"@{$self->{languages}}"};
40 } else {
41 2 $self->{stopmap} = $self->stopwords();
42 2 $STOPMAPS{"@{$self->{languages}}"} = $self->{stopmap};
43 }
44  
45 2 return $self;
46 }
47  
48 ###########################################
49 2 ▶sub scores {
Subroutine scores: called CC=1 SCAR=0.0
Pod documented
50 ###########################################
51 2 my($self, $file) = @_;
52  
53 2 return $self->scores_string(slurp($file));
54 }
55  
56 ###########################################
57 10 ▶sub scores_string {
Subroutine scores_string: called CC=6 SCAR=18.1
Pod documented
58 ###########################################
59 10 my($self, $data) = @_;
60  
61 10 my @words = ();
62 10 my %scores = ();
63  
64 10 ▶ LOGDIE "Cannot score empty/undefined document" if
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and length $data 0 10 10
Condition33%1/3
defined $data and length $data
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table33%1/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
65 !defined $data or !length $data;
66  
67 10 words_list(\@words, $data, {});
68
69 10 for my $word (@words) {
70 300 my $langs = $self->{stopmap}->{$word};
71
72 300 ▶ if(! defined $langs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $langs) 184 116 300
73 184 DEBUG "$word doesn't match any language";
74 184 next;
75 }
76
77 116 for my $lang (@$langs) {
78 209 DEBUG "Scoring for $lang";
79 209 $scores{$lang}++;
80 }
81 }
82  
83 10 return \%scores;
84 }
85
86 ###########################################
87 4 ▶sub language_guess {
Subroutine language_guess: called CC=1 SCAR=0.0
Pod documented
88 ###########################################
89 4 my($self, $file) = @_;
90
91 4 return $self->language_guess_string(slurp($file));
92 }
93  
94 ###########################################
95 6 ▶sub language_guess_string {
Subroutine language_guess_string: called CC=4 SCAR=13.9
Pod documented
96 ###########################################
97 6 my($self, $data) = @_;
98  
99 6 my $scores = $self->scores_string($data);
100  
101 6 my $best_lang;
102 my $max_score;
103
104 6 for my $lang (keys %$scores) {
105 33 ▶ if(!defined $max_score or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $max_score or $max_score < $scores->{$lang}) 10 23 33
Condition100%3/3
not defined $max_score or $max_score < $scores->{$lang}
l!l&&r!l&&!r
6423
MC/DC100%2/2
not defined $max_score or $max_score < $scores->{$lang}
not defined $max_score $max_score < $scores->{$lang}
Truth table100%3/3
not(A) or B
A= not defined $max_scoreB= $max_score < $scores->{$lang}
ABresult
1X1
011
000
106 $max_score < $scores->{$lang}) {
107 10 $best_lang = $lang;
108 10 $max_score = $scores->{$lang};
109 }
110 }
111
112 6 return $best_lang;
113 }
114  
115 ###########################################
116 2 ▶sub stopwords {
Subroutine stopwords: called CC=3 SCAR=11.0
Pod undocumented
117 ###########################################
118 2 my($self) = @_;
119  
120 # Fetch all stopword lists from all supported languages
121  
122 2 my $stopmap = {};
123  
124 2 for my $lang (@{$self->{languages}}) {
125
126 17 DEBUG "Loading language $lang";
127  
128 17 my $stopwords = Lingua::StopWords::getStopWords($lang);
129  
130 17 for my $stopword (keys %$stopwords) {
131 2963 DEBUG "Pushing $stopword => $lang";
132 2963 push @{$stopmap->{$stopword}}, $lang;
133 }
134 }
135  
136 2 return $stopmap;
137 }
138
139 ###########################################
140 2 ▶sub languages {
Subroutine languages: called CC=3 SCAR=11.0
Pod undocumented
141 ###########################################
142  
143 # Check which languages are supported by Lingua::StopWords
144  
145 2 for my $dir (@INC) {
146 10 ▶ if(-f File::Spec->catfile($dir, "Lingua/StopWords.pm")) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "File::Spec"->catfile($dir, "Lingua/StopWords.pm")) 2 8 10
147 2 return [map { s/\.pm$//; lc basename($_); }
148 1 ▶ <$dir/Lingua/StopWords/*.pm>];
Subroutine BEGIN: called CC=1 SCAR=0.0
149 }
150 }
151 }
152  
153 ###########################################
154 10 ▶sub slurp {
Subroutine slurp: called CC=3 SCAR=11.1
Pod undocumented
155 ###########################################
156 10 my($file) = @_;
157  
158 10 ▶ LOGDIE "$file not a file" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 10 10
159  
160 10 local $/ = undef;
161  
162 10 my $data;
163  
164 10 ▶ open FILE, "<$file" or LOGDIE "Cannot open $file ($!)";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or LOGDIE("Cannot open $file ($!)") 10 0 10
165 10 $data = <FILE>;
166 10 close FILE;
167 10 return $data;
168 }
169  
170 1 1;
171  
172 __END__
↑ Summary