File Coverage

blib/lib/Lingua/BioYaTeA/PreProcessing.pm
Criterion Covered Total %
statement 277 298 92.9
branch 36 56 64.2
condition 27 30 90.0
subroutine 70 72 97.2
pod 43 44 97.7
total 453 500 90.6


line stmt bran cond sub pod time code
1             package Lingua::BioYaTeA::PreProcessing;
2              
3 2     2   54817 use strict;
  2         6  
  2         81  
4 2     2   13 use warnings;
  2         4  
  2         77  
5 2     2   2215 use utf8;
  2         23  
  2         13  
6              
7 2     2   2340 use Lingua::YaTeA::Corpus;
  2         173288  
  2         54  
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 and C 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, the list of stop participles
54             C, the list of stop words C and the list of
55             rewritting patterns C.
56              
57             =head2 getStopVerbs()
58              
59             getStopVerbs($form);
60              
61             This method returns the attribute C 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 attribute).
70              
71             =head2 loadStopVerbs()
72              
73             loadStopVerbs($form);
74              
75             This method loads the list of stop verbs in the attribute
76             C and returns the attribute.
77              
78             =head2 getStopParticiples()
79              
80             getStopParticiples($form);
81              
82             This method returns the attribute C 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 attribute).
91              
92             =head2 loadStopParticiples()
93              
94             loadStopParticiples($form);
95              
96             This method loads the list of stop participles in the attribute
97             C and returns the attribute.
98              
99              
100             =head2 getStopList()
101              
102             getStopList($form);
103              
104             This method returns the attribute C 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 attribute).
113              
114             =head2 loadStopList()
115              
116             loadStopList($form);
117              
118             This method loads the list of stop words in the attribute
119             C 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), the array of predicates (C),
130             the array of named groups (C) 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 and represented in the structure C<$result>. This step
145             generates the regular expression (field C).
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 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"),
181             the function associated to the predicate (field C) and is
182             set with C<$predicate>), and the quantifier associated to the
183             predicate (field C) 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"), the list of predicates (field C) and is set
192             with C<$children>), and the quantifier associated to the child list
193             (field C) 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"), the name associated to the group (field
202             C) which is set with C<$name>, the list of predicates (field
203             C) and is set with C<$children>), and the quantifier
204             associated to the child list (field C) 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 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).
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.
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 , Zorana Ratkovic , Robert Bossy , Claire Nédellec , Thierry Hamon
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             our $VERSION='0.1';
392              
393             sub new {
394 2     2 1 30 my ($class, ) = @_;
395              
396 2         17 my $this = {
397             'stopVerbs' => undef,
398             'stopParticiples' => undef,
399             'stopList' => undef,
400             'patterns' => [],
401             };
402              
403 2         11 bless $this, $class;
404              
405 2         14 $this->loadStopVerbs;
406 2         10 $this->loadStopParticiples;
407 2         9 $this->loadStopList;
408              
409 2         11 $this->setPattern1;
410 2         10 $this->setPattern2;
411 2         10 $this->setPattern3;
412 2         11 $this->setPattern4;
413 2         9 $this->setPattern5;
414 2         11 $this->setPattern6;
415 2         10 $this->setPattern7;
416 2         13 $this->setPattern8;
417 2         11 $this->setPattern9;
418 2         11 $this->setPattern10;
419              
420 2         13 return($this);
421             }
422              
423             sub getStopVerbs {
424 8846     8846 1 13391 my ($self, $form)= @_;
425            
426 8846 50       18501 if (defined $form) {
427 0         0 return($self->{'stopVerbs'}->{$form});
428             }
429              
430 8846         44605 return($self->{'stopVerbs'});
431             }
432              
433             sub existsInStopVerbs {
434 8844     8844 1 21201 my ($self, $form)= @_;
435            
436 8844         17171 return(exists($self->getStopVerbs->{$form}));
437             }
438              
439              
440             sub loadStopVerbs {
441 2     2 1 5 my ($self, )= @_;
442              
443 2 50       21 if (!defined $self->{'stopVerbs'}) {
444 2         36 $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         6 return($self->{'stopVerbs'});
463             }
464              
465             sub getStopParticiples {
466 2     2 1 5 my ($self, $form)= @_;
467              
468 2 50       6 if (defined $form) {
469 0         0 return($self->{'stopParticiples'}->{$form});
470             }
471              
472 2         6 return($self->{'stopParticiples'});
473             }
474              
475             sub existsInStopParticiples {
476 0     0 1 0 my ($self, $form)= @_;
477              
478 0         0 return(exists($self->getStopParticiples->{$form}));
479             }
480              
481             sub loadStopParticiples {
482 2     2 1 6 my ($self, )= @_;
483              
484 2 50       9 if (!defined $self->{'stopParticiples'}) {
485 2         20 $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         5 return($self->{'stopParticiples'});
496             }
497              
498             sub getStopList {
499 446     446 1 690 my ($self, $form)= @_;
500              
501 446 50       1307 if (defined $form) {
502 0         0 return($self->{'stopList'}->{$form});
503             }
504              
505 446         2696 return($self->{'stopList'});
506             }
507              
508             sub existsInStopList {
509 444     444 1 879 my ($self, $form)= @_;
510              
511 444         1029 return(exists($self->getStopList->{$form}));
512             }
513              
514             sub loadStopList {
515 2     2 1 6 my ($self, )= @_;
516              
517 2 50       48 if (!defined $self->{'stopList'}) {
518 2         30 $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         6 return($self->{'stopList'});
538             }
539              
540             sub compile1 {
541 110     110 1 148 my ($self, $clause, $result) = @_;
542              
543 110         113 my $type;
544 110 100       208 if (!(defined $result)) {
545 20         98 $result = {
546             'root' => $clause,
547             'predicates' => [],
548             'namedgroups' => [],
549             're' => ''
550             };
551             }
552              
553 110         150 $type = $clause->{'type'};
554 110 100       239 if ($type eq 'predicate') {
    50          
555 60         59 $clause->{'predindex'} = scalar @{$result->{'predicates'}};
  60         115  
556 60         70 push @{$result->{'predicates'}}, $clause;
  60         114  
557             }
558             elsif ($type eq 'group') {
559 50 100       101 if (exists $clause->{'name'}) {
560 20         24 $clause->{'nameindex'} = scalar @{$result->{'namedgroups'}};
  20         40  
561 20         21 push @{$result->{'namedgroups'}}, $clause->{'name'};
  20         52  
562             }
563 50         73 for my $child (@{$clause->{'children'}}) {
  50         90  
564 90         242 $self->compile1($child, $result);
565             }
566             }
567 110         242 return $result;
568             }
569              
570             # my $TRUE = 'Y';
571              
572             sub _TRUE {
573 44236     44236   53379 my ($self) = @_;
574              
575 44236         134476 return('Y');
576             }
577              
578             # my $TOK = '_';
579              
580             sub _TOK {
581 88500     88500   129363 my ($self) = @_;
582              
583 88500         147758 return('_');
584             }
585              
586             # my $FALSE = 'n';
587              
588             sub _FALSE {
589 221144     221144   329441 my ($self) = @_;
590              
591 221144         623191 return('n');
592             }
593              
594             sub compile2 {
595 110     110 1 473 my ($self, $compiled, $clause) = @_;
596              
597 110         107 my $type;
598              
599 110 100       204 if (!(defined $clause)) {
600 20         28 $clause = $compiled->{'root'};
601 20         27 $compiled->{'re'} = '';
602             }
603              
604 110         148 $type = $clause->{'type'};
605 110 100       231 if ($type eq 'predicate') {
    50          
606 60         58 my @preds = @{$compiled->{'predicates'}};
  60         136  
607 60         92 my $lenm1 = scalar @preds - 1;
608 60         76 my $index = $clause->{'predindex'};
609 60         124 $compiled->{'re'} .= $self->_TOK . ('.'x$index) . $self->_TRUE . ('.'x($lenm1 - $index));
610             }
611             elsif ($type eq 'group') {
612 50 100       101 if (exists $clause->{'name'}) {
613 20         34 $compiled->{'re'} .= '(';
614             }
615             else {
616 30         54 $compiled->{'re'} .= '(?:';
617             }
618 50         51 for my $child (@{$clause->{'children'}}) {
  50         88  
619 90         172 $self->compile2($compiled, $child);
620             }
621 50         85 $compiled->{'re'} .= ')';
622             }
623 110         283 $compiled->{'re'} .= $clause->{'quantifier'};
624             }
625              
626             sub compile {
627 20     20 1 28 my ($self, $pattern) = @_;
628              
629 20         43 my $result = $self->compile1($pattern);
630 20         51 $self->compile2($result);
631              
632 20         69 return $result;
633             }
634              
635             sub translate {
636 3580     3580 1 5460 my ($self, $compiled, $sequence) = @_;
637              
638 3580         4571 my $result = '';
639             # print STDERR "compiled/translate: " . Dumper($compiled) . "\n";
640 3580 50       8092 if (defined $compiled) {
641 3580         3517 my @preds = @{$compiled->{'predicates'}};
  3580         15205  
642 3580         7885 for my $item (@$sequence) {
643 88440         186287 $result .= $self->_TOK;
644 88440         142379 for my $p (@preds) {
645 265320 100       328277 if (&{$p->{'predicate'}}($self,$item)) {
  265320         656712  
646 44176         91925 $result .= $self->_TRUE;
647             }
648             else {
649 221144         438192 $result .= $self->_FALSE;
650             }
651             }
652             }
653             }
654             # warn "Result: $result\n";
655 3580         11042 return $result;
656             }
657              
658             sub match {
659 3580     3580 1 12340 my ($self, $compiled, $sequence) = @_;
660              
661 3580         10846 my @result = ();
662             # print STDERR "compiled/match: " . Dumper($compiled) . "\n";
663              
664 3580 50       8288 if (defined $compiled) {
665 3580         7725 my $translated = $self->translate($compiled, $sequence);
666 3580         9517 my $re = $compiled->{'re'};
667 3580         4941 my $len = 1 + scalar @{$compiled->{'predicates'}};
  3580         9159  
668 3580         78140 while ($translated =~ /$re/g) {
669 295         3133 my %m = ( '' => [$-[0]/$len, $+[0]/$len] );
670 295         712 my $i = 1;
671 295         510 for my $n (@{$compiled->{'namedgroups'}}) {
  295         708  
672 295         7137 my $pos = [ $-[$i]/$len, $+[$i]/$len ];
673 295         1253 $m{$n} = $pos;
674 295         839 $i++;
675             }
676 295         9479 push @result, \%m;
677             }
678             }
679             # warn "ARRAY(result): " . join("/", @result) . "\n";
680 3580         20327 return \@result;
681             }
682              
683              
684             sub pred {
685 60     60 1 91 my ($self, $predicate, $quantifier) = @_;
686              
687 60 50       117 if (!(defined $quantifier)) {
688 60         73 $quantifier = '';
689             }
690             return {
691 60         400 'type' => 'predicate',
692             'predicate' => $predicate,
693             'quantifier' => $quantifier
694             };
695             }
696              
697             sub group {
698 30     30 1 44 my ($self, $children, $quantifier) = @_;
699              
700 30 100       71 if (!(defined $quantifier)) {
701 20         24 $quantifier = '';
702             }
703             return {
704 30         199 'type' => 'group',
705             'children' => $children,
706             'quantifier' => $quantifier
707             };
708             }
709              
710             sub named {
711 20     20 1 35 my ($self, $name, $children, $quantifier) = @_;
712              
713 20 50       44 if (!(defined $quantifier)) {
714 20         25 $quantifier = '';
715             }
716             return {
717 20         183 'type' => 'group',
718             'name' => $name,
719             'children' => $children,
720             'quantifier' => $quantifier
721             };
722             }
723              
724             sub is_ing {
725 35376     35376 1 48511 my ($self, $w) = @_;
726              
727 35376         66388 my $form = $w->{'form'};
728 35376   100     148350 return ($form =~ /ing$/) && !($self->existsInStopList($form));
729             }
730              
731             sub patterns {
732 3600     3600 0 4301 my ($self) = @_;
733              
734 3600         15256 return($self->{'patterns'});
735             }
736              
737              
738             sub setPattern1 {
739 2     2 1 5 my ($self) = @_;
740            
741             $self->patterns->[0] = $self->compile(
742             $self->group([
743 8844   100 8844   16015 $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'; }),
  8844         67812  
744 8844     8844   25590 $self->group([$self->pred(sub { return $_[1]->{'pos'} eq 'JJ'; })], '*'),
745             $self->named('ing', [
746             $self->pred(\&is_ing)
747             ]),
748 2   100 8844   20 $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'NN' || $pos eq 'NNS' || $pos eq 'NP' || $pos eq ','; }),
  8844         15583  
  8844         77765  
749             ])
750             );
751             # warn $self->patterns->[0] . "\n";
752             }
753              
754             sub getPattern1 {
755 358     358 1 677 my ($self) = @_;
756              
757 358         993 return($self->patterns->[0]);
758             }
759              
760             sub setPattern2 {
761 2     2 1 3 my ($self) = @_;
762            
763             $self->patterns->[1] = $self->compile(
764             $self->group([
765 8844   100 8844   28653 $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'DT' || $pos eq 'JJ' || $pos eq 'SENT'; }),
  8844         72113  
766 8844     8844   43632 $self->group([$self->pred(sub { return $_[1]->{'pos'} eq 'JJ'; })], '*'),
767             $self->named('ing', [
768             $self->pred(\&is_ing)
769             ]),
770 2     8844   11 $self->pred(sub { return $_[1]->{'pos'} eq 'JJ'; }),
  8844         25507  
771             ])
772             );
773             }
774              
775              
776             sub getPattern2 {
777 358     358 1 562 my ($self) = @_;
778              
779 358         762 return($self->patterns->[1]);
780             }
781              
782              
783             sub setPattern3 {
784 2     2 1 4 my ($self) = @_;
785            
786             $self->patterns->[2] = $self->compile(
787             $self->group([
788 8844     8844   24940 $self->pred(sub { return $_[1]->{'form'} eq 'of' }),
789             $self->named('ing', [ $self->pred(\&is_ing) ]),
790 8844   100 8844   104020 $self->pred(sub { return $_[1]->{'pos'} =~ /^V/ || $_[1]->{'form'} eq ',' || $_[1]->{'form'} eq '.' })
791 2         10 ])
792             );
793             }
794              
795             sub getPattern3 {
796 358     358 1 586 my ($self) = @_;
797              
798 358         894 return($self->patterns->[2]);
799             }
800              
801             sub setPattern4 {
802 2     2 1 5 my ($self) = @_;
803            
804             $self->patterns->[3] = $self->compile(
805             $self->group([
806 8844     8844   30619 $self->pred(sub { $_[1]->{'form'} eq 'of' }),
807             $self->named('ing', [ $self->pred(\&is_ing) ]),
808 8844   100 8844   25566 $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'DT' || $pos eq 'JJ' || $pos eq 'PP' || $pos eq 'WDT' })
  8844         75697  
809 2         11 ])
810             );
811             }
812              
813             sub getPattern4 {
814 358     358 1 833 my ($self) = @_;
815              
816 358         1137 return($self->patterns->[3]);
817             }
818              
819              
820             sub not_sent {
821 17688     17688 1 27329 my ($self, $element) = @_;
822              
823 17688         61443 return $element->{'pos'} ne 'SENT';
824             }
825              
826             sub setPattern5 {
827 2     2 1 4 my ($self) = @_;
828            
829             $self->patterns->[4] = $self->compile(
830             $self->group([
831 8844     8844   25004 $self->pred(sub { return $self->existsInStopVerbs($_[1]->{'lemma'}) }),
832             $self->group([$self->pred(\¬_sent)], '*'),
833 2     8844   20 $self->named('at', [ $self->pred(sub { return $_[1]->{'form'} eq 'at' }) ])
  8844         24157  
834             ])
835             );
836             }
837              
838             sub getPattern5 {
839 358     358 1 506 my ($self) = @_;
840              
841 358         907 return($self->patterns->[4]);
842             }
843              
844              
845              
846             sub is_to {
847 35376     35376 1 42109 my ($self, $element) = @_;
848              
849 35376         110921 return $element->{'form'} eq 'to';
850             }
851              
852             sub setPattern6 {
853 2     2 1 5 my ($self) = @_;
854            
855             $self->patterns->[5] = $self->compile(
856             $self->group([
857 2   100 8844   19 $self->pred(sub { my $form = $_[1]->{'form'}; return $form eq 'from' || $form eq 'by' } ),
  8844         16834  
  8844         58682  
858             $self->group([$self->pred(\¬_sent)], '*'),
859             $self->named('to', [ $self->pred(\&is_to) ])
860             ])
861             );
862             }
863              
864             sub getPattern6 {
865 358     358 1 686 my ($self) = @_;
866              
867 358         1063 return($self->patterns->[5]);
868             }
869              
870              
871             sub setPattern7 {
872 2     2 1 5 my ($self) = @_;
873            
874             $self->patterns->[6] = $self->compile(
875             $self->group([
876 8844     8844   31340 $self->pred(sub { return $_[1]->{'pos'} !~ /NN/; }),
877 2     8844   13 $self->group([$self->pred(sub { return $_[1]->{'pos'} =~ /^V/; })], '*'),
  8844         30218  
878             $self->named('to', [ $self->pred(\&is_to) ])
879             ])
880             );
881             }
882              
883             sub getPattern7 {
884 358     358 1 587 my ($self) = @_;
885              
886 358         880 return($self->patterns->[6]);
887             }
888              
889              
890             sub setPattern8 {
891 2     2 1 16 my ($self) = @_;
892            
893             $self->patterns->[7] = $self->compile(
894             $self->group([
895 2   66 8844   16 $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos =~ /^V/ && $pos ne 'VVN'; } ),
  8844         17305  
  8844         35578  
896             $self->named('to', [$self->pred(\&is_to)])
897             ])
898             );
899             }
900              
901             sub getPattern8 {
902 358     358 1 731 my ($self) = @_;
903              
904 358         827 return($self->patterns->[7]);
905             }
906              
907              
908             sub setPattern9 {
909 2     2 1 5 my ($self) = @_;
910            
911             $self->patterns->[8] = $self->compile(
912             $self->group([
913 8844     8844   26044 $self->pred(sub { return $_[1]->{'pos'} eq 'NN'; }),
914 2   33 8844   14 $self->pred(sub { return $_[1]->{'pos'} eq 'VVN' && !($self->existsInStopParticiples($_[1]->{'form'})) }),
  8844         38041  
915             $self->named('to', [$self->pred(\&is_to)])
916             ])
917             );
918             }
919              
920             sub getPattern9 {
921 358     358 1 552 my ($self) = @_;
922              
923 358         864 return($self->patterns->[8]);
924             }
925              
926              
927             sub setPattern10 {
928 2     2 1 6 my ($self) = @_;
929            
930             $self->patterns->[9] = $self->compile(
931             $self->group([
932 8844     8844   29447 $self->named('ed', [$self->pred(sub { return $_[1]->{'form'} =~ /ed$/; })]),
933 8844   100 8844   14609 $self->pred(sub { my $pos = $_[1]->{'pos'}; return $pos eq 'NN' || $pos eq 'NP' || $pos eq 'JJ' || $pos eq 'NNS'; })
  8844         74155  
934 2         14 ])
935             );
936             }
937              
938             sub getPattern10 {
939 358     358 1 1738 my ($self) = @_;
940              
941 358         867 return($self->patterns->[9]);
942             }
943              
944              
945             sub process_sentence {
946 358     358 1 652 my ($self, $sentence, $fh) = @_;
947              
948 358 50       990 if (!defined $fh) {
949 0         0 $fh = *STDOUT;
950             }
951              
952 358         450 my $m;
953             my $w;
954            
955 358         528 for $m (@{$self->match($self->getPattern1, $sentence)}) {
  358         1480  
956 9         45 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'NN';
957             }
958              
959 358         5728 for $m (@{$self->match($self->getPattern2, $sentence)}) {
  358         1284  
960 1         6 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'JJ';
961             }
962              
963 358         625 for $m (@{$self->match($self->getPattern3, $sentence)}) {
  358         1518  
964 0         0 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'NN';
965             }
966              
967 358         934 for $m (@{$self->match($self->getPattern4, $sentence)}) {
  358         1621  
968 1         7 $sentence->[$m->{'ing'}->[0]]->{'pos'} = 'VVG';
969             }
970              
971 358         722 for $m (@{$self->match($self->getPattern5, $sentence)}) {
  358         1265  
972 56         392 $sentence->[$m->{'at'}->[0]]->{'pos'} = 'AT';
973             }
974              
975 358         859 for $m (@{$self->match($self->getPattern6, $sentence)}) {
  358         1545  
976 20         131 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
977             }
978              
979 358         687 for $m (@{$self->match($self->getPattern7, $sentence)}) {
  358         1445  
980 94         790 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
981             }
982              
983 358         1138 for $m (@{$self->match($self->getPattern8, $sentence)}) {
  358         1588  
984 51         205 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
985             }
986              
987 358         944 for $m (@{$self->match($self->getPattern9, $sentence)}) {
  358         7390  
988 0         0 $sentence->[$m->{'to'}->[0]]->{'pos'} = 'XXX';
989             }
990              
991 358         781 for $m (@{$self->match($self->getPattern10, $sentence)}) {
  358         1396  
992 63         416 $sentence->[$m->{'ed'}->[0]]->{'pos'} = 'JJ';
993             }
994              
995 358         947 for $w (@$sentence) {
996 8844         28776 print $fh $w->{'form'} . "\t" . $w->{'pos'} . "\t" . $w->{'lemma'} . "\n";
997             }
998             }
999              
1000              
1001             sub process_file {
1002 1     1 1 984 my ($self, $file, $fhout) = @_;
1003              
1004 1         5 my $fhin = *STDIN;
1005 1         2 my $line;
1006             my $form;
1007 0         0 my $pos;
1008 0         0 my $lemma;
1009              
1010 1         2 my @sentence = ();
1011 1 50       5 if (defined $file) {
1012 1 50       53 open($fhin, $file) or die "No such file $file\"";
1013             }
1014 1         39 while ($line = <$fhin>) {
1015             # warn ">$line";
1016 8844         23113 $line = &Lingua::YaTeA::Corpus::correctInputLine($line);
1017             # if ($@) {
1018             # warn "no correction of the input lines\n";
1019             # }
1020 8844         162142 chomp $line;
1021 8844         9710 $form = undef;
1022 8844         8785 $pos = undef;
1023 8844         11228 $lemma = undef;
1024 8844 50       24390 if ($line !~ /^\s*$/o) {
1025 8844         37878 ($form, $pos, $lemma) = split("\t", $line);
1026 8844         32095 push @sentence, {'form'=>$form,'pos'=>$pos,'lemma'=>$lemma};
1027 8844 100       43993 if ($pos eq 'SENT') {
1028 357         1325 $self->process_sentence(\@sentence, $fhout);
1029 357         26483 @sentence = ();
1030             }
1031             }
1032             }
1033 1 50       6 if (scalar(@sentence) > 0) {
1034 1         4 $self->process_sentence(\@sentence, $fhout);
1035             }
1036 1 50       8 if (defined $file) {
1037 1         56 close($fhin);
1038             }
1039 1         23 return(1);
1040             }
1041              
1042             sub _printPatterns {
1043 0     0     my ($self, $fh) = @_;
1044              
1045 0           my $i;
1046              
1047 0 0         if (!defined $fh) {
1048 0           $fh = *STDOUT;
1049             }
1050              
1051 0           print $fh "Number of patterns: " . scalar(@{$self->patterns}) . "\n";
  0            
1052 0           for($i = 0; $i < scalar(@{$self->patterns}) ; $i++) {
  0            
1053 0           print $fh join(':', %{$self->patterns->[$i]}) . "\n";
  0            
1054 0           print $fh Dumper($self->patterns->[$i]) . "\n";
1055             }
1056              
1057             }
1058              
1059             1;