line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::YahooJapan::WebMA; |
2
|
2
|
|
|
2
|
|
72258
|
use 5.008001; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
88
|
|
3
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
100
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
102
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
218
|
|
7
|
2
|
|
|
2
|
|
2086
|
use URI; |
|
2
|
|
|
|
|
23282
|
|
|
2
|
|
|
|
|
95
|
|
8
|
2
|
|
|
2
|
|
2828
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
101840
|
|
|
2
|
|
|
|
|
75
|
|
9
|
2
|
|
|
2
|
|
3439
|
use XML::Simple (); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $APIBase = 'http://api.jlp.yahoo.co.jp/MAService/V1/parse'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
|
|
|
|
|
|
my ($class, %args) = @_; |
17
|
|
|
|
|
|
|
my %self; |
18
|
|
|
|
|
|
|
$self{appid} = $args{appid} or croak 'appid is required.'; |
19
|
|
|
|
|
|
|
$self{ua} = $args{ua} || LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); |
20
|
|
|
|
|
|
|
bless \%self, $class; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub parse { |
24
|
|
|
|
|
|
|
my ($self, %args) = @_; |
25
|
|
|
|
|
|
|
croak 'sentence is required.' unless $args{sentence}; |
26
|
|
|
|
|
|
|
$self->error(''); |
27
|
|
|
|
|
|
|
my %param = map { exists $args{$_} ? ( $_ => $args{$_} ) : () } qw( |
28
|
|
|
|
|
|
|
sentence results response filter ma_response ma_filter |
29
|
|
|
|
|
|
|
uniq_response uniq_by_baseform |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
utf8::is_utf8($param{$_}) and utf8::encode($param{$_}) for keys %param; |
32
|
|
|
|
|
|
|
my $uri = URI->new($APIBase); |
33
|
|
|
|
|
|
|
$uri->query_form(appid => $self->{appid}, %param); |
34
|
|
|
|
|
|
|
my $res = $self->{ua}->get($uri); |
35
|
|
|
|
|
|
|
unless ($res->is_success) { |
36
|
|
|
|
|
|
|
my $error = 'Request failed: ' . $res->status_line; |
37
|
|
|
|
|
|
|
if ($res->content) { |
38
|
|
|
|
|
|
|
my ($message) = $res->content =~ m!(.*)!; |
39
|
|
|
|
|
|
|
$error .= "\n$message" if defined $message; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
$self->error($error); |
42
|
|
|
|
|
|
|
return; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
XML::Simple::XMLin($res->content, GroupTags => { word_list => 'word' }); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub error { |
48
|
|
|
|
|
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
if (@_) { |
50
|
|
|
|
|
|
|
$self->{error} = $_[0]; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
$self->{error}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; |
56
|
|
|
|
|
|
|
__END__ |