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.007'; |
4
|
5
|
|
|
5
|
|
250997
|
use strict; |
|
5
|
|
|
|
|
49
|
|
|
5
|
|
|
|
|
138
|
|
5
|
5
|
|
|
5
|
|
25
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
128
|
|
6
|
|
|
|
|
|
|
|
7
|
5
|
|
|
5
|
|
2285
|
use JSON::XS; |
|
5
|
|
|
|
|
27633
|
|
|
5
|
|
|
|
|
296
|
|
8
|
5
|
|
|
5
|
|
1947
|
use CHI; |
|
5
|
|
|
|
|
319190
|
|
|
5
|
|
|
|
|
162
|
|
9
|
5
|
|
|
5
|
|
36
|
use Digest::MD5 qw/md5_base64/; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
297
|
|
10
|
5
|
|
|
5
|
|
2167
|
use LWP::UserAgent; |
|
5
|
|
|
|
|
182144
|
|
|
5
|
|
|
|
|
188
|
|
11
|
5
|
|
|
5
|
|
2263
|
use Encode; |
|
5
|
|
|
|
|
39894
|
|
|
5
|
|
|
|
|
387
|
|
12
|
5
|
|
|
5
|
|
1465
|
use utf8::all; |
|
5
|
|
|
|
|
141828
|
|
|
5
|
|
|
|
|
29
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
4
|
|
|
4
|
1
|
369
|
my ($class, $url) = @_; |
16
|
4
|
|
|
|
|
17
|
my $self = bless( {url=>$url}, $class); |
17
|
|
|
|
|
|
|
|
18
|
4
|
|
|
|
|
38
|
$self->{ua} = LWP::UserAgent->new; |
19
|
4
|
|
|
|
|
8302
|
$self->{cache} = CHI->new( driver => 'Memory', global => 1 ); |
20
|
|
|
|
|
|
|
|
21
|
4
|
|
|
|
|
321567
|
return $self; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub tokenizer { |
25
|
1
|
|
|
1
|
1
|
9
|
my ($self, $text, $opts) = @_; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
|
|
19
|
my $url = $self->_cat('tokenizer'); |
28
|
1
|
|
|
|
|
9
|
$url .= '?' . $self->_args($opts); |
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
|
|
5
|
return $self->_post($url, $text, $opts); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub morph_analyzer { |
34
|
1
|
|
|
1
|
1
|
10
|
my ($self, $word, $opts) = @_; |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
16
|
$word =~ s/\// /g; # make it sane, if someone tries to go guessing |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
6
|
my $url = $self->_cat('morph_analyzer', $word); |
39
|
1
|
|
|
|
|
4
|
$url .= '?' . $self->_args($opts); |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
|
|
4
|
return $self->_get($url, $opts); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub tagger { |
45
|
1
|
|
|
1
|
1
|
9
|
my ($self, $text, $opts) = @_; |
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
|
|
22
|
my $url = $self->_cat('tagger'); |
48
|
1
|
|
|
|
|
10
|
$url .= '?' . $self->_args($opts); |
49
|
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
4
|
return $self->_post($url, $text, $opts); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub dep_parser { |
54
|
1
|
|
|
1
|
1
|
9
|
my ($self, $text, $opts) = @_; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
19
|
my $url = $self->_cat('dep_parser'); |
57
|
1
|
|
|
|
|
10
|
$url .= '?' . $self->_args($opts); |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
5
|
return $self->_post($url, $text, $opts); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub tf { |
63
|
0
|
|
|
0
|
1
|
0
|
my ($self, $text, $opts) = @_; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
0
|
my $url = $self->_cat('tf'); |
66
|
0
|
|
|
|
|
0
|
$url .= '?' . $self->_args($opts); |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
return $self->_post($url, $text, $opts); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub stopwords { |
72
|
0
|
|
|
0
|
1
|
0
|
my ($self, $opts) = @_; |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
0
|
my $url = $self->_cat('stopwords'); |
75
|
0
|
|
|
|
|
0
|
$url .= '?' . $self->_args($opts); |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
0
|
return $self->_get($url, $opts); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub _post { |
81
|
3
|
|
|
3
|
|
9
|
my ($self, $url, $text, $opts) = @_; |
82
|
|
|
|
|
|
|
|
83
|
3
|
|
|
|
|
19
|
my $key = $url . '-' . md5_base64(Encode::encode_utf8($text)); |
84
|
3
|
|
|
|
|
58
|
my $data = $self->{cache}->get($key); |
85
|
|
|
|
|
|
|
|
86
|
3
|
50
|
|
|
|
350
|
unless ($data) { |
87
|
3
|
|
|
|
|
30
|
my $req = HTTP::Request->new(POST => $url); |
88
|
3
|
|
|
|
|
15831
|
$req->header('Content-Type', 'text/html; charset=UTF-8'); |
89
|
3
|
|
|
|
|
279
|
$req->content(Encode::encode_utf8($text)); |
90
|
|
|
|
|
|
|
|
91
|
3
|
|
|
|
|
118
|
my $res = $self->{ua}->request($req); |
92
|
3
|
50
|
|
|
|
72258157
|
if ($res->is_success) { |
93
|
3
|
|
|
|
|
57
|
$data = $res->decoded_content; |
94
|
3
|
50
|
|
|
|
662
|
$data = $res->content unless $data; |
95
|
3
|
|
|
|
|
23
|
$data = Encode::decode_utf8($data); |
96
|
3
|
|
|
|
|
308
|
$self->{cache}->set($key, $data); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
else { |
99
|
0
|
|
|
|
|
0
|
print STDERR "HTTP POST error: ", $res->code, " - ", $res->message, "\n"; |
100
|
0
|
|
|
|
|
0
|
return undef; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
3
|
50
|
33
|
|
|
1156
|
return $data if ($opts->{output} and $opts->{output} eq 'raw'); |
105
|
3
|
|
|
|
|
88
|
return JSON::XS->new->decode($data); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub _get { |
109
|
1
|
|
|
1
|
|
4
|
my ($self, $url, $opts) = @_; |
110
|
|
|
|
|
|
|
|
111
|
1
|
|
|
|
|
9
|
my $key = $url . '-' . md5_base64(join('', values %$opts)); |
112
|
1
|
|
|
|
|
9
|
my $data = $self->{cache}->get($key); |
113
|
|
|
|
|
|
|
|
114
|
1
|
50
|
|
|
|
119
|
unless ($data) { |
115
|
1
|
|
|
|
|
11
|
my $req = HTTP::Request->new(GET => $url); |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
5484
|
my $res = $self->{ua}->request($req); |
118
|
1
|
50
|
|
|
|
14003264
|
if ($res->is_success) { |
119
|
1
|
|
|
|
|
17
|
$data = $res->decoded_content; |
120
|
1
|
50
|
|
|
|
202
|
$data = $res->content unless $data; |
121
|
1
|
|
|
|
|
10
|
$data = Encode::decode_utf8($data); |
122
|
1
|
|
|
|
|
122
|
$self->{cache}->set($key, $data); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else { |
125
|
0
|
|
|
|
|
0
|
print STDERR "HTTP GET error: ", $res->code, " - ", $res->message, "\n"; |
126
|
0
|
|
|
|
|
0
|
return undef; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
1
|
50
|
33
|
|
|
344
|
return $data if ($opts->{output} and $opts->{output} eq 'raw'); |
131
|
1
|
|
|
|
|
48
|
return JSON::XS->new->decode($data); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _cat { |
135
|
4
|
|
|
4
|
|
12
|
my ($self, @args) = @_; |
136
|
|
|
|
|
|
|
|
137
|
4
|
|
|
|
|
15
|
my @parts = ($self->{url}); |
138
|
4
|
|
|
|
|
8
|
push @parts, @args; |
139
|
|
|
|
|
|
|
|
140
|
4
|
|
|
|
|
22
|
return join('/', @parts); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub _args { |
144
|
4
|
|
|
4
|
|
12
|
my ($self, $opts) = @_; |
145
|
|
|
|
|
|
|
|
146
|
4
|
|
|
|
|
7
|
my @args; |
147
|
4
|
|
|
|
|
17
|
foreach (keys %$opts) { |
148
|
0
|
|
|
|
|
0
|
push @args, join('=', $_, $opts->{$_}); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
4
|
|
|
|
|
19
|
return join('&', @args); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |