File Coverage

blib/lib/Text/TFIDF.pm
Criterion Covered Total %
statement 64 64 100.0
branch 12 16 75.0
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 91 95 95.7


line stmt bran cond sub pod time code
1             package Text::TFIDF;
2              
3 1     1   26328 use 5.012003;
  1         4  
  1         37  
4 1     1   6 use strict;
  1         2  
  1         42  
5 1     1   5 use warnings;
  1         6  
  1         35  
6 1     1   5 use Carp;
  1         1  
  1         786  
7              
8              
9             our $VERSION = '0.02';
10              
11              
12             sub new {
13 2     2 1 13 my $class = shift;
14 2         5 my $self = {};
15              
16 2         6 bless $self, $class;
17              
18 2         6 my %args = @_;
19              
20 2 100       8 if ($args{file}) {
21 1         2 $self->process_files(@{$args{file}});
  1         4  
22             }
23              
24 2         341 return $self;
25             }
26              
27             sub TFIDF {
28 5     5 1 969 my $self = shift;
29 5         8 my $file = shift;
30 5         6 my $word = shift;
31              
32 5 100       14 if (!defined $file) {
33 1         639 carp("You must give a filename for the TFIDF measure.\n");
34 1         157 return undef;
35             }
36              
37 4 100       9 if (!defined $word) {
38 1         105 carp("You must give a word for the TFIDF measure.\n");
39 1         353 return undef;
40             }
41              
42 3 50       13 return undef if (!defined $self->{file}->{$file});
43 3         8 return $self->TF($file,$word)*$self->IDF($word);
44             }
45              
46              
47             sub TF {
48 4     4 1 7 my $self = shift;
49 4         4 my $file = shift;
50 4         5 my $word = shift;
51              
52 4         18 return $self->{file}->{$file}->{$word};
53             }
54              
55              
56             #IDF = log(number of documents/(number of documents containing the word))
57             sub IDF {
58              
59 4     4 1 6 my $self = shift;
60 4         6 my $word = shift;
61              
62 4         6 my $count = 0;
63              
64 4         5 foreach my $el (keys %{$self->{file}}) {
  4         13  
65 8 50       101 $count++ if (defined $self->{file}->{$el}->{$word});
66             }
67              
68 4         7 return log(scalar(keys %{$self->{file}})/(1+$count))/log(10);
  4         184  
69             }
70              
71             sub process_files {
72              
73 2     2 1 897 my $self = shift;
74 2         5 my @documents = @_;
75              
76 2         4 foreach my $el (@documents) {
77 4         13 $self->_process_file($el);
78             }
79              
80 2         13 return 1;
81             }
82              
83             sub _process_file {
84 4     4   5 my $self = shift;
85 4         5 my $file = shift;
86              
87 4         6 my $hash;
88 4 50       89 return undef if (!-r $file);
89 4 50       569 open(IF,$file) || die $file," ",$!;
90              
91 4         68 while () {
92 138         626 chop;
93 138         976 my $line = lc($_);
94 138         1546 my @words = split(/\s+/,$line);
95 138         934 foreach my $el (@words) {
96 1016         4448 $el =~ s/[?;:!,.'"]//g;
97 1016 100       4629 if (defined $hash->{$el}) {
98 458         1726 $hash->{$el}++;
99             }
100             else {
101 558         4742 $hash->{$el} = 1;
102             }
103             }
104             }
105 4         46 close(IF);
106            
107 4         25 $self->{file}->{$file} = $hash;
108              
109             }
110              
111             1;
112             __END__