File Coverage

blib/lib/PLN/PT.pm
Criterion Covered Total %
statement 82 95 86.3
branch 8 16 50.0
condition 2 6 33.3
subroutine 17 19 89.4
pod 7 7 100.0
total 116 143 81.1


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.008';
4 5     5   230213 use strict;
  5         42  
  5         132  
5 5     5   26 use warnings;
  5         9  
  5         120  
6              
7 5     5   2201 use JSON::XS;
  5         26605  
  5         243  
8 5     5   1708 use CHI;
  5         313854  
  5         160  
9 5     5   33 use Digest::MD5 qw/md5_base64/;
  5         10  
  5         240  
10 5     5   2137 use LWP::UserAgent;
  5         159953  
  5         170  
11 5     5   1867 use Encode;
  5         35758  
  5         322  
12 5     5   1392 use utf8::all;
  5         134169  
  5         30  
13              
14             sub new {
15 4     4 1 303 my ($class, $url) = @_;
16 4         16 my $self = bless( {url=>$url}, $class);
17              
18 4         28 $self->{ua} = LWP::UserAgent->new;
19 4         8086 $self->{cache} = CHI->new( driver => 'Memory', global => 1 );
20              
21 4         287457 return $self;
22             }
23              
24             sub tokenizer {
25 1     1 1 10 my ($self, $text, $opts) = @_;
26              
27 1         19 my $url = $self->_cat('tokenizer');
28 1         9 $url .= '?' . $self->_args($opts);
29              
30 1         4 return $self->_post($url, $text, $opts);
31             }
32              
33             sub morph_analyzer {
34 1     1 1 9 my ($self, $word, $opts) = @_;
35              
36 1         15 $word =~ s/\// /g; # make it sane, if someone tries to go guessing
37              
38 1         5 my $url = $self->_cat('morph_analyzer', $word);
39 1         4 $url .= '?' . $self->_args($opts);
40              
41 1         5 return $self->_get($url, $opts);
42             }
43              
44             sub tagger {
45 1     1 1 9 my ($self, $text, $opts) = @_;
46              
47 1         15 my $url = $self->_cat('tagger');
48 1         8 $url .= '?' . $self->_args($opts);
49              
50 1         4 return $self->_post($url, $text, $opts);
51             }
52              
53             sub dep_parser {
54 1     1 1 10 my ($self, $text, $opts) = @_;
55              
56 1         16 my $url = $self->_cat('dep_parser');
57 1         11 $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         55 my $data = $self->{cache}->get($key);
85              
86 3 50       315 unless ($data) {
87 3         25 my $req = HTTP::Request->new(POST => $url);
88 3         15397 $req->header('Content-Type', 'text/html; charset=UTF-8');
89 3         270 $req->content(Encode::encode_utf8($text));
90              
91 3         115 my $res = $self->{ua}->request($req);
92 3 50       16840958 if ($res->is_success) {
93 3         58 $data = $res->decoded_content;
94 3 50       513 $data = $res->content unless $data;
95 3         24 $data = Encode::decode_utf8($data);
96 3         309 $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     1067 return $data if ($opts->{output} and $opts->{output} eq 'raw');
105 3         121 return JSON::XS->new->decode($data);
106             }
107              
108             sub _get {
109 1     1   4 my ($self, $url, $opts) = @_;
110              
111 1         8 my $key = $url . '-' . md5_base64(join('', values %$opts));
112 1         9 my $data = $self->{cache}->get($key);
113              
114 1 50       124 unless ($data) {
115 1         9 my $req = HTTP::Request->new(GET => $url);
116              
117 1         5432 my $res = $self->{ua}->request($req);
118 1 50       3118483 if ($res->is_success) {
119 1         18 $data = $res->decoded_content;
120 1 50       230 $data = $res->content unless $data;
121 1         6 $data = Encode::decode_utf8($data);
122 1         87 $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     357 return $data if ($opts->{output} and $opts->{output} eq 'raw');
131 1         33 return JSON::XS->new->decode($data);
132             }
133              
134             sub _cat {
135 4     4   15 my ($self, @args) = @_;
136              
137 4         13 my @parts = ($self->{url});
138 4         9 push @parts, @args;
139              
140 4         22 return join('/', @parts);
141             }
142              
143             sub _args {
144 4     4   10 my ($self, $opts) = @_;
145              
146 4         6 my @args;
147 4         14 foreach (keys %$opts) {
148 0         0 push @args, join('=', $_, $opts->{$_});
149             }
150              
151 4         17 return join('&', @args);
152             }
153              
154             1;
155              
156             __END__