blib/lib/Lingua/PT/Actants.pm

filter: Statementstmt 61.0% 130 / 213 Branchbran 65.9% 29 / 44 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 60.8% 14 / 23 Podpod 54.5% 6 / 11 totaltotal 61.1% 181 / 296 CC 50 SCAR 19.3
CC
50
Cov
62%
cores2dot
20.0
tree2dot
12.0
cores_simple
12.0
CRAP 6.9
↑ Summary
1 package Lingua::PT::Actants;
2 # ABSTRACT: compute verb actants for Portuguese
3 3 $Lingua::PT::Actants::VERSION = '0.05';
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Storable qw/dclone/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
10 4 my ($class, %args) = @_;
11 4 my $self = bless({ }, $class);
12  
13 4 ▶ if (exists($args{conll})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'conll'}) { } 4 0 4
14 4 $self->{conll} = $args{conll};
15 }
16 else {
17 # FIXME PLN::PT
18 }
19  
20 # initial data -- conll format
21 4 $self->{data} = $self->_conll2data($self->{conll});
22  
23 # build a tree from the list of deps
24 4 $self->{tree} = $self->_data2tree($self->{data});
25  
26 # split tree into one tree per verb+conj
27 4 my $tree = dclone($self->{tree});
28 4 $self->{deps} = [ reverse $self->_tree2deps($tree) ];
29  
30 # simplify each dep tree verbs
31 4 my @deps = @{dclone($self->{deps})};
32 4 $self->{simple} = [ map {$self->_tree2simple($_)} @deps ];
33  
34 4 return $self;
35 }
36  
37 # conll -> data
38 4 ▶sub _conll2data {
Subroutine _conll2data: called CC=3 SCAR=11.0
39 4 my ($self, $conll) = @_;
40  
41 4 my @data;
42 4 foreach my $line (split /\n/, $conll) {
43 26 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 26 26
44  
45 26 my @l = split /\s+/, $line;
46 26 push @data, {
47 id=>$l[0], form=>$l[1], pos=>$l[3], dep=>$l[6], rule=>$l[7]
48 };
49 }
50  
51 4 return [@data];
52 }
53  
54 # data -> tree
55 4 ▶sub _data2tree {
Subroutine _data2tree: called CC=3 SCAR=11.0
56 4 my ($self, $data) = @_;
57  
58 4 my $root;
59 4 foreach (@$data) {
60 26 ▶ $root = $_ if $_->{rule} eq 'ROOT';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"rule"} eq "ROOT" 4 22 26
61 }
62  
63 4 $root = $self->_node($root, $data);
64  
65 4 return $root;
66 }
67  
68 26 ▶sub _node {
Subroutine _node: called CC=3 SCAR=11.0
69 26 my ($self, $node, $data) = @_;
70  
71 26 my @child = ();
72 26 foreach (@$data) {
73 174 ▶ push @child, $self->_node($_, $data) if ($_->{dep} == $node->{id});
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"dep"} == $node->{"id"} 22 152 174
74 }
75 26 $node->{child} = [@child];
76  
77 26 return $node;
78 }
79  
80 # tree -> deps
81 4 ▶sub _tree2deps {
Subroutine _tree2deps: called CC=5 SCAR=16.6
82 4 my ($self, $node, @deps) = @_;
83  
84 4 ▶ if ($node->{pos} eq 'VERB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"pos"} eq "VERB") 4 0 4
85 4 my @child = ();
86 4 foreach my $c (@{ $node->{child} }) {
87 13 ▶ if ($c->{pos} eq 'VERB' and $c->{rule} eq 'conj') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'pos'} eq 'VERB' and $c->{'rule'} eq 'conj') { } 0 13 13
Condition66%2/3
$c->{"pos"} eq "VERB" and $c->{"rule"} eq "conj"
!ll&&!rl&&r
1120
MC/DC0%0/2
$c->{"pos"} eq "VERB" and $c->{"rule"} eq "conj"
$c->{"pos"} eq "VERB" $c->{"rule"} eq "conj"
Truth table66%2/3
A and B
A= $c->{"pos"} eq "VERB"B= $c->{"rule"} eq "conj"
ABresult
0X0
100
111
88 0 push @deps, $self->_tree2deps($c, @deps);
89 }
90 else {
91 13 push @child, $c;
92 }
93 }
94 4 $node->{child} = [@child];
95 }
96 4 push @deps, $node;
97  
98 4 return @deps;
99 }
100  
101 # tree -> simple tree
102 # FIXME make recursive
103 4 ▶sub _tree2simple {
Subroutine _tree2simple: called CC=7 SCAR=19.5
104 4 my ($self, $node) = @_;
105  
106 4 ▶ if ($node->{pos} eq 'VERB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"pos"} eq "VERB") 4 0 4
107 4 my $found = undef;
108 4 foreach my $c (@{ $node->{child} }) {
109 13 ▶ $found = $c if ($c->{pos} eq 'VERB');
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"pos"} eq "VERB" 2 11 13
110 }
111 4 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 2 2 4
112 2 my @child;
113 2 foreach (@{ $node->{child} }, @{$found->{child}}) {
114 9 ▶ push @child, $_ unless $_->{id} == $found->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"id"} == $found->{"id"} 7 2 9
115 }
116 2 $found->{child} = [@child];
117 2 $node = $found;
118 }
119 }
120  
121 4 return $node;
122 }
123  
124 0 ▶sub tree2dot {
Subroutine tree2dot: not called CC=3 SCAR=24.8
Pod undocumented
125 0 my ($self, $tree) = @_;
126  
127 0 my $data = $self->{$tree};
128 0 my @graphs = ();
129 0 ▶ if (ref($data) eq 'ARRAY') { push @graphs, @$data; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
130 0 else { push @graphs, $data; }
131  
132 0 my $dot = "digraph G {\ncharset= \"UTF-8\";\n";
133 0 foreach (@graphs) {
134 0 my $rand = int(rand(1000));
135 0 $dot .= " subgraph G_$rand {\n";
136 0 $dot .= join("\n", $self->_deps2nodes($rand, $_));
137 0 $dot .= join("\n", $self->_deps2edges($rand, $_));
138 0 $dot .= "\n }\n";
139 }
140 0 $dot .= "\n}\n";
141 }
142  
143 0 ▶sub _deps2nodes {
Subroutine _deps2nodes: not called CC=2 SCAR=17.9
144 0 my ($self, $prefix, $node) = @_;
145 0 my @lines;
146  
147 0 push @lines, " node [label=\"$node->{form}\"] N_${prefix}_$node->{id};";
148 0 foreach (@{$node->{child}}) {
149 0 push @lines, $self->_deps2nodes($prefix, $_);
150 }
151  
152 0 return @lines;
153 }
154  
155 0 ▶sub _deps2edges {
Subroutine _deps2edges: not called CC=2 SCAR=17.9
156 0 my ($self, $prefix, $node) = @_;
157 0 my @lines;
158  
159 0 foreach (@{$node->{child}}) {
160 0 push @lines, " N_${prefix}_$node->{id} -> N_${prefix}_$_->{id} [label=\"$_->{rule}\"];";
161 0 push @lines, $self->_deps2edges($prefix, $_);
162 }
163  
164 0 return @lines;
165 }
166  
167 0 ▶sub cores2dot {
Subroutine cores2dot: not called CC=4 SCAR=30.0
Pod undocumented
168 0 my ($self, @cores) = @_;
169  
170 0 my $dot = "digraph G {\ncharset= \"UTF-8\";\n";
171 0 foreach my $core (@cores) {
172 0 my $rand = int(rand(1000));
173 0 $dot .= " subgraph G_$rand {\n";
174 0 foreach ($core->{verb}, @{$core->{cores}}) {
175 0 $dot .= " node [label=\"$_->{form}\"] N_$_->{id};";
176 }
177 0 foreach (@{$core->{cores}}) {
178 0 $dot .= " N_$core->{verb}->{id} -> N_$_->{id};\n";
179 }
180 0 $dot .= "\n }\n";
181 }
182 0 $dot .= "\n}\n";
183  
184 0 return $dot;
185 }
186  
187 0 ▶sub text {
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
188 0 my ($self) = @_;
189  
190 0 return join(' ', map {$_->{form}} @{$self->{data}});
191 }
192  
193 0 ▶sub actants {
Subroutine actants: not called CC=1 SCAR=6.9
Pod documented
194 0 my ($self, %args) = @_;
195  
196 0 my @cores = $self->acts_cores;
197 0 my @syns = $self->acts_syns(@cores);
198  
199 0 return ([@cores], [@syns]);
200 }
201  
202 # compute actant cores
203 4 ▶sub acts_cores {
Subroutine acts_cores: called CC=5 SCAR=16.1
Pod documented
204 4 my ($self) = @_;
205 4 my $data = $self->{simple};
206 4 my @final;
207  
208 4 foreach my $tree (@$data) {
209 4 my $verb = dclone($tree);
210 4 delete $verb->{child};
211  
212 4 my @cores = ();
213 4 my @children = @{$tree->{child}};
214  
215 4 foreach my $i (@children) {
216 20 ▶ if ($self->_score($i) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_score($i) > 0) 8 12 20
217 8 push @cores, $i;
218 }
219 20 ▶ push @children, @{$i->{child}} if $i->{child};
Branch: 1 branch
BranchTrueFalseTotal
if $i->{"child"} 20 0 20
220 }
221 4 push @final, { verb=>$verb, cores=>[@cores] };
222 }
223  
224 4 $self->{cores} = [@final];
225  
226 4 return @final;
227 }
228  
229 0 ▶sub cores_simple {
Subroutine cores_simple: not called CC=3 SCAR=24.8
Pod undocumented
230 0 my ($self, @cores) = @_;
231 0 ▶ @cores = @{$self->{cores}} unless @cores;
Branch: 1 branch
BranchTrueFalseTotal
unless @cores 0 0 0
232  
233 0 my @simple;
234 0 foreach (@cores) {
235 0 my $verb = $_->{verb}->{form};
236 0 my @cs = @{$_->{cores}};
237 0 @cs = map {$_->{form}} @cs;
238 0 push @simple, { $verb => [@cs] };
239 }
240  
241 0 return @simple;
242 }
243  
244 3 ▶sub acts_syns {
Subroutine acts_syns: called CC=8 SCAR=21.0
Pod undocumented
245 3 my ($self, @cores) = @_;
246 0 ▶ @cores = @{$self->{cores}} unless @cores;
Branch: 1 branch
BranchTrueFalseTotal
unless @cores 0 3 3
247  
248 3 my @syns;
249 3 foreach (@cores) {
250 3 my $verb = $_->{verb};
251 3 my @cs = @{ $_->{cores} };
252 3 my @curr;
253  
254 3 foreach my $c (@cs) {
255 6 my @tokens = ($c);
256 6 ▶ my @child = exists($c->{child}) ? @{$c->{child}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $c->{'child'} ? : 6 0 6
257 6 foreach (@child) {
258 6 ▶ unless (_is_core($_, @cs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_core($_, @cs)) 6 0 6
259 6 push @tokens, $_;
260 6 push @child, @{$_->{child}};
261 }
262 }
263 6 @tokens = sort {$a->{id} <=> $b->{id}} @tokens;
264 6 delete($_->{child}) foreach (@tokens);
265 6 push @curr, [@tokens];
266 }
267  
268 3 push @syns, { verb=>$verb, syns=>[@curr] };
269 }
270  
271 3 return @syns;
272 }
273  
274 6 ▶sub _is_core {
Subroutine _is_core: called CC=3 SCAR=11.1
275 6 my ($c, @cs) = @_;
276  
277 6 foreach (@cs) {
278 12 ▶ return 1 if $_->{id} == $c->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"id"} == $c->{"id"} 0 12 12
279 }
280  
281 6 return 0;
282 }
283  
284 20 ▶sub _score {
Subroutine _score: called CC=5 SCAR=16.1
285 20 my ($self, $token) = @_;
286 20 my $score = 0;
287  
288 # token POS component
289 20 ▶ $score += 8 if ($token->{pos} =~ m/^(noun|propn|prop)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"pos"} =~ /^(noun|propn|prop)$/i 8 12 20
290 20 ▶ $score += -10 if ($token->{pos} =~ m/^(punct)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"pos"} =~ /^(punct)$/i 4 16 20
291  
292 # token rule component
293 20 ▶ $score += 4 if ($token->{rule} =~ m/^(nsubj|nsubjpass)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"rule"} =~ /^(nsubj|nsubjpass)$/i 4 16 20
294 20 ▶ $score += 2 if ($token->{rule} =~ m/^(nmod)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"rule"} =~ /^(nmod)$/i 0 20 20
295  
296 20 return $score;
297 }
298  
299 3 ▶sub syns_simple {
Subroutine syns_simple: called CC=3 SCAR=11.0
Pod undocumented
300 3 my ($self, @syns) = @_;
301  
302 3 my @simple;
303 3 foreach (@syns) {
304 3 my $verb = $_->{verb}->{form};
305 3 my @curr;
306 3 foreach my $s (@{$_->{syns}}) {
307 6 push @curr, join(' ', map {$_->{form}} @$s);
308 };
309
310 3 push @simple, { $verb => [@curr] };
311 }
312  
313 3 return @simple;
314 }
315  
316 0 ▶sub pp_acts_cores {
Subroutine pp_acts_cores: not called CC=3 SCAR=24.8
Pod documented
317 0 my ($self, @cores) = @_;
318  
319 0 my $r = "# Actants syntagma cores\n";
320 0 foreach (@cores) {
321 0 my ($verb, @tokens) = ($_->{verb}, @{$_->{cores}} );
322  
323 0 $r .= " Verb: $verb->{form}\n";
324 0 foreach (@tokens) {
325 0 $r .= sprintf " = %s\n", $_->{form};
326 }
327 }
328  
329 0 return $r;
330 }
331  
332 0 ▶sub pp_acts_syntagmas {
Subroutine pp_acts_syntagmas: not called CC=3 SCAR=24.8
Pod documented
333 0 my ($self, @syns) = @_;
334  
335 0 my $r = "# Actants syntagmas\n";
336 0 foreach (@syns) {
337 0 my ($verb, @list) = ($_->{verb}, @{ $_->{syns} });
338  
339 0 $r .= " Verb: $verb->{form}\n";
340 0 foreach (@list) {
341 0 $r .= sprintf " = %s\n", join(' ', map {$_->{form}} @$_);
342 }
343 }
344  
345 0 return $r;
346 }
347  
348 3 1;
349  
350 __END__
↑ Summary