line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package PLN::PT; |
2
|
|
|
|
|
|
|
# ABSTRACT: interface for the http://pln.pt web service |
3
|
|
|
|
|
|
|
$PLN::PT::VERSION = '0.006'; |
4
|
2
|
|
|
2
|
|
99543
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
64
|
|
5
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
648
|
use JSON::MaybeXS (); |
|
2
|
|
|
|
|
14176
|
|
|
2
|
|
|
|
|
56
|
|
8
|
2
|
|
|
2
|
|
899
|
use CHI; |
|
2
|
|
|
|
|
143166
|
|
|
2
|
|
|
|
|
97
|
|
9
|
2
|
|
|
2
|
|
20
|
use Digest::MD5 qw/md5_base64/; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
158
|
|
10
|
2
|
|
|
2
|
|
1216
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
72682
|
|
|
2
|
|
|
|
|
78
|
|
11
|
2
|
|
|
2
|
|
939
|
use Encode; |
|
2
|
|
|
|
|
15255
|
|
|
2
|
|
|
|
|
1598
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { |
14
|
1
|
|
|
1
|
1
|
85
|
my ($class, $url) = @_; |
15
|
1
|
|
|
|
|
5
|
my $self = bless( {url=>$url}, $class); |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
11
|
$self->{ua} = LWP::UserAgent->new; |
18
|
1
|
|
|
|
|
2499
|
$self->{cache} = CHI->new( driver => 'Memory', global => 1 ); |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
85373
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub tokenizer { |
24
|
1
|
|
|
1
|
1
|
12
|
my ($self, $text, $opts) = @_; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
|
|
6
|
my $url = $self->_cat('tokenizer'); |
27
|
1
|
|
|
|
|
5
|
$url .= '?' . $self->_args($opts); |
28
|
|
|
|
|
|
|
|
29
|
1
|
|
|
|
|
6
|
return $self->_post($url, $text, $opts); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub morph_analyzer { |
33
|
1
|
|
|
1
|
1
|
1043
|
my ($self, $word, $opts) = @_; |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
3
|
$word =~ s/\// /g; # make it sane, if someone tries to go guessing |
36
|
|
|
|
|
|
|
|
37
|
1
|
|
|
|
|
4
|
my $url = $self->_cat('morph', $word); |
38
|
1
|
|
|
|
|
20
|
$url .= '?' . $self->_args($opts); |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
|
|
4
|
return $self->_get($url, $opts); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub tagger { |
44
|
1
|
|
|
1
|
1
|
1586
|
my ($self, $text, $opts) = @_; |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
4
|
my $url = $self->_cat('tagger'); |
47
|
1
|
|
|
|
|
4
|
$url .= '?' . $self->_args($opts); |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
3
|
return $self->_post($url, $text, $opts); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub dep_parser { |
53
|
1
|
|
|
1
|
1
|
1463
|
my ($self, $text, $opts) = @_; |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
4
|
my $url = $self->_cat('dep_parser'); |
56
|
1
|
|
|
|
|
4
|
$url .= '?' . $self->_args($opts); |
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
4
|
return $self->_post($url, $text, $opts); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub tf { |
62
|
0
|
|
|
0
|
1
|
0
|
my ($self, $text, $opts) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
my $url = $self->_cat('tf'); |
65
|
0
|
|
|
|
|
0
|
$url .= '?' . $self->_args($opts); |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
0
|
return $self->_post($url, $text, $opts); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub stopwords { |
71
|
0
|
|
|
0
|
1
|
0
|
my ($self, $opts) = @_; |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
my $url = $self->_cat('stopwords'); |
74
|
0
|
|
|
|
|
0
|
$url .= '?' . $self->_args($opts); |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
return $self->_get($url, $opts); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _post { |
80
|
3
|
|
|
3
|
|
8
|
my ($self, $url, $text, $opts) = @_; |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
18
|
my $key = $url . '-' . md5_base64(Encode::encode_utf8($text)); |
83
|
3
|
|
|
|
|
59
|
my $data = $self->{cache}->get($key); |
84
|
|
|
|
|
|
|
|
85
|
3
|
50
|
|
|
|
288
|
unless ($data) { |
86
|
3
|
|
|
|
|
25
|
my $req = HTTP::Request->new(POST => $url); |
87
|
3
|
|
|
|
|
6425
|
$req->content(Encode::encode_utf8($text)); |
88
|
|
|
|
|
|
|
|
89
|
3
|
|
|
|
|
135
|
my $res = $self->{ua}->request($req); |
90
|
3
|
100
|
|
|
|
6280455
|
if ($res->is_success) { |
91
|
2
|
|
|
|
|
28
|
$data = $res->decoded_content; |
92
|
2
|
50
|
|
|
|
285
|
$data = $res->content unless $data; |
93
|
2
|
|
|
|
|
41
|
$self->{cache}->set($key, $data); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
else { |
96
|
1
|
|
|
|
|
11
|
print STDERR "HTTP POST error: ", $res->code, " - ", $res->message, "\n"; |
97
|
1
|
|
|
|
|
77
|
return undef; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
2
|
50
|
33
|
|
|
585
|
return $data if ($opts->{output} and $opts->{output} eq 'raw'); |
102
|
2
|
|
|
|
|
20
|
return JSON::MaybeXS->new(utf8 => 1)->decode($data); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub _get { |
106
|
1
|
|
|
1
|
|
3
|
my ($self, $url, $opts) = @_; |
107
|
|
|
|
|
|
|
|
108
|
1
|
|
|
|
|
10
|
my $key = $url . '-' . md5_base64(join('', values %$opts)); |
109
|
1
|
|
|
|
|
9
|
my $data = $self->{cache}->get($key); |
110
|
|
|
|
|
|
|
|
111
|
1
|
50
|
|
|
|
84
|
unless ($data) { |
112
|
1
|
|
|
|
|
15
|
my $req = HTTP::Request->new(GET => $url); |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
169
|
my $res = $self->{ua}->request($req); |
115
|
1
|
50
|
|
|
|
84524
|
if ($res->is_success) { |
116
|
0
|
|
|
|
|
0
|
$data = $res->decoded_content; |
117
|
0
|
0
|
|
|
|
0
|
$data = $res->content unless $data; |
118
|
0
|
|
|
|
|
0
|
$self->{cache}->set($key, $data); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
1
|
|
|
|
|
14
|
print STDERR "HTTP GET error: ", $res->code, " - ", $res->message, "\n"; |
122
|
1
|
|
|
|
|
88
|
return undef; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
0
|
0
|
0
|
|
|
0
|
return $data if ($opts->{output} and $opts->{output} eq 'raw'); |
127
|
0
|
|
|
|
|
0
|
return JSON::MaybeXS->new(utf8 => 1)->decode($data); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub _cat { |
131
|
4
|
|
|
4
|
|
15
|
my ($self, @args) = @_; |
132
|
|
|
|
|
|
|
|
133
|
4
|
|
|
|
|
11
|
my @parts = ($self->{url}); |
134
|
4
|
|
|
|
|
12
|
push @parts, @args; |
135
|
|
|
|
|
|
|
|
136
|
4
|
|
|
|
|
17
|
return join('/', @parts); |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub _args { |
140
|
4
|
|
|
4
|
|
11
|
my ($self, $opts) = @_; |
141
|
|
|
|
|
|
|
|
142
|
4
|
|
|
|
|
6
|
my @args; |
143
|
4
|
|
|
|
|
15
|
foreach (keys %$opts) { |
144
|
0
|
|
|
|
|
0
|
push @args, join('=', $_, $opts->{$_}); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
4
|
|
|
|
|
17
|
return join('&', @args); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
__END__ |