| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | 118 | my $word = lc($1); | |||||||||||||||||||||||||||
| 59 | 118 | ▶ | next if $stopwords->{$word}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 73 | ▶ | next if $word =~ /^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 73 | ▶ | next if length($word) <= 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 58 | ▶ | next if exists $self->{exclude}->{$word}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 57 | $words{$word}++; | |||||||||||||||||||||||||||
| 64 | 57 | ▶ | $words{$word} += 3 if length $word > 6; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my @weighted_words = sort { | ||||||||||||||||||||||||||||
| 68 | 3 | ▶ | $words{$b} <=> $words{$a} or | ||||||||||||||||||||||||||
|
Condition66%2/3
$words{$b} <=> $words{$a} || $a cmp $b
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
| |||||||||||||||||||||||||||||
| 69 | $a cmp $b # sort alphabetically on equal score | ||||||||||||||||||||||||||||
| 70 | } keys %words; | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if(get_logger()->is_debug()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $opts->{"max"} and $opts->{"max"} < @weighted_words
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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||