line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hatena::Keyword::Similar; |
2
|
4
|
|
|
4
|
|
76546
|
use warnings; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
131
|
|
3
|
4
|
|
|
4
|
|
24
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
139
|
|
4
|
4
|
|
|
4
|
|
22
|
use base qw 'Hatena::Keyword'; |
|
4
|
|
|
|
|
18
|
|
|
4
|
|
|
|
|
2624
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = 0.01; |
7
|
|
|
|
|
|
|
|
8
|
4
|
|
|
4
|
|
24
|
use Carp; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
335
|
|
9
|
4
|
|
|
4
|
|
3441
|
use RPC::XML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub similar { |
12
|
|
|
|
|
|
|
my $class = shift; |
13
|
|
|
|
|
|
|
@_ or croak sprintf 'usage %s->similar(@words)', $class; |
14
|
|
|
|
|
|
|
my $res = $class->_call_rpc_with_cache(@_); |
15
|
|
|
|
|
|
|
my @similar = map { $class->SUPER::new({ word => $_->{word}->value }) } @{$res->{wordlist}}; |
16
|
|
|
|
|
|
|
return wantarray ? @similar : \@similar; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _call_rpc_with_cache { |
20
|
|
|
|
|
|
|
my $class = shift; |
21
|
|
|
|
|
|
|
my $args = ref $_[-1] eq 'HASH' ? pop : {}; |
22
|
|
|
|
|
|
|
my @words = map {pack('C0A*', $_) }@_ ; # hacking for utf-8 flag |
23
|
|
|
|
|
|
|
my $cache = delete $args->{cache}; |
24
|
|
|
|
|
|
|
return $class->_call_rpc(@words) unless ref ($cache); |
25
|
|
|
|
|
|
|
croak "cache object must have get and set method." |
26
|
|
|
|
|
|
|
if not $cache->can('get') or not $cache->can('set'); |
27
|
|
|
|
|
|
|
require Digest::MD5; |
28
|
|
|
|
|
|
|
require Storable; |
29
|
|
|
|
|
|
|
my $key = sprintf('%s', Digest::MD5::md5_hex(@words)); |
30
|
|
|
|
|
|
|
my $res = Storable::thaw($cache->get($key)); |
31
|
|
|
|
|
|
|
unless (defined $res) { |
32
|
|
|
|
|
|
|
$res = $class->_call_rpc(@words) |
33
|
|
|
|
|
|
|
or return $class->error($class->errstr); |
34
|
|
|
|
|
|
|
$cache->set($key => Storable::freeze($res)); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
$res; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _call_rpc { |
40
|
|
|
|
|
|
|
my $class = shift; |
41
|
|
|
|
|
|
|
my $res = $class->rpc_client->send_request( |
42
|
|
|
|
|
|
|
RPC::XML::request->new('hatena.getSimilarWord', { |
43
|
|
|
|
|
|
|
wordlist => RPC::XML::array->new( |
44
|
|
|
|
|
|
|
map { RPC::XML::string->new($_) } @_, |
45
|
|
|
|
|
|
|
), |
46
|
|
|
|
|
|
|
}), |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
return ref $res ? $res : $class->error(qq/RPC Error: "$res"/); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
1; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 NAME |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Hatena::Keyword::Similar - Retrieve similarity Hatena Keywords. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 VERSION |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Version 0.01 |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
use Hatena::Keyword::Similar; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
@keywords = Hatena::Keyword::Similar->similar(qw(Perl Ruby Python)); |
66
|
|
|
|
|
|
|
print $_ for @keywords; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $cache = Cache::File->new( |
69
|
|
|
|
|
|
|
cache_root => '/path/to/cache', |
70
|
|
|
|
|
|
|
default_expires => '3600 sec', |
71
|
|
|
|
|
|
|
); |
72
|
|
|
|
|
|
|
$keywords = Hatena::Keyword::Similar->similar(qw(Perl Ruby), { |
73
|
|
|
|
|
|
|
cache => $cache, |
74
|
|
|
|
|
|
|
}); |
75
|
|
|
|
|
|
|
print $_->jcode->euc for @$keywords; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 DESCRIPTION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
This module allows you to retrieve Hatena keywords similar to given |
80
|
|
|
|
|
|
|
words with Web API. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
A Hatena keyword is an element in a suite of web sites *.hatena.ne.jp |
83
|
|
|
|
|
|
|
having blogs and social bookmarks among others. Please refer to |
84
|
|
|
|
|
|
|
http://d.hatena.ne.jp/keyword/ (in Japanese) for details. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
It queries Hatena Keyword Similarity API internally for retrieving |
87
|
|
|
|
|
|
|
terms. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 CLASS METHODS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 similar(@words, \%options) |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
Returns an array or an array reference which contains Hatena::Keyword |
94
|
|
|
|
|
|
|
objects similar to given words as argument. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This method works correctly for Japanese characters but their encoding |
97
|
|
|
|
|
|
|
must be utf-8. And also returned words are encoded as utf-8 string. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
Last argument is a optional. It can be contained a cache object, same |
100
|
|
|
|
|
|
|
as L. |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 AUTHOR |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Naoya Ito, C<< >> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 BUGS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
109
|
|
|
|
|
|
|
C, or through the web interface at |
110
|
|
|
|
|
|
|
L. |
111
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
112
|
|
|
|
|
|
|
your bug as I make changes. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SUPPORT |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
perldoc Hatena::Keyword::Similar |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
You can also look for information at: |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over 4 |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * CPAN Ratings |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
L |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * Search CPAN |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=back |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over 4 |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item L |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item Hatena Keyword Similarity API L (redirect to d.hatena.ne.jp) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item Hatena Diary L |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item Hatena L |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=back |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Copyright 2006 Naoya Ito, all rights reserved. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
161
|
|
|
|
|
|
|
under the same terms as Perl itself. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=cut |
164
|
|
|
|
|
|
|
|