blib/lib/Lingua/BioYaTeA/PreProcessing.pm

filter: Statementstmt 93.6% 281 / 300 Branchbran 64.5% 40 / 62 Conditioncond 94.7% 54 / 57 MC/DCmcdc 89.6% 26 / 29 Subroutinesub 97.2% 70 / 72 Podpod 97.7% 43 / 44 totaltotal 91.1% 514 / 564 SCAR 42.8
CC
67
Cov
89%
process_sentence
13.2
_printPatterns
12.0
process_file
8.1
CRAP 72.2
↑ Summary Build »
1 package Lingua::BioYaTeA::PreProcessing;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Lingua::YaTeA::Corpus;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # use Data::Dumper;
10  
11 =encoding utf8
12  
13 =head1 NAME
14  
15 Lingua::BioYaTeA::PreProcessing - Perl extension for preprocessing BioYaTeA input.
16  
17 =head1 SYNOPSIS
18  
19 use Lingua::BioYaTeA::PreProcessing;
20  
21 $preProc = Lingua::BioYaTeA::PreProcessing->new();
22 open($fh, ">t/example_output_preprocessing-new.ttg") or ($fh = *STDERR);
23 $preProc->process_file("t/example_input_preprocessing.ttg", $fh);
24 close($fh);
25  
26 =head1 DESCRIPTION
27  
28  
29 The module implements an extension for the pre-processing of the
30 TreeTagger output in order to improve the extraction of both terms
31 containing prepositional phrases (with C<TO> and C<AT> prepositions) and
32 terms containing participles (past participles C<-ED> and gerunds C<-ING>).
33  
34 Context-based rules are applied to the POS tags either to trigger the
35 extraction of relevant structures or to prevent the extraction of
36 irrelevant ones. The modified file becomes a new input file for
37 BioYaTeA.
38  
39 The input and output files are in the TreeTagger format.
40  
41 =head1 METHODS
42  
43 =head2 new()
44  
45 new();
46  
47 The method creates a pre-processing component of BioYaTeA and loads
48 the additional resources (stop verbs, stop participles, stop words)
49 the rewritting patrerns (all are currently hardcoded), and returns the
50 created object.
51  
52 The pre-processing object is defined with 4 attributes: the list of
53 stop verbs C<stopVerbs>, the list of stop participles
54 C<stopParticiples>, the list of stop words C<stoplist> and the list of
55 rewritting patterns C<patterns>.
56  
57 =head2 getStopVerbs()
58  
59 getStopVerbs($form);
60  
61 This method returns the attribute C<stopVerbs> or the specific value
62 associated to form C<$form>.
63  
64 =head2 existsInStopVerbs()
65  
66 existsInStopVerbs($form);
67  
68 This method indicates if the form C<$form> exists in the list of stop
69 verbs (C<stopVerbs> attribute).
70  
71 =head2 loadStopVerbs()
72  
73 loadStopVerbs($form);
74  
75 This method loads the list of stop verbs in the attribute
76 C<stopVerbs> and returns the attribute.
77  
78 =head2 getStopParticiples()
79  
80 getStopParticiples($form);
81  
82 This method returns the attribute C<stopParticiples> or the specific value
83 associated to form C<$form>.
84  
85 =head2 existsInStopParticiples()
86  
87 existsInStopParticiples($form);
88  
89 This method indicates if the form C<$form> exists in the list of stop
90 participles (C<stopParticiples> attribute).
91  
92 =head2 loadStopParticiples()
93  
94 loadStopParticiples($form);
95  
96 This method loads the list of stop participles in the attribute
97 C<stopParticiples> and returns the attribute.
98  
99  
100 =head2 getStopList()
101  
102 getStopList($form);
103  
104 This method returns the attribute C<stopList> or the specific value
105 associated to form C<$form>.
106  
107 =head2 existsInStopList()
108  
109 existsInStopList($form);
110  
111 This method indicates if the form C<$form> exists in the list of stop
112 words (C<stopList> attribute).
113  
114 =head2 loadStopList()
115  
116 loadStopList($form);
117  
118 This method loads the list of stop words in the attribute
119 C<stopList> and returns the attribute.
120  
121  
122 =head2 compile1()
123  
124 compile1($pattern, $result);
125  
126 This method performs the first step of the compilation of the pattern
127 C<$pattern> by generating the related regular expression and creating
128 the related pattern structure. This structure is composed 4 fields:
129 the pattern itself (C<root>), the array of predicates (C<predicates>),
130 the array of named groups (C<namedgroup>) and the regular expression.
131 The array of predicates are functions which will be used for
132 checking the Part-of-speech tags or the form of the words.
133  
134  
135 The second argument is not set at the fist call.
136 The method returns the resulting structure (an array reference).
137  
138 =head2 compile2()
139  
140 compile2($result, $child_pattern);
141  
142 This method performs the second step of the compilation of the
143 patterns. Patterns have been already processed by the method
144 C<compile1> and represented in the structure C<$result>. This step
145 generates the regular expression (field C<re>).
146  
147 The second argument is not set at the fist call.
148  
149 =head2 compile()
150  
151 compile($pattern);
152  
153 This method compiles the pattern C<$pattern> in order to have the
154 relevant represenation and the corresponding regular expression into a
155 array structure C<$result>. This structure is returned.
156  
157 =head2 translate()
158  
159 translate($compiledpattern, $sequence);
160  
161 This method applies the compiled pattern (C<$compiledpattern>) to the
162 sequence C<sequence> into a string and return it. The string provides
163 information associated to various elements of the pattern (it depends
164 on the pattern).
165  
166 =head2 match()
167  
168 match($compiledpattern, $sequence);
169  
170 This method applies the pattern C<$compiledpattern> to the token
171 sequence C<$sequence> and merges the information in order to correct
172 the part-of-speech tag associated to some words. Any rewriting
173 operation is recorded in a array which is returned.
174  
175 =head2 pred()
176  
177 pred($predicate, $quantifier);
178  
179 The method returns the structure defining a predicate. The structure
180 is composed of 3 fields: the type of structure (here "C<predicate>"),
181 the function associated to the predicate (field C<predicate>) and is
182 set with C<$predicate>), and the quantifier associated to the
183 predicate (field C<quantifier>) which is set with C<$quantifier>.
184  
185 =head2 group()
186  
187 group($children, $quantifier);
188  
189 This method returns the structure defining a group of predicates. The
190 structure is composed of 3 fields: the type of structure (here
191 "C<group>"), the list of predicates (field C<children>) and is set
192 with C<$children>), and the quantifier associated to the child list
193 (field C<quantifier>) which is set with C<$quantifier>.
194  
195 =head2 named()
196  
197 named($name, $children, $quantifier);
198  
199 This method returns the structure defining a named group of
200 predicates. The structure is composed of 4 fields: the type of
201 structure (here "C<group>"), the name associated to the group (field
202 C<name>) which is set with C<$name>, the list of predicates (field
203 C<children>) and is set with C<$children>), and the quantifier
204 associated to the child list (field C<quantifier>) which is set with
205 C<$quantifier>.
206  
207  
208 =head2 is_ing()
209  
210 is_ing($word);
211  
212 This method indicates if the word terminates by C<ing> and is not in
213 the list of stop words.
214 o
215 =head2 patterns()
216  
217 patterns();
218  
219 This method returns the list of patterns associated to the current
220 object (field C<patterns>).
221  
222 =head2 setPattern1()
223  
224 setPattern1();
225  
226 This method sets the pattern 1.
227  
228 =head2 getPattern1()
229  
230 getPattern1();
231  
232 This method returns the pattern 1.
233  
234 =head2 setPattern2()
235  
236 setPattern2();
237  
238 This method sets the pattern 2.
239  
240 =head2 getPattern2()
241  
242 getPattern2();
243  
244 This method returns the pattern 2.
245  
246 =head2 setPattern3()
247  
248 setPattern3();
249  
250 This method sets the pattern 3.
251  
252 =head2 getPattern3()
253  
254 getPattern3();
255  
256 This method returns the pattern 3.
257  
258 =head2 setPattern4()
259  
260 setPattern4();
261  
262 This method sets the pattern 4.
263  
264 =head2 getPattern4()
265  
266 getPattern4();
267  
268 This method returns the pattern 4.
269  
270 =head2 setPattern5()
271  
272 setPattern5();
273  
274 This method sets the pattern 5.
275  
276 =head2 getPattern5()
277  
278 getPattern5();
279  
280 This method returns the pattern 5.
281  
282 =head2 setPattern6()
283  
284 setPattern6();
285  
286 This method sets the pattern 6.
287  
288 =head2 getPattern6()
289  
290 getPattern6();
291  
292 This method returns the pattern 6.
293  
294 =head2 setPattern7()
295  
296 setPattern7();
297  
298 This method sets the pattern 7.
299  
300 =head2 getPattern7()
301  
302 getPattern7();
303  
304 This method returns the pattern 7.
305  
306 =head2 setPattern8()
307  
308 setPattern8();
309  
310 This method sets the pattern 8.
311  
312 =head2 getPattern8()
313  
314 getPattern8();
315  
316 This method returns the pattern 8.
317  
318 =head2 setPattern9()
319  
320 setPattern9();
321  
322 This method sets the pattern 9.
323  
324 =head2 getPattern9()
325  
326 getPattern9();
327  
328 This method returns the pattern 9.
329  
330 =head2 setPattern10()
331  
332 setPattern10();
333  
334 This method sets the pattern 10.
335  
336 =head2 getPattern10()
337  
338 getPattern10();
339  
340 This method returns the pattern 10.
341  
342 =head2 not_sent()
343  
344 not_sent($element);
345  
346 This method indicates whether the part-of-speech of element
347 C<$element> is a mark of sentence end.
348  
349 =head2 is_to()
350  
351 is_to($element);
352  
353 This method indicates whether the form of element
354 C<$element> is the preposition C<to>.
355  
356 =head2 process_sentence()
357  
358 process_sentence($sentence, $fh);
359  
360 This method processes the sentence C<$sentence> in order to correct
361 the part-of-speech tags if necessary, and print the corrected sentence
362 in the file handle C<$fh> (the output respects the TreeTagger format).
363  
364 =head2 process_file()
365  
366 process_file($file, $fhout);
367  
368 The method performs the correction process on the file
369 C<$file>. The output will be printed in the file handle C<$fhout>.
370 C<$file> is the filename of the file to process.
371  
372 =head1 SEE ALSO
373  
374 Documentation of Lingua::BioYaTeA and Lingua::YaTeA
375  
376 =head1 AUTHORS
377  
378 Wiktoria Golik <wiktoria.golik@jouy.inra.fr>, Zorana Ratkovic <Zorana.Ratkovic@jouy.inra.fr>, Robert Bossy <Robert.Bossy@jouy.inra.fr>, Claire Nédellec <claire.nedellec@jouy.inra.fr>, Thierry Hamon <thierry.hamon@univ-paris13.fr>
379  
380 =head1 LICENSE
381  
382 Copyright (C) 2012 Wiktoria Golik, Zorana Ratkovic, Robert Bossy, Claire Nédellec and Thierry Hamon
383  
384 This library is free software; you can redistribute it and/or modify
385 it under the same terms as Perl itself, either Perl version 5.8.6 or,
386 at your option, any later version of Perl 5 you may have available.
387  
388  
389 =cut
390  
391 2 our $VERSION='0.1';
392  
393 sub new {
394 2 ▶ my ($class, ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
395  
396 2 my $this = {
397 'stopVerbs' => undef,
398 'stopParticiples' => undef,
399 'stopList' => undef,
400 'patterns' => [],
401 };
402  
403 2 bless $this, $class;
404  
405 2 $this->loadStopVerbs;
406 2 $this->loadStopParticiples;
407 2 $this->loadStopList;
408  
409 2 $this->setPattern1;
410 2 $this->setPattern2;
411 2 $this->setPattern3;
412 2 $this->setPattern4;
413 2 $this->setPattern5;
414 2 $this->setPattern6;
415 2 $this->setPattern7;
416 2 $this->setPattern8;
417 2 $this->setPattern9;
418 2 $this->setPattern10;
419  
420 2 return($this);
421 }
422  
423 sub getStopVerbs {
424 8846 ▶ my ($self, $form)= @_;
Subroutine getStopVerbs: called CC=2 SCAR=7.6
Pod documented
425
426 8846 ▶ if (defined $form) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form) 0 8846 8846
427 0 return($self->{'stopVerbs'}->{$form});
428 }
429  
430 8846 return($self->{'stopVerbs'});
431 }
432  
433 sub existsInStopVerbs {
434 8844 ▶ my ($self, $form)= @_;
Subroutine existsInStopVerbs: called CC=1 SCAR=0.0
Pod documented
435
436 8844 return(exists($self->getStopVerbs->{$form}));
437 }
438  
439  
440 sub loadStopVerbs {
441 2 ▶ my ($self, )= @_;
Subroutine loadStopVerbs: called CC=2 SCAR=7.0
Pod documented
442  
443 2 ▶ if (!defined $self->{'stopVerbs'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"stopVerbs"}) 2 0 2
444 2 $self->{'stopVerbs'} = {
445 'be' => 1,
446 'became' => 1,
447 'bind' => 1,
448 'find' => 1,
449 'gain' => 1,
450 'grow' => 1,
451 'inhibit' => 1,
452 'isolate' => 1,
453 'keep' => 1,
454 'live' => 1,
455 'oxidize' => 1,
456 'see' => 1,
457 'swim' => 1,
458 'synthesize' => 1
459 };
460 }
461  
462 2 return($self->{'stopVerbs'});
463 }
464  
465 sub getStopParticiples {
466 2 ▶ my ($self, $form)= @_;
Subroutine getStopParticiples: called CC=2 SCAR=7.6
Pod documented
467  
468 2 ▶ if (defined $form) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form) 0 2 2
469 0 return($self->{'stopParticiples'}->{$form});
470 }
471  
472 2 return($self->{'stopParticiples'});
473 }
474  
475 sub existsInStopParticiples {
476 0 ▶ my ($self, $form)= @_;
Subroutine existsInStopParticiples: not called CC=1 SCAR=6.9
Pod documented
477  
478 0 return(exists($self->getStopParticiples->{$form}));
479 }
480  
481 sub loadStopParticiples {
482 2 ▶ my ($self, )= @_;
Subroutine loadStopParticiples: called CC=2 SCAR=7.0
Pod documented
483  
484 2 ▶ if (!defined $self->{'stopParticiples'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"stopParticiples"}) 2 0 2
485 2 $self->{'stopParticiples'} = {
486 'attached' => 1,
487 'bound' => 1,
488 'designed' => 1,
489 'exposed' => 1,
490 'intended' => 1,
491 'known' => 1,
492 'related' => 1
493 };
494 }
495 2 return($self->{'stopParticiples'});
496 }
497  
498 sub getStopList {
499 446 ▶ my ($self, $form)= @_;
Subroutine getStopList: called CC=2 SCAR=7.6
Pod documented
500  
501 446 ▶ if (defined $form) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $form) 0 446 446
502 0 return($self->{'stopList'}->{$form});
503 }
504  
505 446 return($self->{'stopList'});
506 }
507  
508 sub existsInStopList {
509 444 ▶ my ($self, $form)= @_;
Subroutine existsInStopList: called CC=1 SCAR=0.0
Pod documented
510  
511 444 return(exists($self->getStopList->{$form}));
512 }
513  
514 sub loadStopList {
515 2 ▶ my ($self, )= @_;
Subroutine loadStopList: called CC=2 SCAR=7.0
Pod documented
516  
517 2 ▶ if (!defined $self->{'stopList'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"stopList"}) 2 0 2
518 2 $self->{'stopList'} = {
519 'being' => 1,
520 'collecting' => 1,
521 'concerning' => 1,
522 'considering' => 1,
523 'containing' => 1,
524 'dividing' => 1,
525 'during' => 1,
526 'enhancing' => 1,
527 'excluding' => 1,
528 'getting' => 1,
529 'having' => 1,
530 'including' => 1,
531 'indicating' => 1,
532 'involving' => 1,
533 'leaving' => 1,
534 'using' => 1
535 };
536 }
537 2 return($self->{'stopList'});
538 }
539  
540 sub compile1 {
541 110 ▶ my ($self, $clause, $result) = @_;
Subroutine compile1: called CC=6 SCAR=17.9
Pod documented
542  
543 110 my $type;
544 110 ▶ if (!(defined $result)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 20 90 110
545 20 $result = {
546 'root' => $clause,
547 'predicates' => [],
548 'namedgroups' => [],
549 're' => ''
550 };
551 }
552  
553 110 $type = $clause->{'type'};
554 110 ▶ if ($type eq 'predicate') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'predicate') { } 60 50 110
elsif ($type eq 'group') { } 50 0 50
555 60 $clause->{'predindex'} = scalar @{$result->{'predicates'}};
556 60 push @{$result->{'predicates'}}, $clause;
557 }
558 elsif ($type eq 'group') {
559 50 ▶ if (exists $clause->{'name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $clause->{"name"}) 20 30 50
560 20 $clause->{'nameindex'} = scalar @{$result->{'namedgroups'}};
561 20 push @{$result->{'namedgroups'}}, $clause->{'name'};
562 }
563 50 for my $child (@{$clause->{'children'}}) {
564 90 $self->compile1($child, $result);
565 }
566 }
567 110 return $result;
568 }
569  
570 # my $TRUE = 'Y';
571  
572 sub _TRUE {
573 44236 ▶ my ($self) = @_;
Subroutine _TRUE: called CC=1 SCAR=0.0
574  
575 44236 return('Y');
576 }
577  
578 # my $TOK = '_';
579  
580 sub _TOK {
581 88500 ▶ my ($self) = @_;
Subroutine _TOK: called CC=1 SCAR=0.0
582  
583 88500 return('_');
584 }
585  
586 # my $FALSE = 'n';
587  
588 sub _FALSE {
589 221144 ▶ my ($self) = @_;
Subroutine _FALSE: called CC=1 SCAR=0.0
590  
591 221144 return('n');
592 }
593  
594 sub compile2 {
595 110 ▶ my ($self, $compiled, $clause) = @_;
Subroutine compile2: called CC=6 SCAR=17.9
Pod documented
596  
597 110 my $type;
598  
599 110 ▶ if (!(defined $clause)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $clause) 20 90 110
600 20 $clause = $compiled->{'root'};
601 20 $compiled->{'re'} = '';
602 }
603  
604 110 $type = $clause->{'type'};
605 110 ▶ if ($type eq 'predicate') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'predicate') { } 60 50 110
elsif ($type eq 'group') { } 50 0 50
606 60 my @preds = @{$compiled->{'predicates'}};
607 60 my $lenm1 = scalar @preds - 1;
608 60 my $index = $clause->{'predindex'};
609 60 $compiled->{'re'} .= $self->_TOK . ('.'x$index) . $self->_TRUE . ('.'x($lenm1 - $index));
610 }
611 elsif ($type eq 'group') {
612 50 ▶ if (exists $clause->{'name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $clause->{'name'}) { } 20 30 50
613 20 $compiled->{'re'} .= '(';
614 }
615 else {
616 30 $compiled->{'re'} .= '(?:';
617 }
618 50 for my $child (@{$clause->{'children'}}) {
619 90 $self->compile2($compiled, $child);
620 }
621 50 $compiled->{'re'} .= ')';
622 }
623 110 $compiled->{'re'} .= $clause->{'quantifier'};
624 }
625  
626 sub compile {
627 20 ▶ my ($self, $pattern) = @_;
Subroutine compile: called CC=1 SCAR=0.0
Pod documented
628  
629 20 my $result = $self->compile1($pattern);
630 20 $self->compile2($result);
631  
632 20 return $result;
633 }
634  
635 sub translate {
636 3580 ▶ my ($self, $compiled, $sequence) = @_;
Subroutine translate: called CC=5 SCAR=16.1
Pod documented
637  
638 3580 my $result = '';
639 # print STDERR "compiled/translate: " . Dumper($compiled) . "\n";
640 3580 ▶ if (defined $compiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compiled) 3580 0 3580
641 3580 my @preds = @{$compiled->{'predicates'}};
642 3580 for my $item (@$sequence) {
643 88440 $result .= $self->_TOK;
644 88440 for my $p (@preds) {
645 265320 ▶ if (&{$p->{'predicate'}}($self,$item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&{$p->{'predicate'};}($self, $item)) { } 44176 221144 265320
646 44176 $result .= $self->_TRUE;
647 }
648 else {
649 221144 $result .= $self->_FALSE;
650 }
651 }
652 }
653 }
654 # warn "Result: $result\n";
655 3580 return $result;
656 }
657  
658 sub match {
659 3580 ▶ my ($self, $compiled, $sequence) = @_;
Subroutine match: called CC=4 SCAR=13.9
Pod documented
660  
661 3580 my @result = ();
662 # print STDERR "compiled/match: " . Dumper($compiled) . "\n";
663  
664 3580 ▶ if (defined $compiled) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $compiled) 3580 0 3580
665 3580 my $translated = $self->translate($compiled, $sequence);
666 3580 my $re = $compiled->{'re'};
667 3580 my $len = 1 + scalar @{$compiled->{'predicates'}};
668 3580 ▶ while ($translated =~ /$re/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translated =~ /$re/g) 295 3580 3875
669 295 my %m = ( '' => [$-[0]/$len, $+[0]/$len] );
670 295 my $i = 1;
671 295 for my $n (@{$compiled->{'namedgroups'}}) {
672 295 my $pos = [ $-[$i]/$len, $+[$i]/$len ];
673 295 $m{$n} = $pos;
674 295 $i++;
675 }
676 295 push @result, \%m;
677 }
678 }
679 # warn "ARRAY(result): " . join("/", @result) . "\n";
680 3580 return \@result;
681 }
682  
683  
684 sub pred {
685 60 ▶ my ($self, $predicate, $quantifier) = @_;
Subroutine pred: called CC=2 SCAR=7.0
Pod documented
686  
687 60 ▶ if (!(defined $quantifier)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $quantifier) 60 0 60
688 60 $quantifier = '';
689 }
690 return {
691 60 'type' => 'predicate',
692 'predicate' => $predicate,
693 'quantifier' => $quantifier
694 };
695 }
696  
697 sub group {
698 30 ▶ my ($self, $children, $quantifier) = @_;
Subroutine group: called CC=2 SCAR=6.9
Pod documented
699  
700 30 ▶ if (!(defined $quantifier)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $quantifier) 20 10 30
701 20 $quantifier = '';
702 }
703 return {
704 30 'type' => 'group',
705 'children' => $children,
706 'quantifier' => $quantifier
707 };
708 }
709  
710 sub named {
711 20 ▶ my ($self, $name, $children, $quantifier) = @_;
Subroutine named: called CC=2 SCAR=7.0
Pod documented
712  
713 20 ▶ if (!(defined $quantifier)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $quantifier) 20 0 20
714 20 $quantifier = '';
715 }
716 return {
717 20 'type' => 'group',
718 'name' => $name,
719 'children' => $children,
720 'quantifier' => $quantifier
721 };
722 }
723  
724 sub is_ing {
725 35376 ▶ my ($self, $w) = @_;
Subroutine is_ing: called CC=2 SCAR=6.9
Pod documented
726  
727 35376 my $form = $w->{'form'};
728 35376 ▶ return ($form =~ /ing$/) && !($self->existsInStopList($form));
Condition100%3/3
$form =~ /ing$/ && !$self->existsInStopList($form)
!ll&&!rl&&r
34932120324
MC/DC100%2/2
$form =~ /ing$/ && !$self->existsInStopList($form)
$form =~ /ing$/ !$self->existsInStopList($form)
Truth table100%3/3
A && not(B)
A= $form =~ /ing$/B= !$self->existsInStopList($form)
ABresult
0X0
100
111
729 }
730  
731 sub patterns {
732 3600 ▶ my ($self) = @_;
Subroutine patterns: called CC=1 SCAR=0.0
Pod undocumented
733  
734 3600 return($self->{'patterns'});
735 }
736  
737  
738 sub setPattern1 {
739 2 ▶ my ($self) = @_;
Subroutine setPattern1: called CC=1 SCAR=0.0
Pod documented
740
741 $self->patterns->[0] = $self->compile(
742 $self->group([
743 8844 ▶ $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'; }),
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%6/6
$pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'
l!l&&r!l&&!r
12133577274
$pos eq "DT" or $pos eq "JJ"
l!l&&r!l&&!r
6525617631
MC/DC100%3/3
$pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'
$pos eq "DT" $pos eq "JJ" $pos eq 'SENT'
Truth table100%4/4
A or B || C
A= $pos eq "DT"B= $pos eq "JJ"C= $pos eq 'SENT'
ABCresult
1XX1
01X1
0011
0000
744 8844 ▶ $self->group([$self->pred(sub { return $_[1]->{'pos'} eq 'JJ'; })], '*'),
Subroutine __ANON__: called CC=1 SCAR=0.0
745 $self->named('ing', [
746 $self->pred(\&is_ing)
747 ]),
748 2 ▶ $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'NN' || $pos eq 'NNS' || $pos eq 'NP' || $pos eq ','; }),
Subroutine __ANON__: called CC=4 SCAR=13.9
Condition100%9/9
$pos eq 'NN' || $pos eq 'NNS' || $pos eq 'NP' || $pos eq ','
l!l&&r!l&&!r
31845185142
$pos eq "NN" or $pos eq "NNS" or $pos eq "NP"
l!l&&r!l&&!r
212310615660
$pos eq "NN" or $pos eq "NNS"
l!l&&r!l&&!r
15315926721
MC/DC100%4/4
$pos eq 'NN' || $pos eq 'NNS' || $pos eq 'NP' || $pos eq ','
$pos eq "NN" $pos eq "NNS" $pos eq "NP" $pos eq ','
Truth table100%5/5
A or B or C || D
A= $pos eq "NN"B= $pos eq "NNS"C= $pos eq "NP"D= $pos eq ','
ABCDresult
1XXX1
01XX1
001X1
00011
00000
749 ])
750 );
751 # warn $self->patterns->[0] . "\n";
752 }
753  
754 sub getPattern1 {
755 358 ▶ my ($self) = @_;
Subroutine getPattern1: called CC=1 SCAR=0.0
Pod documented
756  
757 358 return($self->patterns->[0]);
758 }
759  
760 sub setPattern2 {
761 2 ▶ my ($self) = @_;
Subroutine setPattern2: called CC=1 SCAR=0.0
Pod documented
762
763 $self->patterns->[1] = $self->compile(
764 $self->group([
765 8844 ▶ $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'; }),
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%6/6
$pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'
l!l&&r!l&&!r
12083577279
$pos eq "DT" or $pos eq "JJ"
l!l&&r!l&&!r
6525567636
MC/DC100%3/3
$pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'
$pos eq "DT" $pos eq "JJ" $pos eq 'SENT'
Truth table100%4/4
A or B || C
A= $pos eq "DT"B= $pos eq "JJ"C= $pos eq 'SENT'
ABCresult
1XX1
01X1
0011
0000
766 8844 ▶ $self->group([$self->pred(sub { return $_[1]->{'pos'} eq 'JJ'; })], '*'),
Subroutine __ANON__: called CC=1 SCAR=0.0
767 $self->named('ing', [
768 $self->pred(\&is_ing)
769 ]),
770 2 ▶ $self->pred(sub { return $_[1]->{'pos'} eq 'JJ'; }),
Subroutine __ANON__: called CC=1 SCAR=0.0
771 ])
772 );
773 }
774  
775  
776 sub getPattern2 {
777 358 ▶ my ($self) = @_;
Subroutine getPattern2: called CC=1 SCAR=0.0
Pod documented
778  
779 358 return($self->patterns->[1]);
780 }
781  
782  
783 sub setPattern3 {
784 2 ▶ my ($self) = @_;
Subroutine setPattern3: called CC=1 SCAR=0.0
Pod documented
785
786 $self->patterns->[2] = $self->compile(
787 $self->group([
788 8844 ▶ $self->pred(sub { return $_[1]->{'form'} eq 'of' }),
Subroutine __ANON__: called CC=1 SCAR=0.0
789 $self->named('ing', [ $self->pred(\&is_ing) ]),
790 8844 ▶ $self->pred(sub { return $_[1]->{'pos'} =~ /^V/ || $_[1]->{'form'} eq ',' || $_[1]->{'form'} eq '.' })
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%6/6
$_[1]{'pos'} =~ /^V/ || $_[1]{'form'} eq ',' || $_[1]{'form'} eq '.'
l!l&&r!l&&!r
12873577200
$_[1]{"pos"} =~ /^V/ or $_[1]{"form"} eq ","
l!l&&r!l&&!r
7695187557
MC/DC100%3/3
$_[1]{'pos'} =~ /^V/ || $_[1]{'form'} eq ',' || $_[1]{'form'} eq '.'
$_[1]{"pos"} =~ /^V/ $_[1]{"form"} eq "," $_[1]{'form'} eq '.'
Truth table100%4/4
A or B || C
A= $_[1]{"pos"} =~ /^V/B= $_[1]{"form"} eq ","C= $_[1]{'form'} eq '.'
ABCresult
1XX1
01X1
0011
0000
791 2 ])
792 );
793 }
794  
795 sub getPattern3 {
796 358 ▶ my ($self) = @_;
Subroutine getPattern3: called CC=1 SCAR=0.0
Pod documented
797  
798 358 return($self->patterns->[2]);
799 }
800  
801 sub setPattern4 {
802 2 ▶ my ($self) = @_;
Subroutine setPattern4: called CC=1 SCAR=0.0
Pod documented
803
804 $self->patterns->[3] = $self->compile(
805 $self->group([
806 8844 ▶ $self->pred(sub { $_[1]->{'form'} eq 'of' }),
Subroutine __ANON__: called CC=1 SCAR=0.0
807 $self->named('ing', [ $self->pred(\&is_ing) ]),
808 8844 ▶ $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'DT' || $pos eq 'JJ' || $pos eq 'PP' || $pos eq 'WDT' })
Subroutine __ANON__: called CC=4 SCAR=13.9
Condition100%9/9
$pos eq 'DT' || $pos eq 'JJ' || $pos eq 'PP' || $pos eq 'WDT'
l!l&&r!l&&!r
1233117600
$pos eq "DT" or $pos eq "JJ" or $pos eq "PP"
l!l&&r!l&&!r
1209247611
$pos eq "DT" or $pos eq "JJ"
l!l&&r!l&&!r
6525577635
MC/DC100%4/4
$pos eq 'DT' || $pos eq 'JJ' || $pos eq 'PP' || $pos eq 'WDT'
$pos eq "DT" $pos eq "JJ" $pos eq "PP" $pos eq 'WDT'
Truth table100%5/5
A or B or C || D
A= $pos eq "DT"B= $pos eq "JJ"C= $pos eq "PP"D= $pos eq 'WDT'
ABCDresult
1XXX1
01XX1
001X1
00011
00000
809 2 ])
810 );
811 }
812  
813 sub getPattern4 {
814 358 ▶ my ($self) = @_;
Subroutine getPattern4: called CC=1 SCAR=0.0
Pod documented
815  
816 358 return($self->patterns->[3]);
817 }
818  
819  
820 sub not_sent {
821 17688 ▶ my ($self, $element) = @_;
Subroutine not_sent: called CC=1 SCAR=0.0
Pod documented
822  
823 17688 return $element->{'pos'} ne 'SENT';
824 }
825  
826 sub setPattern5 {
827 2 ▶ my ($self) = @_;
Subroutine setPattern5: called CC=1 SCAR=0.0
Pod documented
828
829 $self->patterns->[4] = $self->compile(
830 $self->group([
831 8844 ▶ $self->pred(sub { return $self->existsInStopVerbs($_[1]->{'lemma'}) }),
Subroutine __ANON__: called CC=1 SCAR=0.0
832 $self->group([$self->pred(\&not_sent)], '*'),
833 2 ▶ $self->named('at', [ $self->pred(sub { return $_[1]->{'form'} eq 'at' }) ])
Subroutine __ANON__: called CC=1 SCAR=0.0
834 ])
835 );
836 }
837  
838 sub getPattern5 {
839 358 ▶ my ($self) = @_;
Subroutine getPattern5: called CC=1 SCAR=0.0
Pod documented
840  
841 358 return($self->patterns->[4]);
842 }
843  
844  
845  
846 sub is_to {
847 35376 ▶ my ($self, $element) = @_;
Subroutine is_to: called CC=1 SCAR=0.0
Pod documented
848  
849 35376 return $element->{'form'} eq 'to';
850 }
851  
852 sub setPattern6 {
853 2 ▶ my ($self) = @_;
Subroutine setPattern6: called CC=1 SCAR=0.0
Pod documented
854
855 $self->patterns->[5] = $self->compile(
856 $self->group([
857 2 ▶ $self->pred(sub { my $form = $_[1]->{'form'}; return $form eq 'from' || $form eq 'by' } ),
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%3/3
$form eq 'from' || $form eq 'by'
l!l&&r!l&&!r
28368780
MC/DC100%2/2
$form eq 'from' || $form eq 'by'
$form eq 'from' $form eq 'by'
Truth table100%3/3
A || B
A= $form eq 'from'B= $form eq 'by'
ABresult
1X1
011
000
858 $self->group([$self->pred(\&not_sent)], '*'),
859 $self->named('to', [ $self->pred(\&is_to) ])
860 ])
861 );
862 }
863  
864 sub getPattern6 {
865 358 ▶ my ($self) = @_;
Subroutine getPattern6: called CC=1 SCAR=0.0
Pod documented
866  
867 358 return($self->patterns->[5]);
868 }
869  
870  
871 sub setPattern7 {
872 2 ▶ my ($self) = @_;
Subroutine setPattern7: called CC=1 SCAR=0.0
Pod documented
873
874 $self->patterns->[6] = $self->compile(
875 $self->group([
876 8844 ▶ $self->pred(sub { return $_[1]->{'pos'} !~ /NN/; }),
Subroutine __ANON__: called CC=1 SCAR=0.0
877 2 ▶ $self->group([$self->pred(sub { return $_[1]->{'pos'} =~ /^V/; })], '*'),
Subroutine __ANON__: called CC=1 SCAR=0.0
878 $self->named('to', [ $self->pred(\&is_to) ])
879 ])
880 );
881 }
882  
883 sub getPattern7 {
884 358 ▶ my ($self) = @_;
Subroutine getPattern7: called CC=1 SCAR=0.0
Pod documented
885  
886 358 return($self->patterns->[6]);
887 }
888  
889  
890 sub setPattern8 {
891 2 ▶ my ($self) = @_;
Subroutine setPattern8: called CC=1 SCAR=0.0
Pod documented
892
893 $self->patterns->[7] = $self->compile(
894 $self->group([
895 2 ▶ $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos =~ /^V/ && $pos ne 'VVN'; } ),
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition66%2/3
$pos =~ /^V/ && $pos ne 'VVN'
!ll&&!rl&&r
80750769
MC/DC50%1/2
$pos =~ /^V/ && $pos ne 'VVN'
$pos =~ /^V/ $pos ne 'VVN'
Truth table66%2/3
A && B
A= $pos =~ /^V/B= $pos ne 'VVN'
ABresult
0X0
100
111
896 $self->named('to', [$self->pred(\&is_to)])
897 ])
898 );
899 }
900  
901 sub getPattern8 {
902 358 ▶ my ($self) = @_;
Subroutine getPattern8: called CC=1 SCAR=0.0
Pod documented
903  
904 358 return($self->patterns->[7]);
905 }
906  
907  
908 sub setPattern9 {
909 2 ▶ my ($self) = @_;
Subroutine setPattern9: called CC=1 SCAR=0.2
Pod documented
910
911 $self->patterns->[8] = $self->compile(
912 $self->group([
913 8844 ▶ $self->pred(sub { return $_[1]->{'pos'} eq 'NN'; }),
Subroutine __ANON__: called CC=1 SCAR=0.0
914 2 ▶ $self->pred(sub { return $_[1]->{'pos'} eq 'VVN' && !($self->existsInStopParticiples($_[1]->{'form'})) }),
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition33%1/3
$_[1]{'pos'} eq 'VVN' && !$self->existsInStopParticiples($_[1]{'form'})
!ll&&!rl&&r
884400
MC/DC0%0/2
$_[1]{'pos'} eq 'VVN' && !$self->existsInStopParticiples($_[1]{'form'})
$_[1]{'pos'} eq 'VVN' !$self->existsInStopParticiples($_[1]{'form'})
Truth table33%1/3
A && not(B)
A= $_[1]{'pos'} eq 'VVN'B= !$self->existsInStopParticiples($_[1]{'form'})
ABresult
0X0
100
111
915 $self->named('to', [$self->pred(\&is_to)])
916 ])
917 );
918 }
919  
920 sub getPattern9 {
921 358 ▶ my ($self) = @_;
Subroutine getPattern9: called CC=1 SCAR=0.0
Pod documented
922  
923 358 return($self->patterns->[8]);
924 }
925  
926  
927 sub setPattern10 {
928 2 ▶ my ($self) = @_;
Subroutine setPattern10: called CC=1 SCAR=0.0
Pod documented
929
930 $self->patterns->[9] = $self->compile(
931 $self->group([
932 8844 ▶ $self->named('ed', [$self->pred(sub { return $_[1]->{'form'} =~ /ed$/; })]),
Subroutine __ANON__: called CC=1 SCAR=0.0
933 8844 ▶ $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'NN' || $pos eq 'NP' || $pos eq 'JJ' || $pos eq 'NNS'; })
Subroutine __ANON__: called CC=4 SCAR=13.9
Condition100%9/9
$pos eq 'NN' || $pos eq 'NP' || $pos eq 'JJ' || $pos eq 'NNS'
l!l&&r!l&&!r
31565925096
$pos eq "NN" or $pos eq "NP" or $pos eq "JJ"
l!l&&r!l&&!r
25995575688
$pos eq "NN" or $pos eq "NP"
l!l&&r!l&&!r
153810616245
MC/DC100%4/4
$pos eq 'NN' || $pos eq 'NP' || $pos eq 'JJ' || $pos eq 'NNS'
$pos eq "NN" $pos eq "NP" $pos eq "JJ" $pos eq 'NNS'
Truth table100%5/5
A or B or C || D
A= $pos eq "NN"B= $pos eq "NP"C= $pos eq "JJ"D= $pos eq 'NNS'
ABCDresult
1XXX1
01XX1
001X1
00011
00000
934 2 ])
935 );
936 }
937  
938 sub getPattern10 {
939 358 ▶ my ($self) = @_;
Subroutine getPattern10: called CC=1 SCAR=0.0
Pod documented
940  
941 358 return($self->patterns->[9]);
942 }
943  
944  
945 sub process_sentence {
946 358 ▶ my ($self, $sentence, $fh) = @_;
Subroutine process_sentence: called CC=13 SCAR=25.8
Pod documented
947  
948 358 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 358 358
949 0 $fh = *STDOUT;
950 }
951  
952 358 my $m;
953 my $w;
954
955 358 for $m (@{$self->match($self->getPattern1, $sentence)}) {
956 9 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'NN';
957 }
958  
959 358 for $m (@{$self->match($self->getPattern2, $sentence)}) {
960 1 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'JJ';
961 }
962  
963 358 for $m (@{$self->match($self->getPattern3, $sentence)}) {
964 0 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'NN';
965 }
966  
967 358 for $m (@{$self->match($self->getPattern4, $sentence)}) {
968 1 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'VVG';
969 }
970  
971 358 for $m (@{$self->match($self->getPattern5, $sentence)}) {
972 56 $sentence->[$m->{'at'}->[0]]->{'pos'} = 'AT';
973 }
974  
975 358 for $m (@{$self->match($self->getPattern6, $sentence)}) {
976 20 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
977 }
978  
979 358 for $m (@{$self->match($self->getPattern7, $sentence)}) {
980 94 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
981 }
982  
983 358 for $m (@{$self->match($self->getPattern8, $sentence)}) {
984 51 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
985 }
986  
987 358 for $m (@{$self->match($self->getPattern9, $sentence)}) {
988 0 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
989 }
990  
991 358 for $m (@{$self->match($self->getPattern10, $sentence)}) {
992 63 $sentence->[$m->{'ed'}->[0]]->{'pos'} = 'JJ';
993 }
994  
995 358 for $w (@$sentence) {
996 8844 print $fh $w->{'form'} . "\t" . $w->{'pos'} . "\t" . $w->{'lemma'} . "\n";
997 }
998 }
999  
1000  
1001 sub process_file {
1002 1 ▶ my ($self, $file, $fhout) = @_;
Subroutine process_file: called CC=8 SCAR=21.0
Pod documented
1003  
1004 1 my $fhin = *STDIN;
1005 1 my $line;
1006 my $form;
1007 1 my $pos;
1008 1 my $lemma;
1009  
1010 1 my @sentence = ();
1011 1 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 1 0 1
1012 1 ▶ open($fhin, $file) or die "No such file $file\"";
Branch: 1 branch
BranchTrueFalseTotal
open $fhin, $file or die qq[No such file $file"] 0 1 1
1013 }
1014 1 ▶ while ($line = <$fhin>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fhin)) 8844 1 8845
1015 # warn ">$line";
1016 8844 $line = &Lingua::YaTeA::Corpus::correctInputLine($line);
1017 # if ($@) {
1018 # warn "no correction of the input lines\n";
1019 # }
1020 8844 chomp $line;
1021 8844 $form = undef;
1022 8844 $pos = undef;
1023 8844 $lemma = undef;
1024 8844 ▶ if ($line !~ /^\s*$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*$/o) 8844 0 8844
1025 8844 ($form, $pos, $lemma) = split("\t", $line);
1026 8844 push @sentence, {'form'=>$form,'pos'=>$pos,'lemma'=>$lemma};
1027 8844 ▶ if ($pos eq 'SENT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos eq "SENT") 357 8487 8844
1028 357 $self->process_sentence(\@sentence, $fhout);
1029 357 @sentence = ();
1030 }
1031 }
1032 }
1033 1 ▶ if (scalar(@sentence) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @sentence > 0) 1 0 1
1034 1 $self->process_sentence(\@sentence, $fhout);
1035 }
1036 1 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 1 0 1
1037 1 close($fhin);
1038 }
1039 1 return(1);
1040 }
1041  
1042 sub _printPatterns {
1043 0 ▶ my ($self, $fh) = @_;
Subroutine _printPatterns: not called CC=3 SCAR=24.8
1044  
1045 0 my $i;
1046  
1047 0 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
1048 0 $fh = *STDOUT;
1049 }
1050  
1051 0 print $fh "Number of patterns: " . scalar(@{$self->patterns}) . "\n";
1052 0 ▶ for($i = 0; $i < scalar(@{$self->patterns}) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->patterns;}) 0 0 0
1053 0 print $fh join(':', %{$self->patterns->[$i]}) . "\n";
1054 0 print $fh Dumper($self->patterns->[$i]) . "\n";
1055 }
1056  
1057 }
1058  
1059 2 1;
↑ Summary Build »