PT.pm

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