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