line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: FindConcept.pm,v 1.7 2004/01/06 07:40:30 cvspub Exp $ |
2
|
|
|
|
|
|
|
package WWW::FindConcept; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
19217
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
465
|
use WWW::FindConcept::Sources; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
7
|
1
|
|
|
1
|
|
1159
|
use WWW::Mechanize; |
|
1
|
|
|
|
|
236816
|
|
|
1
|
|
|
|
|
36
|
|
8
|
1
|
|
|
1
|
|
22872
|
use Data::Dumper; |
|
1
|
|
|
|
|
10490
|
|
|
1
|
|
|
|
|
109
|
|
9
|
1
|
|
|
1
|
|
11
|
use Exporter; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
321
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
12
|
|
|
|
|
|
|
our @EXPORT = qw(find_concept update_concept delete_concept dump_cache remove_cache); |
13
|
|
|
|
|
|
|
our @EXPORT_FAIL = qw(extract get_concept); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our $cachepath = $ENV{HOME}."/.find-concept"; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub extract { |
20
|
0
|
|
|
0
|
|
|
my ($pattern, $text, $concept) = @_; |
21
|
|
|
|
|
|
|
# print $pattern.$/; |
22
|
0
|
|
|
|
|
|
while($text =~ /$pattern/g){ |
23
|
0
|
|
|
|
|
|
$concept->{$1} = 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub get_concept { |
28
|
0
|
0
|
|
0
|
|
|
die unless caller eq __PACKAGE__; |
29
|
0
|
|
|
|
|
|
my ($url, $template) = @{shift()}; |
|
0
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my ($query) = shift; |
31
|
0
|
|
|
|
|
|
my ($concept) = shift; |
32
|
0
|
|
|
|
|
|
$url =~ s/\Q{%query%}\E/$query/o; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
my $a = WWW::Mechanize->new( |
35
|
|
|
|
|
|
|
env_proxy => 1, |
36
|
|
|
|
|
|
|
timeout => 10, |
37
|
|
|
|
|
|
|
); |
38
|
0
|
|
|
|
|
|
$a->agent_alias( 'Windows IE 6' ); |
39
|
0
|
|
|
|
|
|
$a->get( $url ); |
40
|
0
|
0
|
|
|
|
|
if($a->success){ |
41
|
0
|
|
|
|
|
|
extract($template, $a->content, $concept); |
42
|
|
|
|
|
|
|
# print $template.$/; |
43
|
|
|
|
|
|
|
# print Dumper $concept; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
1
|
|
2299
|
use DB_File; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
use Storable qw(freeze thaw); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub delete_concept($) { |
51
|
|
|
|
|
|
|
tie my %cache, 'DB_File', $cachepath, O_CREAT | O_RDWR, 0644, $DB_BTREE |
52
|
|
|
|
|
|
|
or die "cannot open $cachepath"; |
53
|
|
|
|
|
|
|
delete $cache{$_[0]}; |
54
|
|
|
|
|
|
|
untie %cache; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub find_concept($) { |
58
|
|
|
|
|
|
|
my $query = shift; |
59
|
|
|
|
|
|
|
my %concept; |
60
|
|
|
|
|
|
|
tie my %cache, 'DB_File', $cachepath, O_CREAT | O_RDWR, 0644, $DB_BTREE |
61
|
|
|
|
|
|
|
or die "cannot open $cachepath"; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
if($cache{$query}){ |
64
|
|
|
|
|
|
|
%concept = %{ thaw($cache{$query}) }; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
else{ |
67
|
|
|
|
|
|
|
foreach my $src ( keys %WWW::FindConcept::Sources::source ){ |
68
|
|
|
|
|
|
|
# next if $WWW::FindConcept::Sources::source{$src}->[-1] eq 'to_skip'; |
69
|
|
|
|
|
|
|
get_concept($WWW::FindConcept::Sources::source{$src}, $query, \%concept); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
$cache{$query} = freeze \%concept; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
untie %cache; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
keys %concept; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub dump_cache(){ |
79
|
|
|
|
|
|
|
tie my %cache, 'DB_File', $cachepath, O_CREAT | O_RDWR, 0644, $DB_BTREE |
80
|
|
|
|
|
|
|
or die "cannot open $cachepath"; |
81
|
|
|
|
|
|
|
my @c = keys %cache; |
82
|
|
|
|
|
|
|
untie %cache; |
83
|
|
|
|
|
|
|
return @c; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub update_concept($) { |
87
|
|
|
|
|
|
|
delete_concept($_[0]); |
88
|
|
|
|
|
|
|
find_concept($_[0]); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub remove_cache { |
92
|
|
|
|
|
|
|
unlink $cachepath; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |