blib/lib/Text/TermExtract.pm

filter: Statementstmt 93.6% 44 / 47 Branchbran 80.0% 16 / 20 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 84.0% 74 / 88 CC 15 SCAR 23.4
CC
15
Cov
86%
terms_extract
15.6
exclude
2.0
BEGIN
1.0
CRAP 10.4
↑ Summary
1 ###########################################
2 package Text::TermExtract;
3 ###########################################
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Lingua::StopWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Text::Language::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = "0.02";
12  
13 ###########################################
14 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 ###########################################
16 2 my($class, %options) = @_;
17  
18 2 my $self = {
19 languages => ['en'],
20 %options,
21 };
22  
23 2 bless $self, $class;
24 }
25  
26 ###########################################
27 1 ▶sub exclude {
Subroutine exclude: called CC=2 SCAR=6.9
Pod documented
28 ###########################################
29 1 my($self, $aref) = @_;
30  
31 1 for (@$aref) {
32 1 $self->{exclude}->{$_}++;
33 }
34 }
35  
36 ###########################################
37 3 ▶sub terms_extract {
Subroutine terms_extract: called CC=14 SCAR=27.5
Pod documented
38 ###########################################
39 3 my($self, $text, $opts) = @_;
40  
41 3 ▶ $opts = {} unless defined $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts 0 3 3
42  
43 my $guesser = Text::Language::Guess->
44 3 new(languages => $self->{languages});
45  
46 3 my $lang =
47 $guesser->language_guess_string($text);
48  
49 3 ▶ $lang = $self->{languages}->[0] unless $lang;
Branch: 1 branch
BranchTrueFalseTotal
unless $lang 1 2 3
50 3 DEBUG "Guessed language: $lang\n";
51  
52 3 my $stopwords =
53 Lingua::StopWords::getStopWords($lang);
54  
55 3 my %words;
56  
57 3 ▶ while($text =~ /\b(\w+)\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\b(\w+)\b/g) 118 3 121
58 118 my $word = lc($1);
59 118 ▶ next if $stopwords->{$word};
Branch: 1 branch
BranchTrueFalseTotal
if $stopwords->{$word} 45 73 118
60 73 ▶ next if $word =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^\d+$/ 0 73 73
61 73 ▶ next if length($word) <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $word <= 2 15 58 73
62 58 ▶ next if exists $self->{exclude}->{$word};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"exclude"}{$word} 1 57 58
63 57 $words{$word}++;
64 57 ▶ $words{$word} += 3 if length $word > 6;
Branch: 1 branch
BranchTrueFalseTotal
if length $word > 6 27 30 57
65 }
66
67 my @weighted_words = sort {
68 3 ▶ $words{$b} <=> $words{$a} or
Condition66%2/3
$words{$b} <=> $words{$a} || $a cmp $b
l!l&&r!l&&!r
511210
MC/DC0%0/2
$words{$b} <=> $words{$a} || $a cmp $b
$words{$b} <=> $words{$a} $a cmp $b
Truth table66%2/3
A || B
A= $words{$b} <=> $words{$a}B= $a cmp $b
ABresult
1X1
011
000
69 $a cmp $b # sort alphabetically on equal score
70 } keys %words;
71  
72 3 ▶ if(get_logger()->is_debug()) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_logger()->is_debug) 0 3 3
73 0 for my $word (@weighted_words) {
74 0 DEBUG "$word scores $words{$word}";
75 }
76 }
77  
78 3 ▶ if(exists $opts->{max} and $opts->{max} < @weighted_words) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'max'} and $opts->{'max'} < @weighted_words) { } 3 0 3
Condition33%1/3
exists $opts->{"max"} and $opts->{"max"} < @weighted_words
!ll&&!rl&&r
003
MC/DC0%0/2
exists $opts->{"max"} and $opts->{"max"} < @weighted_words
exists $opts->{"max"} $opts->{"max"} < @weighted_words
Truth table33%1/3
A and B
A= exists $opts->{"max"}B= $opts->{"max"} < @weighted_words
ABresult
0X0
100
111
79 3 return @weighted_words[0..($opts->{max}-1)];
80 } else {
81 0 return @weighted_words;
82 }
83 }
84  
85 2 1;
86  
87 __END__
↑ Summary