blib/lib/Lingua/PT/Actants.pm

filter: Statementstmt 39.9% 85 / 213 Branchbran 47.7% 21 / 44 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 47.8% 11 / 23 Podpod 54.5% 6 / 11 totaltotal 41.8% 124 / 296 SCAR 63.3
CC
50
Cov
41%
acts_syns
72.0
cores2dot
20.0
_tree2simple
14.0
CRAP 559.4
↑ Summary
1 package Lingua::PT::Actants;
2 # ABSTRACT: compute verb actants for Portuguese
3 3 $Lingua::PT::Actants::VERSION = '0.04';
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 sub new {
10 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
11 1 my $self = bless({ }, $class);
12  
13 1 ▶ if (exists($args{conll})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'conll'}) { } 1 0 1
14 1 $self->{conll} = $args{conll};
15 }
16 else {
17 # FIXME PLN::PT
18 }
19  
20 # initial data -- conll format
21 1 $self->{data} = $self->_conll2data($self->{conll});
22  
23 # build a tree from the list of deps
24 1 $self->{tree} = $self->_data2tree($self->{data});
25  
26 # split tree into one tree per verb+conj
27 1 my $tree = dclone($self->{tree});
28 1 $self->{deps} = [ reverse $self->_tree2deps($tree) ];
29  
30 # simplify each dep tree verbs
31 1 my @deps = @{dclone($self->{deps})};
32 1 $self->{simple} = [ map {$self->_tree2simple($_)} @deps ];
33  
34 1 return $self;
35 }
36  
37 # conll -> data
38 sub _conll2data {
39 1 ▶ my ($self, $conll) = @_;
Subroutine _conll2data: called CC=3 SCAR=11.0
40  
41 1 my @data;
42 1 foreach my $line (split /\n/, $conll) {
43 5 ▶ next if $line =~ m/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 5 5
44  
45 5 my @l = split /\s+/, $line;
46 5 push @data, {
47 id=>$l[0], form=>$l[1], pos=>$l[3], dep=>$l[6], rule=>$l[7]
48 };
49 }
50  
51 1 return [@data];
52 }
53  
54 # data -> tree
55 sub _data2tree {
56 1 ▶ my ($self, $data) = @_;
Subroutine _data2tree: called CC=3 SCAR=11.0
57  
58 1 my $root;
59 1 foreach (@$data) {
60 5 ▶ $root = $_ if $_->{rule} eq 'ROOT';
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"rule"} eq "ROOT" 1 4 5
61 }
62  
63 1 $root = $self->_node($root, $data);
64  
65 1 return $root;
66 }
67  
68 sub _node {
69 5 ▶ my ($self, $node, $data) = @_;
Subroutine _node: called CC=3 SCAR=11.0
70  
71 5 my @child = ();
72 5 foreach (@$data) {
73 25 ▶ push @child, $self->_node($_, $data) if ($_->{dep} == $node->{id});
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"dep"} == $node->{"id"} 4 21 25
74 }
75 5 $node->{child} = [@child];
76  
77 5 return $node;
78 }
79  
80 # tree -> deps
81 sub _tree2deps {
82 1 ▶ my ($self, $node, @deps) = @_;
Subroutine _tree2deps: called CC=5 SCAR=17.1
83  
84 1 ▶ if ($node->{pos} eq 'VERB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"pos"} eq "VERB") 1 0 1
85 1 my @child = ();
86 1 foreach my $c (@{ $node->{child} }) {
87 3 ▶ 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 3 3
Condition33%1/3
$c->{"pos"} eq "VERB" and $c->{"rule"} eq "conj"
!ll&&!rl&&r
300
MC/DC0%0/2
$c->{"pos"} eq "VERB" and $c->{"rule"} eq "conj"
$c->{"pos"} eq "VERB" $c->{"rule"} eq "conj"
Truth table33%1/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 3 push @child, $c;
92 }
93 }
94 1 $node->{child} = [@child];
95 }
96 1 push @deps, $node;
97  
98 1 return @deps;
99 }
100  
101 # tree -> simple tree
102 # FIXME make recursive
103 sub _tree2simple {
104 1 ▶ my ($self, $node) = @_;
Subroutine _tree2simple: called CC=7 SCAR=26.4
105  
106 1 ▶ if ($node->{pos} eq 'VERB') {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"pos"} eq "VERB") 1 0 1
107 1 my $found = undef;
108 1 foreach my $c (@{ $node->{child} }) {
109 3 ▶ $found = $c if ($c->{pos} eq 'VERB');
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"pos"} eq "VERB" 0 3 3
110 }
111 1 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 0 1 1
112 0 my @child;
113 0 foreach (@{ $node->{child} }, @{$found->{child}}) {
114 0 ▶ push @child, $_ unless $_->{id} == $found->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"id"} == $found->{"id"} 0 0 0
115 }
116 0 $found->{child} = [@child];
117 0 $node = $found;
118 }
119 }
120  
121 1 return $node;
122 }
123  
124 sub tree2dot {
125 0 ▶ my ($self, $tree) = @_;
Subroutine tree2dot: not called CC=3 SCAR=24.8
Pod undocumented
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 sub _deps2nodes {
144 0 ▶ my ($self, $prefix, $node) = @_;
Subroutine _deps2nodes: not called CC=2 SCAR=17.9
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 sub _deps2edges {
156 0 ▶ my ($self, $prefix, $node) = @_;
Subroutine _deps2edges: not called CC=2 SCAR=17.9
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 sub cores2dot {
168 0 ▶ my ($self, @cores) = @_;
Subroutine cores2dot: not called CC=4 SCAR=30.0
Pod undocumented
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 sub text {
188 0 ▶ my ($self) = @_;
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
189  
190 0 return join(' ', map {$_->{form}} @{$self->{data}});
191 }
192  
193 sub actants {
194 0 ▶ my ($self, %args) = @_;
Subroutine actants: not called CC=1 SCAR=6.9
Pod documented
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 sub acts_cores {
204 1 ▶ my ($self) = @_;
Subroutine acts_cores: called CC=5 SCAR=16.1
Pod documented
205 1 my $data = $self->{simple};
206 1 my @final;
207  
208 1 foreach my $tree (@$data) {
209 1 my $verb = dclone($tree);
210 1 delete $verb->{child};
211  
212 1 my @cores = ();
213 1 my @children = @{$tree->{child}};
214  
215 1 foreach my $i (@children) {
216 4 ▶ if ($self->_score($i) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_score($i) > 0) 2 2 4
217 2 push @cores, $i;
218 }
219 4 ▶ push @children, @{$i->{child}} if $i->{child};
Branch: 1 branch
BranchTrueFalseTotal
if $i->{"child"} 4 0 4
220 }
221 1 push @final, { verb=>$verb, cores=>[@cores] };
222 }
223  
224 1 $self->{cores} = [@final];
225  
226 1 return @final;
227 }
228  
229 sub cores_simple {
230 0 ▶ my ($self, @cores) = @_;
Subroutine cores_simple: not called CC=3 SCAR=24.8
Pod undocumented
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 sub acts_syns {
245 0 ▶ my ($self, @cores) = @_;
Subroutine acts_syns: not called CC=8 SCAR=42.8
Pod undocumented
246 0 ▶ @cores = @{$self->{cores}} unless @cores;
Branch: 1 branch
BranchTrueFalseTotal
unless @cores 0 0 0
247  
248 0 my @syns;
249 0 foreach (@cores) {
250 0 my $verb = $_->{verb};
251 0 my @cs = @{ $_->{cores} };
252 0 my @curr;
253  
254 0 foreach my $c (@cs) {
255 0 my @tokens = ($c);
256 0 ▶ my @child = exists($c->{child}) ? @{$c->{child}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $c->{'child'} ? : 0 0 0
257 0 foreach (@child) {
258 0 ▶ unless (_is_core($_, @cs)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_core($_, @cs)) 0 0 0
259 0 push @tokens, $_;
260 0 push @child, @{$_->{child}};
261 }
262 }
263 0 @tokens = sort {$a->{id} <=> $b->{id}} @tokens;
264 0 delete($_->{child}) foreach (@tokens);
265 0 push @curr, [@tokens];
266 }
267  
268 0 push @syns, { verb=>$verb, syns=>[@curr] };
269 }
270  
271 0 return @syns;
272 }
273  
274 sub _is_core {
275 0 ▶ my ($c, @cs) = @_;
Subroutine _is_core: not called CC=3 SCAR=24.8
276  
277 0 foreach (@cs) {
278 0 ▶ return 1 if $_->{id} == $c->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"id"} == $c->{"id"} 0 0 0
279 }
280  
281 0 return 0;
282 }
283  
284 sub _score {
285 4 ▶ my ($self, $token) = @_;
Subroutine _score: called CC=5 SCAR=16.1
286 4 my $score = 0;
287  
288 # token POS component
289 4 ▶ $score += 8 if ($token->{pos} =~ m/^(noun|propn|prop)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"pos"} =~ /^(noun|propn|prop)$/i 2 2 4
290 4 ▶ $score += -10 if ($token->{pos} =~ m/^(punct)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"pos"} =~ /^(punct)$/i 1 3 4
291  
292 # token rule component
293 4 ▶ $score += 4 if ($token->{rule} =~ m/^(nsubj|nsubjpass)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"rule"} =~ /^(nsubj|nsubjpass)$/i 1 3 4
294 4 ▶ $score += 2 if ($token->{rule} =~ m/^(nmod)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"rule"} =~ /^(nmod)$/i 0 4 4
295  
296 4 return $score;
297 }
298  
299 sub syns_simple {
300 0 ▶ my ($self, @syns) = @_;
Subroutine syns_simple: not called CC=3 SCAR=24.8
Pod undocumented
301  
302 0 my @simple;
303 0 foreach (@syns) {
304 0 my $verb = $_->{verb}->{form};
305 0 my @curr;
306 0 foreach my $s (@{$_->{syns}}) {
307 0 push @curr, join(' ', map {$_->{form}} @$s);
308 };
309
310 0 push @simple, { $verb => [@curr] };
311 }
312  
313 0 return @simple;
314 }
315  
316 sub pp_acts_cores {
317 0 ▶ my ($self, @cores) = @_;
Subroutine pp_acts_cores: not called CC=3 SCAR=24.8
Pod documented
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 sub pp_acts_syntagmas {
333 0 ▶ my ($self, @syns) = @_;
Subroutine pp_acts_syntagmas: not called CC=3 SCAR=24.8
Pod documented
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