line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Search::Mousse::Writer::Related; |
2
|
2
|
|
|
2
|
|
3597
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
107
|
|
3
|
2
|
|
|
2
|
|
10
|
use base qw(Class::Accessor::Chained::Fast); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
235
|
|
4
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors( |
5
|
|
|
|
|
|
|
qw(mousse id_to_related size) |
6
|
|
|
|
|
|
|
); |
7
|
2
|
|
|
2
|
|
13
|
use CDB_File; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
96
|
|
8
|
2
|
|
|
2
|
|
11
|
use CDB_File_Thawed; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
71
|
|
9
|
2
|
|
|
2
|
|
11
|
use File::Temp qw/ :POSIX /; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
279
|
|
10
|
2
|
|
|
2
|
|
11
|
use List::Uniq qw(uniq); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
106
|
|
11
|
2
|
|
|
2
|
|
10
|
use Path::Class; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
103
|
|
12
|
2
|
|
|
2
|
|
1071
|
use Search::ContextGraph; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
|
|
|
|
|
|
my $class = shift; |
16
|
|
|
|
|
|
|
my %args = @_; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $self = {}; |
19
|
|
|
|
|
|
|
bless $self, $class; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
my $mousse = $args{mousse} || die "No mousse passed"; |
22
|
|
|
|
|
|
|
$self->mousse($mousse); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $name = $mousse->name; |
25
|
|
|
|
|
|
|
my $directory = $mousse->directory; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $filename = file($directory, "${name}_id_to_related.cdb"); |
28
|
|
|
|
|
|
|
my $tempfile = tmpnam(); |
29
|
|
|
|
|
|
|
$self->id_to_related(CDB_File_Thawed->new($filename, $tempfile)) or die $!; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$self->size($args{size} || 20); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
return $self; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub write { |
37
|
|
|
|
|
|
|
my $self = shift; |
38
|
|
|
|
|
|
|
my $mousse = $self->mousse; |
39
|
|
|
|
|
|
|
my $id_to_related = $self->id_to_related; |
40
|
|
|
|
|
|
|
my $size = $self->size; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $cg = Search::ContextGraph->new(auto_reweight => 0); |
43
|
|
|
|
|
|
|
my %docs; |
44
|
|
|
|
|
|
|
while (my ($word, $ids) = each %{ $mousse->word_to_id }) { |
45
|
|
|
|
|
|
|
foreach my $id (@$ids) { |
46
|
|
|
|
|
|
|
push @{ $docs{$id} }, $word; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
$cg->bulk_add(%docs); |
50
|
|
|
|
|
|
|
$cg->reweight_graph(); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
while (my ($id, $key) = each %{ $mousse->id_to_key }) { |
53
|
|
|
|
|
|
|
my @ids; |
54
|
|
|
|
|
|
|
eval { |
55
|
|
|
|
|
|
|
local $SIG{ALRM} = sub { die "alarm\n" }; |
56
|
|
|
|
|
|
|
alarm 1; |
57
|
|
|
|
|
|
|
my ($docs, $words) = $cg->find_similar($id); |
58
|
|
|
|
|
|
|
@ids = (sort { $docs->{$b} <=> $docs->{$a} } keys %$docs); |
59
|
|
|
|
|
|
|
@ids = grep { $_ ne $id } @ids; |
60
|
|
|
|
|
|
|
@ids = splice(@ids, 0, $size); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
my @keys = map { $mousse->id_to_key->{$_} } @ids; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# print "$key -> @keys\n"; |
65
|
|
|
|
|
|
|
alarm 0; |
66
|
|
|
|
|
|
|
}; |
67
|
|
|
|
|
|
|
$id_to_related->insert($id, \@ids); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
$id_to_related->finish; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
__END__ |