line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Text::DocumentCollection;
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
1688
|
use strict;
|
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
106
|
|
5
|
3
|
|
|
3
|
|
16
|
use warnings;
|
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
86
|
|
6
|
3
|
|
|
3
|
|
1811
|
use Text::Document;
|
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
141
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
6689
|
use DB_File;
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use v5.6.0;
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$Text::DocumentCollection::VERSION = 1.02;
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new
|
15
|
|
|
|
|
|
|
{
|
16
|
|
|
|
|
|
|
my $class = shift;
|
17
|
|
|
|
|
|
|
my %self = @_;
|
18
|
|
|
|
|
|
|
my $self = \%self;
|
19
|
|
|
|
|
|
|
bless $self, $class;
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
defined( $self->{file} ) or die( __PACKAGE__ . '::new : '
|
22
|
|
|
|
|
|
|
. "keyword 'file' is mandatory"
|
23
|
|
|
|
|
|
|
);
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my %persistent;
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
(tie %persistent, 'DB_File', $self->{file}) or die( __PACKAGE__
|
28
|
|
|
|
|
|
|
. '::new : '
|
29
|
|
|
|
|
|
|
. "Cannot tie persistent hash: $!"
|
30
|
|
|
|
|
|
|
);
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$self->{pdocs} = \%persistent;
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return $self;
|
35
|
|
|
|
|
|
|
}
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub NewFromDB
|
38
|
|
|
|
|
|
|
{
|
39
|
|
|
|
|
|
|
my $self = Text::DocumentCollection::new(@_);
|
40
|
|
|
|
|
|
|
while( my @kv = each %{$self->{pdocs}} ){
|
41
|
|
|
|
|
|
|
$self->{docs}->{$kv[0]} =
|
42
|
|
|
|
|
|
|
Text::Document::NewFromString( $kv[1] );
|
43
|
|
|
|
|
|
|
}
|
44
|
|
|
|
|
|
|
return $self;
|
45
|
|
|
|
|
|
|
}
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub Add
|
48
|
|
|
|
|
|
|
{
|
49
|
|
|
|
|
|
|
my $self = shift;
|
50
|
|
|
|
|
|
|
my ($key,$doc) = @_;
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
if( defined( $self->{docs}->{$key} ) ){
|
53
|
|
|
|
|
|
|
die( __PACKAGE__ . '::Add : '
|
54
|
|
|
|
|
|
|
. "document `$key' is already in this collection"
|
55
|
|
|
|
|
|
|
);
|
56
|
|
|
|
|
|
|
}
|
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$self->{docs}->{$key} = $doc;
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
delete $self->{IDF};
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->{pdocs}->{$key} = $doc->WriteToString();
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return $doc;
|
65
|
|
|
|
|
|
|
}
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub Delete
|
68
|
|
|
|
|
|
|
{
|
69
|
|
|
|
|
|
|
my $self = shift;
|
70
|
|
|
|
|
|
|
my ($key) = @_;
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
if( not defined( $self->{docs}->{$key} ) ){
|
73
|
|
|
|
|
|
|
return undef;
|
74
|
|
|
|
|
|
|
}
|
75
|
|
|
|
|
|
|
delete $self->{docs}->{$key};
|
76
|
|
|
|
|
|
|
delete $self->{pdocs}->{$key};
|
77
|
|
|
|
|
|
|
return 1;
|
78
|
|
|
|
|
|
|
}
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub EnumerateV
|
81
|
|
|
|
|
|
|
{
|
82
|
|
|
|
|
|
|
my $self = shift;
|
83
|
|
|
|
|
|
|
my ($callback,$rock) = @_;
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my @result = ();
|
86
|
|
|
|
|
|
|
while( my @kv = each %{$self->{docs}} ){
|
87
|
|
|
|
|
|
|
my @l = &{$callback}( $self, $kv[0], $kv[1], $rock );
|
88
|
|
|
|
|
|
|
push @result, @l;
|
89
|
|
|
|
|
|
|
}
|
90
|
|
|
|
|
|
|
return @result;
|
91
|
|
|
|
|
|
|
}
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub IDF_Help
|
94
|
|
|
|
|
|
|
{
|
95
|
|
|
|
|
|
|
my $self = shift;
|
96
|
|
|
|
|
|
|
my ($key,$doc,$term) = @_;
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my $o = $doc->Occurrences( $term );
|
99
|
|
|
|
|
|
|
$self->{_idf_n}++;
|
100
|
|
|
|
|
|
|
if( $o and ($o>0) ){
|
101
|
|
|
|
|
|
|
$self->{_idf_dt}++;
|
102
|
|
|
|
|
|
|
}
|
103
|
|
|
|
|
|
|
}
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub IDF
|
106
|
|
|
|
|
|
|
{
|
107
|
|
|
|
|
|
|
my $self = shift;
|
108
|
|
|
|
|
|
|
my ($term) = @_;
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
defined( $self->{IDF}->{$term} ) and return $self->{IDF}->{$term};
|
111
|
|
|
|
|
|
|
$self->{_idf_n} = 0;
|
112
|
|
|
|
|
|
|
$self->{_idf_dt} = 0;
|
113
|
|
|
|
|
|
|
$self->EnumerateV( \&Text::DocumentCollection::IDF_Help, $term );
|
114
|
|
|
|
|
|
|
if( $self->{_idf_dt} <= 0 ){
|
115
|
|
|
|
|
|
|
warn( "term $term does not occur in any document" );
|
116
|
|
|
|
|
|
|
return $self->{IDF}->{$term} = 0.0;
|
117
|
|
|
|
|
|
|
}
|
118
|
|
|
|
|
|
|
$self->{IDF}->{$term} =
|
119
|
|
|
|
|
|
|
log( $self->{_idf_n} / $self->{_idf_dt} ) / log(2.0);
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# print "IDF($term) = $self->{IDF}->{$term}\n";
|
122
|
|
|
|
|
|
|
return $self->{IDF}->{$term} ;
|
123
|
|
|
|
|
|
|
}
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
1;
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
__END__
|