line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################### |
2
|
|
|
|
|
|
|
package Text::TermExtract; |
3
|
|
|
|
|
|
|
########################################### |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
48487
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
6
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
55
|
|
7
|
2
|
|
|
2
|
|
1744
|
use Lingua::StopWords; |
|
2
|
|
|
|
|
690
|
|
|
2
|
|
|
|
|
101
|
|
8
|
2
|
|
|
2
|
|
1958
|
use Text::Language::Guess; |
|
2
|
|
|
|
|
141018
|
|
|
2
|
|
|
|
|
68
|
|
9
|
2
|
|
|
2
|
|
18
|
use Log::Log4perl qw(:easy); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
13
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
########################################### |
14
|
|
|
|
|
|
|
sub new { |
15
|
|
|
|
|
|
|
########################################### |
16
|
2
|
|
|
2
|
1
|
365
|
my($class, %options) = @_; |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
|
|
9
|
my $self = { |
19
|
|
|
|
|
|
|
languages => ['en'], |
20
|
|
|
|
|
|
|
%options, |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
10
|
bless $self, $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
########################################### |
27
|
|
|
|
|
|
|
sub exclude { |
28
|
|
|
|
|
|
|
########################################### |
29
|
1
|
|
|
1
|
1
|
1107
|
my($self, $aref) = @_; |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
4
|
for (@$aref) { |
32
|
1
|
|
|
|
|
5
|
$self->{exclude}->{$_}++; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
########################################### |
37
|
|
|
|
|
|
|
sub terms_extract { |
38
|
|
|
|
|
|
|
########################################### |
39
|
3
|
|
|
3
|
1
|
19
|
my($self, $text, $opts) = @_; |
40
|
|
|
|
|
|
|
|
41
|
3
|
50
|
|
|
|
11
|
$opts = {} unless defined $opts; |
42
|
|
|
|
|
|
|
|
43
|
3
|
|
|
|
|
31
|
my $guesser = Text::Language::Guess-> |
44
|
|
|
|
|
|
|
new(languages => $self->{languages}); |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
16215
|
my $lang = |
47
|
|
|
|
|
|
|
$guesser->language_guess_string($text); |
48
|
|
|
|
|
|
|
|
49
|
3
|
100
|
|
|
|
950
|
$lang = $self->{languages}->[0] unless $lang; |
50
|
3
|
|
|
|
|
16
|
DEBUG "Guessed language: $lang\n"; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
27
|
my $stopwords = |
53
|
|
|
|
|
|
|
Lingua::StopWords::getStopWords($lang); |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
649
|
my %words; |
56
|
|
|
|
|
|
|
|
57
|
3
|
|
|
|
|
28
|
while($text =~ /\b(\w+)\b/g) { |
58
|
118
|
|
|
|
|
157
|
my $word = lc($1); |
59
|
118
|
100
|
|
|
|
265
|
next if $stopwords->{$word}; |
60
|
73
|
50
|
|
|
|
142
|
next if $word =~ /^\d+$/; |
61
|
73
|
100
|
|
|
|
142
|
next if length($word) <= 2; |
62
|
58
|
100
|
|
|
|
106
|
next if exists $self->{exclude}->{$word}; |
63
|
57
|
|
|
|
|
92
|
$words{$word}++; |
64
|
57
|
100
|
|
|
|
226
|
$words{$word} += 3 if length $word > 6; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
177
|
50
|
|
|
|
298
|
my @weighted_words = sort { |
68
|
3
|
|
|
|
|
27
|
$words{$b} <=> $words{$a} or |
69
|
|
|
|
|
|
|
$a cmp $b # sort alphabetically on equal score |
70
|
|
|
|
|
|
|
} keys %words; |
71
|
|
|
|
|
|
|
|
72
|
3
|
50
|
|
|
|
15
|
if(get_logger()->is_debug()) { |
73
|
0
|
|
|
|
|
0
|
for my $word (@weighted_words) { |
74
|
0
|
|
|
|
|
0
|
DEBUG "$word scores $words{$word}"; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
3
|
50
|
33
|
|
|
144
|
if(exists $opts->{max} and $opts->{max} < @weighted_words) { |
79
|
3
|
|
|
|
|
80
|
return @weighted_words[0..($opts->{max}-1)]; |
80
|
|
|
|
|
|
|
} else { |
81
|
0
|
|
|
|
|
|
return @weighted_words; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
1; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
__END__ |