File Coverage

blib/lib/Lingua/YaTeA/MultiWordUnit.pm
Criterion Covered Total %
statement 197 246 80.0
branch 32 60 53.3
condition 7 15 46.6
subroutine 27 31 87.1
pod 23 24 95.8
total 286 376 76.0


line stmt bran cond sub pod time code
1             package Lingua::YaTeA::MultiWordUnit;
2 5     5   50 use strict;
  5         13  
  5         148  
3 5     5   26 use warnings;
  5         12  
  5         150  
4 5     5   60 use NEXT;
  5         11  
  5         28  
5 5     5   140 use Data::Dumper;
  5         10  
  5         213  
6 5     5   28 use UNIVERSAL;
  5         10  
  5         21  
7 5     5   180 use Scalar::Util qw(blessed);
  5         11  
  5         342  
8 5     5   39 use Lingua::YaTeA::MultiWordPhrase;
  5         16  
  5         59  
9              
10             our $VERSION=$Lingua::YaTeA::VERSION;
11              
12             sub new
13             {
14 81     81 1 9399 my ($class_or_object,$num_content_words,$words_a) = @_;
15 81         139 my $this = shift;
16 81 50       230 $this = bless {}, $this unless ref $this;
17 81         210 $this->{FOREST} = ();
18 81         150 $this->{CONTENT_WORDS} = $num_content_words;
19 81         149 $this->{PARSING_METHOD} = ();
20 81         369 $this->{LENGTH} = scalar @$words_a;
21 81         1004 $this->NEXT::new(@_);
22 81         8684 return $this;
23             }
24              
25              
26              
27             sub getContentWordNumber
28             {
29 0     0 1 0 my ($this) = @_;
30 0         0 return $this->{CONTENT_WORDS};
31             }
32              
33             sub getLength
34             {
35 224     224 1 365 my ($this) = @_;
36 224         581 return $this->{LENGTH};
37             }
38              
39             sub addTree
40             {
41 103     103 1 248 my ($this,$tree) = @_;
42 103         188 push @{$this->{FOREST}}, $tree;
  103         411  
43             }
44              
45             sub getForest
46             {
47 432     432 1 832 my ($this) = @_;
48 432         1518 return $this->{FOREST};
49             }
50              
51             sub forestSize
52             {
53 1     1 1 3 my ($this) = @_;
54 1         14 return scalar @{$this->getForest};
  1         5  
55             }
56              
57             sub emptyForest
58             {
59 8     8 1 25 my ($this) = @_;
60 8         22 @{$this->getForest} = ();
  8         19  
61             }
62              
63             sub getTree
64             {
65 70     70 1 140 my ($this,$index) = @_;
66 70         164 return $this->getForest->[$index];
67             }
68              
69              
70             sub exportNodeSets
71             {
72 9     9 1 27 my ($this) = @_;
73 9         22 my $tree;
74             my @node_sets;
75 9         34 foreach $tree (@{$this->getForest})
  9         35  
76             {
77 9         59 push @node_sets, $tree->getNodeSet->copy;
78             }
79 9         35 return \@node_sets;
80             }
81              
82              
83             sub searchParsingPattern
84             {
85 73     73 1 182 my ($this,$parsing_pattern_set,$tag_set,$parsing_direction) = @_;
86 73         179 my $record;
87             my $simplified_pos;
88 73         0 my $tree;
89              
90             # if ($this->{CONTENT_WORDS} <= $Lingua::YaTeA::ParsingPatternRecordSet::max_content_words) # commented by SA: limite l'analyse le terme a deja ete simplifie par un ilot
91             # {
92            
93 73 100       213 if(!defined $this->getForest)
94             {
95 70 100       237 if ($record = $parsing_pattern_set->existRecord($this->getPOS))
96             {
97 45         173 return $this->getParseFromPattern($record,$tag_set);
98             }
99 25         95 return;
100             }
101             # exogenous islands were found (used for Phrases only)
102             else
103             {
104 3         5 foreach $tree (@{$this->getForest})
  3         10  
105             {
106 3         10 $simplified_pos = $tree->getSimplifiedIndexSet->buildPOSSequence($this->getWords,$tag_set);
107            
108 3 100       16 if ($record = $parsing_pattern_set->existRecord($simplified_pos))
109             {
110 1         7 return $this->getParseFromPattern($record,$parsing_direction,$tag_set);
111             }
112             }
113             }
114             #}
115 2         7 return;
116             }
117              
118              
119             sub getParseFromPattern
120             {
121 46     46 1 121 my ($this,$pattern_record,$parsing_direction,$tag_set) = @_;
122 46         164 my $pattern;
123             my $node_set;
124 46         0 my $tree;
125 46         0 my @concurrent_trees;
126 46         75 my $parsed = 0;
127            
128 46 100 66     149 if(
129             (defined $this->{FOREST})
130             &&
131             ($this->forestSize > 0)
132             )
133             {
134 1         10 $pattern = $this->chooseBestPattern($pattern_record->{PARSING_PATTERNS},$parsing_direction);
135 1         4 foreach $tree (@{$this->{FOREST}})
  1         3  
136             {
137 1         6 $node_set = $pattern->getNodeSet->copy;
138 1         5 $node_set->fillNodeLeaves($tree->getSimplifiedIndexSet);
139              
140 1 50       3 if($tree->append($node_set,$tree->getSimplifiedIndexSet,\@concurrent_trees,$this->getWords,$tag_set) == 1)
141             {
142 1         5 $tree->setHead;
143 1         6 $parsed = 1;
144             }
145             }
146             }
147             else
148             {
149 45         105 foreach $pattern (@{$pattern_record->{PARSING_PATTERNS}})
  45         149  
150             {
151 45         170 $tree = Lingua::YaTeA::Tree->new;
152 45         130 $tree->{INDEX_SET} = $this->getIndexSet;
153 45         180 $tree->{NODE_SET} = $pattern->getNodeSet->copy;
154 45         189 $tree->fillNodeLeaves;
155            
156 45 50       164 if($tree->check($this))
157             {
158 45         161 $tree->setReliability(1);
159 45         131 $tree->setHead;
160 45         184 $this->addTree($tree);
161 45         122 $parsed = 1;
162             }
163             }
164             }
165 46         205 return $parsed;
166             }
167              
168              
169              
170             sub getPartialPattern
171             {
172 66     66 1 208 my ($this,$simplified_index_set,$tag_set,$parsing_direction,$parsing_pattern_set,$fh) = @_;
173 66         114 my $pattern;
174             my $position;
175 66         221 my $POS = $simplified_index_set->buildPOSSequence($this->getWords,$tag_set);
176             # print $fh "pos: ". $POS . "\n";
177 66 50       186 if($parsing_direction eq "LEFT")
178             {
179 0         0 ($pattern,$position) = $this->getPatternsLeftFirst($POS,$parsing_pattern_set,$parsing_direction);
180             }
181             else{
182 66         213 ($pattern,$position) = $this->getPatternsRightFirst($POS,$parsing_pattern_set,$parsing_direction);
183             }
184 66         201 return ($pattern,$position);
185             }
186              
187              
188             sub getPatternsLeftFirst
189             {
190 0     0 1 0 my ($this,$POS,$parsing_pattern_set,$parsing_direction) = @_;
191 0         0 my $pattern;
192 0         0 my $position = "LEFT";
193 0         0 $pattern = $this->getPatternOnTheLeft($POS,$parsing_pattern_set,$parsing_direction);
194 0 0 0     0 if (!((blessed($pattern)) && ($pattern->isa('Lingua::YaTeA::ParsingPattern')))) {
195 0         0 $pattern = $this->getPatternOnTheRight($POS,$parsing_pattern_set,$parsing_direction);
196 0         0 $position = "RIGHT";
197             }
198 0         0 return ($pattern,$position);
199             }
200              
201             sub getPatternsRightFirst
202             {
203 66     66 1 196 my ($this,$POS,$parsing_pattern_set,$parsing_direction) = @_;
204 66         107 my $pattern;
205 66         126 my $position = "RIGHT";
206            
207 66         208 $pattern = $this->getPatternOnTheRight($POS,$parsing_pattern_set,$parsing_direction);
208 66 100 66     637 if (!((blessed($pattern)) && ($pattern->isa('Lingua::YaTeA::ParsingPattern'))))
209             {
210 2         30 $pattern = $this->getPatternOnTheLeft($POS,$parsing_pattern_set,$parsing_direction);
211 2         6 $position = "LEFT";
212             }
213 66         251 return ($pattern,$position);
214             }
215              
216             sub getPatternOnTheLeft
217             {
218 2     2 1 12 my ($this,$POS,$parsing_pattern_set,$parsing_direction) = @_;
219 2         18 my @selection;
220             my $key;
221 2         0 my $record;
222 2         0 my $pattern;
223 2         0 my $bounded_key;
224 2         0 my $qm_key;
225 2         8 my $bounded_POS = "-" . $POS . "-";
226 2         14 $bounded_POS =~ s/ /-/g;
227 2         7 while (($key,$record) = each %{$parsing_pattern_set->getRecordSet})
  142         354  
228             {
229 140         292 $bounded_key = "-" . $key . "-";
230 140         426 $bounded_key =~ s/ /-/g;
231 140         262 $qm_key = quotemeta($bounded_key);
232 140 50       1282 if ($bounded_POS =~ /^$qm_key/)
233             {
234 0         0 foreach $pattern (@{$record->getPatterns})
  0         0  
235             {
236 0         0 push @selection, $pattern;
237             }
238             }
239             }
240 2         12 $pattern = $this->chooseBestPattern(\@selection,$parsing_direction);
241 2         6 return $pattern;
242             }
243              
244             sub getPatternOnTheRight
245             {
246 66     66 1 184 my ($this,$POS,$parsing_pattern_set,$parsing_direction) = @_;
247 66         335 my @selection;
248             my $key;
249 66         0 my $record;
250 66         0 my $pattern;
251 66         0 my $bounded_key;
252 66         0 my $qm_key;
253            
254 66         168 my $bounded_POS = "-" . $POS . "-";
255 66         417 $bounded_POS =~ s/ /-/g;
256 66         161 while (($key,$record) = each %{$parsing_pattern_set->getRecordSet})
  4686         11700  
257             {
258 4620         9497 $bounded_key = "-" . $key . "-";
259 4620         12538 $bounded_key =~ s/ /-/g;
260 4620         8652 $qm_key = quotemeta($bounded_key);
261 4620 100       40753 if ($bounded_POS =~ /$qm_key$/)
262             {
263 66         132 foreach $pattern (@{$record->getPatterns})
  66         303  
264             {
265 66         203 push @selection, $pattern;
266             }
267             }
268             }
269 66         307 $pattern = $this->chooseBestPattern(\@selection,$parsing_direction);
270 66         191 return $pattern;
271             }
272              
273              
274              
275              
276             sub chooseBestPattern
277             {
278 69     69 1 189 my ($this,$patterns_a,$parsing_direction) = @_;
279            
280 69         189 my @tmp = sort {$this->sortPatternsByPriority($a,$b,$parsing_direction)} @$patterns_a;
  2         29  
281            
282 69         127 my @sorted = @tmp;
283              
284 69         172 return $sorted[0];
285             }
286              
287             sub sortPatternsByPriority
288             {
289 2     2 1 9 my ($this,$first,$second,$parsing_direction) = @_;
290              
291 2 50       13 if($first->getDirection eq $parsing_direction)
292             {
293 2 50       10 if($second->getDirection eq $parsing_direction)
294             {
295 2 50       10 if($first->getNumContentWords > $second->getNumContentWords)
296             {
297 2         15 return -1;
298             }
299             else
300             {
301 0 0       0 if($first->getNumContentWords < $second->getNumContentWords)
302             {
303 0         0 return 1;
304             }
305             else
306             {
307 0         0 return ($second->getPriority <=> $first->getPriority);
308             }
309             }
310             }
311             else
312             {
313 0         0 return -1;
314             }
315             }
316             else
317             {
318 0 0       0 if($second->getDirection eq $parsing_direction)
319             {
320 0         0 return 1;
321             }
322             else
323             {
324 0 0       0 if($first->getNumContentWords > $second->getNumContentWords)
325             {
326 0         0 return -1;
327             }
328             else
329             {
330 0 0       0 if($first->getNumContentWords < $second->getNumContentWords)
331             {
332 0         0 return 1;
333             }
334             else
335             {
336 0         0 return ($second->getPriority <=> $first->getPriority);
337             }
338             }
339             }
340             }
341             }
342              
343              
344             sub setParsingMethod
345             {
346 71     71 1 166 my ($this,$method) = @_;
347 71 50 33     509 if ((blessed($this)) && ($this->isa('Lingua::YaTeA::Phrase')))
348             {
349 71         150 $Lingua::YaTeA::MultiWordPhrase::parsed++;
350             }
351 71         221 $this->{PARSING_METHOD} = $method;
352             }
353              
354              
355             sub getParsingMethod
356             {
357 48     48 1 85 my ($this) = @_;
358 48 50       129 if(defined $this->{PARSING_METHOD})
359             {
360 48         157 return $this->{PARSING_METHOD};
361             }
362             else
363             {
364 0         0 return "UNPARSED";
365             }
366             }
367              
368              
369             sub parseProgressively
370             {
371 27     27 1 87 my ($this,$tag_set,$parsing_direction,$parsing_pattern_set, $fh) = @_;
372 27         152 my $tree;
373             my $pattern;
374 27         0 my $position;
375 27         0 my $partial_index_set;
376 27         0 my $node_set;
377 27         0 my @concurrent_trees;
378 27         59 my $parsed = 0;
379            
380             # print $fh "parseProgressively\n";
381             # print $fh $this->getIF . "\n";
382              
383 27 100       66 if(!defined $this->getForest)
384             {
385 19         98 $tree = Lingua::YaTeA::Tree->new;
386 19         76 $tree->setSimplifiedIndexSet($this->getIndexSet);
387 19         49 push @concurrent_trees, $tree;
388             }
389             else
390             {
391 8         18 @concurrent_trees = @{$this->getForest};
  8         29  
392             # print $fh scalar @concurrent_trees . " arbres\n";
393 8         56 $this->emptyForest;
394             }
395 27         95 while (scalar @concurrent_trees != 0)
396             {
397 106         289 foreach ($tree = pop (@concurrent_trees))
398             {
399             # print $fh "TREE parse\n";
400             # $tree->print($this->getWords,$fh);
401 106 100       288 if($tree->getSimplifiedIndexSet->getSize == 1)
402             {
403 40 50       160 if($tree->check($this))
404             {
405 40         84 $parsed = 1;
406 40         141 $tree->setHead;
407 40         142 $tree->setReliability(2);
408 40         124 $this->addTree($tree);
409             }
410             }
411             else
412             {
413             # $tree->getSimplifiedIndexSet->print($fh);
414             # print $fh "\n";
415 66         190 ($pattern,$position) = $this->getPartialPattern($tree->getSimplifiedIndexSet,$tag_set,$parsing_direction,$parsing_pattern_set,$fh);
416             # $tree->getSimplifiedIndexSet->print($fh);
417 66 100 66     424 if ((blessed($pattern)) && ($pattern->isa('Lingua::YaTeA::ParsingPattern'))) {
418             # print $fh "trouve partial pattern\n";
419 64         217 $partial_index_set = $tree->getSimplifiedIndexSet->getPartial($pattern->getLength,$position);
420 64         231 $node_set = $pattern->getNodeSet->copy;
421 64         218 $node_set->fillNodeLeaves($partial_index_set);
422            
423 64 50       285 if ($tree->append($node_set,$partial_index_set,\@concurrent_trees,$this->getWords,$tag_set,$fh) == -1) {
424             # print $fh "termine append avec -1\n";
425 0         0 return 0;
426             }
427             }
428             else
429             {
430 2         12 next;
431             }
432             }
433             }
434             }
435             #$this->printDebug($fh);
436 27         188 return $parsed;
437             }
438              
439              
440              
441              
442             sub printForest
443             {
444 0     0 1 0 my ($this,$fh) = @_;
445 0         0 my $tree;
446             # print "FOREST\n";
447 0 0       0 if(defined $this->getForest)
448             {
449             #print "Taille de la foret: " . $this->forestSize . "\n";
450 0         0 foreach $tree (@{$this->getForest})
  0         0  
451             {
452 0         0 $tree->print($this->getWords,$fh);
453             }
454             }
455             else
456             {
457 0         0 print "Pas d'analyse\n";
458             }
459             }
460              
461              
462             sub printForestParenthesised
463             {
464 45     45 1 81 my ($this,$fh) = @_;
465 45         67 my $tree;
466 45         71 my $tree_counter = 1;
467            
468 45 50       86 if(defined $fh)
469             {
470 45 50       94 if(defined $this->getForest)
471             {
472 45         69 print $fh " number of trees: " . scalar @{$this->getForest} . "\n";
  45         82  
473 45         71 foreach $tree (@{$this->getForest})
  45         78  
474             {
475             # print STDERR "$tree\n";
476 45         135 print $fh "\tT" . $tree_counter++ .": ";
477 45         107 $tree->printParenthesised($this->getWords,$fh);
478             }
479             }
480             else
481             {
482 0           print $fh "Pas d'analyse\n";
483             }
484             }
485             else
486             {
487 0 0         if(defined $this->getForest)
488             {
489 0           print " : number of trees" . scalar @{$this->getForest} . "\n";
  0            
490 0           foreach $tree (@{$this->getForest})
  0            
491             {
492 0           $tree->printParenthesised($this->getWords);
493             }
494             }
495             else
496             {
497 0           print "Pas d'analyse\n";
498             }
499             }
500             }
501              
502             sub printDebug
503             {
504 0     0 0   my ($this, $fh) = @_;
505              
506 0           print $fh "\n\n";
507 0           print $fh "$this\n";
508 0           print $fh $this->{'IF'} . "\n";
509 0           $this->print($fh);
510 0           $this->printForestParenthesised($fh);
511 0           print $fh "\n\n";
512              
513             }
514              
515              
516             1;
517              
518             __END__