blib/lib/Text/TFIDF.pm

filter: Statementstmt 100% 68 / 68 Branchbran 81.2% 13 / 16 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 10 / 10 Podpod 100% 5 / 5 totaltotal 95.1% 97 / 102 CC 13 SCAR 12.2
CC
13
Cov
95%
_process_file
6.0
TFIDF
4.0
IDF
3.0
CRAP 3.4
↑ Summary
1 package Text::TFIDF;
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 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require Encode;
8  
9 1 our $VERSION = '0.04';
10  
11  
12 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
13 2 my $class = shift;
14 2 my $self = {};
15  
16 2 bless $self, $class;
17  
18 2 my %args = @_;
19  
20 2 ▶ if ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"file"}) 1 1 2
21 1 $self->process_files(@{$args{file}});
22 }
23  
24 2 return $self;
25 }
26  
27 5 ▶sub TFIDF {
Subroutine TFIDF: called CC=4 SCAR=13.9
Pod documented
28 5 my $self = shift;
29 5 my $file = shift;
30 5 my $word = shift;
31  
32 5 ▶ if (!defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 4 5
33 1 carp("You must give a filename for the TFIDF measure.\n");
34 1 return undef;
35 }
36  
37 4 ▶ if (!defined $word) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 1 3 4
38 1 carp("You must give a word for the TFIDF measure.\n");
39 1 return undef;
40 }
41  
42 # $word =~ s/[?;:!,.'"]//g;
43 3 $word =~ s/[?;:!,."\(\)]//g;
44 3 ▶ return undef if (!defined $self->{file}->{$file});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"}{$file} 0 3 3
45 3 return $self->TF($file,$word)*$self->IDF($word);
46 }
47  
48  
49 4 ▶sub TF {
Subroutine TF: called CC=1 SCAR=0.0
Pod documented
50 4 my $self = shift;
51 4 my $file = shift;
52 4 my $word = shift;
53  
54 4 return $self->{file}->{$file}->{$word};
55 }
56  
57  
58 #IDF = log(number of documents/(number of documents containing the word))
59 4 ▶sub IDF {
Subroutine IDF: called CC=3 SCAR=11.0
Pod documented
60  
61 4 my $self = shift;
62 4 my $word = shift;
63  
64 4 my $count = 0;
65  
66 4 foreach my $el (keys %{$self->{file}}) {
67 8 ▶ $count++ if (defined $self->{file}->{$el}->{$word});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"file"}{$el}{$word} 8 0 8
68 }
69  
70 4 return log(scalar(keys %{$self->{file}})/($count))/log(10);
71 }
72  
73 2 ▶sub process_files {
Subroutine process_files: called CC=2 SCAR=6.9
Pod documented
74  
75 2 my $self = shift;
76 2 my @documents = @_;
77  
78 2 foreach my $el (@documents) {
79 4 $self->_process_file($el);
80 }
81  
82 2 return 1;
83 }
84  
85 4 ▶sub _process_file {
Subroutine _process_file: called CC=6 SCAR=17.9
86 4 my $self = shift;
87 4 my $file = shift;
88  
89 4 my $hash;
90 4 ▶ return undef if (!-r $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 4 4
91 4 ▶ open my $handle, '<:encoding(UTF-8)', $file || die $file," ",$!;
Condition50%1/2
$file || die($file, ' ', $!)
l!l
40
MC/DC0%0/1
$file || die($file, ' ', $!)
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
92 4 ▶ while (<$handle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 138 4 142
93 138 chop;
94 138 my $line = lc($_);
95 138 my @words = split(/\s+/,$line);
96 138 foreach my $el (@words) {
97 1016 $el =~ s/[?;:!,."\(\)]//g;
98 1016 my $word = Encode::encode("utf8",$el);
99 1016 ▶ if (defined $hash->{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash->{$word}) { } 464 552 1016
100 464 $hash->{$word}++;
101 }
102 else {
103 552 $hash->{$word} = 1;
104 }
105 }
106 }
107 4 close($handle);
108
109 4 $self->{file}->{$file} = $hash;
110  
111 }
112  
113 1 1;
114 __END__
↑ Summary