MSTperl/Labeller.pm

filter: Statementstmt 55.0% 115 / 209 Branchbran 38.4% 30 / 78 Conditioncond 20.8% 10 / 48 MC/DCmcdc 4.5% 1 / 22 Subroutinesub 81.2% 13 / 16 Podpod 81.8% 9 / 11 totaltotal 46.3% 178 / 384 CC 66 SCAR 46.0
CC
66
Cov
46%
get_possible_labels
228.6
get_possible_labels_internal
182.0
label_edge
32.7
CRAP 99.4
« MSTperl/ModelLabelling.pm ↑ Summary MSTperl/TrainerLabelling.pm »
1 package Treex::Tool::Parser::MSTperl::Labeller;
2 {
3 4 $Treex::Tool::Parser::MSTperl::Labeller::VERSION = '0.11949';
4 }
5  
6 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Treex::Tool::Parser::MSTperl::Sentence;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Treex::Tool::Parser::MSTperl::Edge;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Treex::Tool::Parser::MSTperl::ModelLabelling;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 has config => (
14 isa => 'Treex::Tool::Parser::MSTperl::Config',
15 is => 'ro',
16 required => '1',
17 );
18  
19 4 has model => (
20 isa => 'Maybe[Treex::Tool::Parser::MSTperl::ModelLabelling]',
21 is => 'rw',
22 );
23  
24 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
25 2 my ($self) = @_;
26  
27 2 $self->model(
28 Treex::Tool::Parser::MSTperl::ModelLabelling->new(
29 config => $self->config,
30 )
31 );
32  
33 2 return;
34 }
35  
36 1 ▶sub load_model {
Subroutine load_model: called CC=1 SCAR=0.0
Pod documented
37  
38 # (Str $filename)
39 1 my ( $self, $filename ) = @_;
40  
41 1 return $self->model->load($filename);
42 }
43  
44 0 ▶sub load_model_tsv {
Subroutine load_model_tsv: not called CC=1 SCAR=6.9
Pod documented
45  
46 # (Str $filename)
47 0 my ( $self, $filename ) = @_;
48  
49 0 return $self->model->load_tsv($filename);
50 }
51  
52 0 ▶sub label_sentence {
Subroutine label_sentence: not called CC=1 SCAR=6.9
Pod documented
53  
54 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
55 0 my ( $self, $sentence ) = @_;
56  
57 # parse sentence (does not modify $sentence)
58 0 my $sentence_labelled = $self->label_sentence_internal($sentence);
59  
60 0 return $sentence_labelled->toLabelsArray();
61 }
62  
63 17 ▶sub label_sentence_internal {
Subroutine label_sentence_internal: called CC=3 SCAR=12.5
Pod documented
64  
65 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
66 17 my ( $self, $sentence ) = @_;
67  
68 17 ▶ if ( !$self->model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->model) 0 17 17
69 0 croak "MSTperl parser error: There is no model for labelling!";
70 }
71  
72 # copy the sentence (do not modify $sentence directly)
73 17 my $sentence_working_copy = $sentence->copy_nonlabelled();
74 17 $sentence_working_copy->fill_fields_before_labelling();
75  
76 17 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 17 17
77 print "Labelling sentence: "
78 0 . ( join ' ', map { $_->fields->[1] } @{ $sentence->nodes } )
79 . " \n";
80 }
81  
82 # take root's children, find best scoring labelling sequence, recursion
83 17 my $root = $sentence_working_copy->getNodeByOrd(0);
84 17 $self->label_subtree($root);
85  
86 17 return $sentence_working_copy;
87 }
88  
89 # assign labels to edges going from the $parent node to its children
90 # (and recurse on the children)
91 # directly modifies the sentence (or better: the nodes within the sentence)
92 248 ▶sub label_subtree {
Subroutine label_subtree: called CC=12 SCAR=27.3
Pod documented
93  
94 # (Treex::Tool::Parser::MSTperl::Node $parent)
95 248 my ( $self, $parent ) = @_;
96  
97 248 my $ALGORITHM = $self->config->labeller_algorithm;
98  
99 248 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 248 248
100 0 print "Label subtree of node number " . $parent->ord . ' '
101 . $parent->fields->[1] . "\n";
102 }
103  
104 248 my @edges = @{ $parent->children };
105 248 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 248 248
106 0 print "There are " . scalar(@edges) . " children edges \n";
107 }
108  
109 248 ▶ if ( @edges == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@edges == 0) 120 128 248
110 120 return;
111 }
112  
113 # label the nodes using Viterbi algorithm
114 # (this is my own implementation of Viterbi, fitted for this task)
115  
116 # States structure: for each state (its key being a label)
117 # there is an array with the current best path to it as 'path' (append-only)
118 # and its current score 'score' (computed somehow,
119 # i.e. differently for different algorithms)
120 128 my $states = {};
121 128 my $starting_state_key = $self->config->SEQUENCE_BOUNDARY_LABEL;
122  
123 # correspond to algorithms
124 128 my @starting_scores = (
125  
126 # 0 1 2 3 4 5 6 7 8 9
127 1e300, 1, 1, 1e300, 1e300, 1e300, -1, -1, 0, 0,
128  
129 # 10 11 12 13 14 15 16 17 18 19 20 21
130 1e300, 1e300, 1e300, 1e300, 1e300, 1e300, 0, 0, 0, 1, 0, 0,
131 );
132  
133 # path could be constructed by backpointers
134 # but one would have to keep all the states the whole time
135 128 $states->{$starting_state_key} = {
136 'path' => [ $self->config->SEQUENCE_BOUNDARY_LABEL ],
137 'score' => $starting_scores[$ALGORITHM],
138 };
139  
140 # run Viterbi
141 # In each cycle generates %new_states and sets them as %states,
142 # so at the end it suffices to find the state with the best score in %states
143 # and use its path as the result.
144 128 my $prev_edge = undef;
145 128 foreach my $edge (@edges) {
146  
147 # only progress and/or debug info
148 231 ▶ if ( $self->config->DEBUG >= 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 3) 0 231 231
149 0 print " Labelling edge to node "
150 . $edge->child->ord . ' ' . $edge->child->fields->[1] . "\n";
151 0 print " Currently there are "
152 . ( keys %$states ) . " states\n";
153 }
154  
155 # do one Viterbi step - assign possible labels to $edge
156 # (including appropriate scores of course)
157 231 $states = $self->label_edge( $edge, $states, $prev_edge );
158  
159 231 ▶ if ( $ALGORITHM == 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 20) 0 231 231
160  
161 # set the best label
162 0 my $best_state_label = $self->find_best_state_label($states);
163 0 $edge->child->label($best_state_label);
164 }
165  
166 231 $prev_edge = $edge;
167 }
168  
169 # TODO: foreach last state multiply its score
170 # by the label->sequence_boundary probability
171  
172 128 ▶ if ( $ALGORITHM != 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM != 20) 128 0 128
173  
174 # End - find the state with the best score - this is the result
175 128 my $best_state_label = $self->find_best_state_label($states);
176  
177 128 ▶ if ($best_state_label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($best_state_label) { } 128 0 128
178  
179 128 my @labels = @{ $states->{$best_state_label}->{'path'} };
180  
181 # get rid of SEQUENCE_BOUNDARY_LABEL
182 128 shift @labels;
183  
184 # only progress and/or debug info
185 128 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 128 128
186 0 print "best state $best_state_label score: " . "\n";
187 0 print "best path: "
188 . ( join ' ', @labels )
189 . "\n";
190 }
191  
192 128 foreach my $edge (@edges) {
193 231 my $label = shift @labels;
194 231 $edge->child->label($label)
195 }
196  
197 } else {
198  
199 # TODO do not die, provide some backoff instead
200 # (do some smoothing, at least when no states are generated)
201 0 print "No best state generated, cannot label the sentence!"
202 . " (This is weird.)\n";
203 }
204 } # else: edges are labelled in each step
205  
206 # end of Viterbi
207  
208 # recursion
209 128 foreach my $edge (@edges) {
210 231 $self->label_subtree( $edge->child );
211 }
212  
213 128 return;
214 }
215  
216 128 ▶sub find_best_state_label {
Subroutine find_best_state_label: called CC=5 SCAR=17.1
Pod undocumented
217  
218 128 my ( $self, $states ) = @_;
219  
220 # "negative infinity" (works both with real probs and with their logs)
221 128 my $best_state_score = -999999999;
222 128 my $best_state_label = undef;
223  
224 128 foreach my $state_label ( keys %$states ) {
225 128 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 128 128
226 print "state $state_label score: "
227 0 . $states->{$state_label}->{'score'} . "\n";
228 }
229 128 ▶ if ( $states->{$state_label}->{'score'} > $best_state_score ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($states->{$state_label}{"score"} > $best_state_score) 128 0 128
230 128 $best_state_label = $state_label;
231 128 $best_state_score = $states->{$state_label}->{'score'};
232 }
233 }
234  
235 # only progress and/or debug info
236 128 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 128 128
237 0 print "best state $best_state_label score: "
238 . $best_state_score . "\n";
239 }
240  
241 128 return $best_state_label;
242 }
243  
244 # used as an internal part of label_subtree
245 # to get all probable labels for an edge
246 # i.e. make one step of the Viterbi algorithm
247 231 ▶sub label_edge {
Subroutine label_edge: called CC=12 SCAR=34.9
Pod documented
248  
249 231 my ( $self, $edge, $states, $prev_edge ) = @_;
250  
251 231 my $ALGORITHM = $self->config->labeller_algorithm;
252 231 my $new_states = {};
253 231 foreach my $last_state ( keys %$states ) {
254  
255 231 ▶ if ( $ALGORITHM == 21 && defined $prev_edge ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 21 and defined $prev_edge) 0 231 231
Condition33%1/3
$ALGORITHM == 21 and defined $prev_edge
!ll&&!rl&&r
23100
MC/DC0%0/2
$ALGORITHM == 21 and defined $prev_edge
$ALGORITHM == 21 defined $prev_edge
Truth table33%1/3
A and B
A= $ALGORITHM == 21B= defined $prev_edge
ABresult
0X0
100
111
256  
257 # set last label
258 0 my $best_prev_state_label = $self->find_best_state_label($states);
259 0 $prev_edge->child->label($best_prev_state_label);
260 }
261  
262 # only progress and/or debug info
263 231 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 231 231
264 print " Processing state $last_state (score "
265 0 . $states->{$last_state}->{'score'} . ")\n";
266 }
267  
268 # compute the possible labels scores (typically probabilities),
269 # i.e. products of emission and transition probs
270 # possible_labels{label} = score
271 my %possible_labels = %{
272 231 $self->get_possible_labels(
273 $edge,
274 $last_state,
275 231 $states->{$last_state}->{'score'},
276 )
277 };
278  
279 # only progress and/or debug info
280 231 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 231 231
281 0 print " " . scalar( keys %possible_labels )
282 . " possible labels are "
283 . ( join ' ', keys %possible_labels )
284 . "\n";
285 }
286  
287 231 foreach my $new_label ( keys %possible_labels ) {
288 3696 my $new_state_score = $possible_labels{$new_label};
289  
290 # only progress and/or debug info
291 3696 ▶ if ( $self->config->DEBUG >= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 5) 0 3696 3696
292 0 print " Trying label $new_label, "
293 . "score $new_state_score\n";
294 print " Old state path "
295 0 . ( join ' ', @{ $states->{$last_state}->{'path'} } )
296 . " \n";
297 print " Old states: "
298 . (
299 join ' ',
300 map {
301 0 "$_ (" . (
302 0 join ' ', @{ $states->{$_}->{'path'} }
303 ) . ")"
304 } keys %$states
305 )
306 . " \n";
307 print " New states: "
308 . (
309 join ' ',
310 map {
311 0 "$_ (" . (
312 0 join ' ', @{ $new_states->{$_}->{'path'} }
313 ) . ")"
314 } keys %$new_states
315 )
316 . " \n";
317 }
318  
319 # test if this is the best
320 3696 ▶ if (defined $new_states->{$new_label}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_states->{$new_label} and $new_states->{$new_label}{"score"} > $new_state_score) 0 3696 3696
Condition33%1/3
defined $new_states->{$new_label} and $new_states->{$new_label}{"score"} > $new_state_score
!ll&&!rl&&r
369600
MC/DC0%0/2
defined $new_states->{$new_label} and $new_states->{$new_label}{"score"} > $new_state_score
defined $new_states->{$new_label} $new_states->{$new_label}{"score"} > $new_state_score
Truth table33%1/3
A and B
A= defined $new_states->{$new_label}B= $new_states->{$new_label}{"score"} > $new_state_score
ABresult
0X0
100
111
321 && $new_states->{$new_label}->{'score'} > $new_state_score
322 )
323 {
324  
325 # there is already the same state state
326 # with higher score
327 0 next;
328 }
329  
330 # else such a state is not yet there resp. it is but its score
331 # is lower than $new_state_score -> set it (resp. replace it)
332  
333 3696 my @new_state_path = @{ $states->{$last_state}->{'path'} };
334 3696 push @new_state_path, $new_label;
335 3696 $new_states->{$new_label} = {
336 'path' => \@new_state_path,
337 'score' => $new_state_score,
338 };
339  
340 # only progress and/or debug info
341 3696 ▶ if ( $self->config->DEBUG >= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 5) 0 3696 3696
342 0 print " New state path "
343 . ( join ' ', @new_state_path )
344 . " \n";
345 print " Old states: "
346 . (
347 join ' ',
348 map {
349 0 "$_ (" . (
350 0 join ' ', @{ $states->{$_}->{'path'} }
351 ) . ")"
352 } keys %$states
353 )
354 . " \n";
355 print " New states: "
356 . (
357 join ' ',
358 map {
359 0 "$_ (" . (
360 0 join ' ', @{ $new_states->{$_}->{'path'} }
361 ) . ")"
362 } keys %$new_states
363 )
364 . " \n";
365 }
366 } # foreach $new_label
367  
368 # only progress and/or debug info
369 231 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 231 231
370 0 print " Now there are "
371 . ( keys %$new_states ) . " new states\n";
372 }
373 } # foreach $last_state
374  
375 231 $new_states = $self->prune($new_states);
376  
377 231 return $new_states;
378 }
379  
380 # prune the states (keep only some of the best)
381 231 ▶sub prune {
Subroutine prune: called CC=5 SCAR=16.7
Pod documented
382  
383 231 my ( $self, $states ) = @_;
384  
385 # pruning
386  
387 # pruning type 1 commented out because emission scores are not probs
388 # (they do not sum up to 1)
389 #
390 # pruning: keep as many best states so that their normed prob
391 # sums up to at least $VITERBI_STATES_PROB_SUM_THRESHOLD
392 # %states = ();
393 # my @best_states = sort {
394 # $new_states{$b}->{'score'}
395 # <=> $new_states{$a}->{'score'}
396 # } keys %new_states;
397 # my $prob_sum = 0;
398 # foreach my $state (@best_states) {
399 # $prob_sum += $new_states{$state}->{'score'};
400 # }
401 # my $threshold = $prob_sum
402 # * $VITERBI_STATES_PROB_SUM_THRESHOLD;
403 # my $best_prob_sum = 0;
404 # while ($best_prob_sum < $threshold) {
405 # my $state = shift @best_states;
406 # $states{$state} = $new_states{$state};
407 # $best_prob_sum += $new_states{$state}->{'score'};
408 # }
409  
410 # states going form pruning phase 1 to pruning phase 2
411 # %new_states = %states;
412  
413 # simple pruning: keep n best states
414 231 my $new_states = {};
415 my @best_states
416 = sort {
417 231 $states->{$b}->{'score'} <=> $states->{$a}->{'score'}
418 } keys %$states;
419 231 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if (@best_states and $i < $self->config->VITERBI_STATES_NUM_THRESHOLD) 231 231 462
Condition66%2/3
@best_states and $i < $self->config->VITERBI_STATES_NUM_THRESHOLD
!ll&&!rl&&r
0231231
MC/DC50%1/2
@best_states and $i < $self->config->VITERBI_STATES_NUM_THRESHOLD
@best_states $i < $self->config->VITERBI_STATES_NUM_THRESHOLD
Truth table66%2/3
A and B
A= @best_statesB= $i < $self->config->VITERBI_STATES_NUM_THRESHOLD
ABresult
0X0
100
111
420 my $i = 0;
421 @best_states && $i < $self->config->VITERBI_STATES_NUM_THRESHOLD;
422 $i++
423 )
424 {
425 231 my $state = shift @best_states;
426 231 $new_states->{$state} = $states->{$state};
427  
428 # only progress and/or debug info
429 231 ▶ if ( $self->config->DEBUG >= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 5) 0 231 231
430 0 print " Pruning let thrgough the state $state"
431 . "\n";
432 }
433 }
434  
435 # only progress and/or debug info
436 231 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 231 231
437 0 print " After pruning there are "
438 . ( keys %$new_states ) . " states\n";
439 }
440  
441 231 return $new_states;
442 }
443  
444 # computes possible labels for an edge, using info about
445 # the emission scores, transition scores and last state's score
446 231 ▶sub get_possible_labels {
Subroutine get_possible_labels: called CC=22 SCAR=54.3
Pod documented
447 231 my ( $self, $edge, $previous_label, $previous_label_score ) = @_;
448  
449 231 my $ALGORITHM = $self->config->labeller_algorithm;
450  
451 231 ▶ if ($ALGORITHM == 8
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19 or $ALGORITHM >= 20) { } 231 0 231
Condition33%6/18
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19 or $ALGORITHM >= 20
l!l&&r!l&&!r
23100
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19
l!l&&r!l&&!r
23100
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18
l!l&&r!l&&!r
02310
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17
l!l&&r!l&&!r
00231
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16
l!l&&r!l&&!r
00231
$ALGORITHM == 8 or $ALGORITHM == 9
l!l&&r!l&&!r
00231
MC/DC0%0/7
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19 or $ALGORITHM >= 20
$ALGORITHM == 8 $ALGORITHM == 9 $ALGORITHM == 16 $ALGORITHM == 17 $ALGORITHM == 18 $ALGORITHM == 19 $ALGORITHM >= 20
Truth table12%1/8
A or B or C or D or E or F or G
A= $ALGORITHM == 8B= $ALGORITHM == 9C= $ALGORITHM == 16D= $ALGORITHM == 17E= $ALGORITHM == 18F= $ALGORITHM == 19G= $ALGORITHM >= 20
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
452 || $ALGORITHM == 9
453 || $ALGORITHM == 16
454 || $ALGORITHM == 17
455 || $ALGORITHM == 18
456 || $ALGORITHM == 19
457 || $ALGORITHM >= 20
458 )
459 {
460  
461 231 my $result = {};
462 231 my $all_labels = $self->model->get_all_labels();
463  
464 # foreach possible label (actually foreach existing label)
465 231 foreach my $label ( @{$all_labels} ) {
466  
467 # score = previous score + new score
468  
469 3696 ▶ if ( $ALGORITHM == 20 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ALGORITHM == 20) { } 0 3696 3696
elsif ($ALGORITHM == 19) { } 0 3696 3696
elsif ($ALGORITHM == 21) { } 0 3696 3696
elsif ($ALGORITHM == 18) { } 3696 0 3696
470 0 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 0 0
471 0 print " Score for label $label: "
472 . (
473 $self->model->get_label_score(
474 $label, $previous_label, $edge->features_all_labeller()
475 )
476 ) . "\n";
477 }
478 0 $result->{$label} =
479 $self->model->get_label_score(
480 $label, $previous_label, $edge->features_all_labeller()
481 )
482 ;
483 } elsif ( $ALGORITHM == 19 ) {
484 0 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 0 0
485 0 print " Score for label $label: $previous_label_score + "
486 . (
487 $self->model->get_label_score(
488 $label, $previous_label, $edge->features_all_labeller()
489 )
490 ) . "\n";
491 }
492 0 $result->{$label} =
493 $previous_label_score
494 * $self->model->get_label_score(
495 $label, $previous_label, $edge->features
496 )
497 ;
498 } elsif ( $ALGORITHM == 21 ) {
499 0 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 0 0
500 0 print " Score for label $label: $previous_label_score + "
501 . (
502 $self->model->get_label_score(
503 $label, $previous_label, $edge->features_all_labeller()
504 )
505 ) . "\n";
506 }
507 0 $result->{$label} =
508 $previous_label_score
509 + $self->model->get_label_score(
510 $label, $previous_label, $edge->features_all_labeller()
511 )
512 ;
513 } elsif ( $ALGORITHM == 18 ) {
514 3696 $result->{$label} =
515 $self->model->get_label_score(
516 $label, $previous_label, $edge->features
517 )
518 ;
519 } else {
520 0 ▶ if ( $self->config->DEBUG >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 4) 0 0 0
521 0 print " Score for label $label: $previous_label_score + "
522 . (
523 $self->model->get_label_score(
524 $label, $previous_label, $edge->features
525 )
526 ) . "\n";
527 }
528 0 $result->{$label} =
529 $previous_label_score
530 + $self->model->get_label_score(
531 $label, $previous_label, $edge->features
532 )
533 ;
534 }
535  
536 } # end foreach $label
537  
538 231 return $result;
539  
540 } else { # $ALGORITHM not in 8, 9, >16
541  
542 0 my $emission_scores =
543 $self->model->get_emission_scores( $edge->features );
544  
545 0 my $transition_scores = {};
546 0 foreach my $possible_label ( keys %$emission_scores ) {
547 0 $transition_scores->{$possible_label} =
548 $self->model->get_transition_score(
549 $possible_label, $previous_label
550 );
551 }
552  
553 0 my $possible_labels = $self->get_possible_labels_internal(
554 $emission_scores,
555 $transition_scores,
556 $previous_label_score,
557 );
558  
559 0 ▶ if ( scalar( keys %$possible_labels ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$possible_labels > 0) { } 0 0 0
560 0 return $possible_labels;
561 } else {
562  
563 # no possible states generated -> backoff
564 0 my %unigrams_copy = %{ $self->model->unigrams };
565 0 my $blind_scores = \%unigrams_copy;
566  
567 # TODO: smoothing (now a very stupid way is used
568 # just to lower the scores below the unblind scores)
569 0 foreach my $label ( keys %$blind_scores ) {
570 0 $blind_scores->{$label} *= 0.01;
571 }
572  
573 # fall back to unigram distribution for transitions
574 # TODO: smoothing of bigram, unigram and uniform distros
575 # for transitions
576 # (this fallback should then become obsolete)
577 0 $possible_labels = $self->get_possible_labels_internal(
578 $emission_scores,
579 $blind_scores,
580 $previous_label_score,
581 );
582  
583 0 ▶ if ( scalar( keys %$possible_labels ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$possible_labels > 0) { } 0 0 0
584 0 return $possible_labels;
585 } else {
586 0 warn "Based on the training data, no possible label was found"
587 . " for an edge. This usually means that either"
588 . " your training data are not big enough or that"
589 . " the set of features you are using"
590 . " is not well constructed - either it is too small"
591 . " or it lacks features that would be general enough"
592 . " to cover all possible sentences."
593 . " Using blind emission probabilities instead.\n"
594 . " get_possible_labels ($edge, $previous_label,"
595 . " $previous_label_score ) \n"
596 . $edge->sentence->id
597 . ': '
598 . $edge->parent->fields->[1]
599 . " -> "
600 . $edge->child->fields->[1]
601 . "\n";
602  
603 # TODO: these are more or less probabilities, which might
604 # be unappropriate for some of the
605 # algorithms -> recompute somehow;
606 # also they do contain the SEQUENCE_BOUNDARY_LABEL prob
607  
608 0 $possible_labels = $self->get_possible_labels_internal(
609 $blind_scores,
610 $blind_scores,
611 $previous_label_score,
612 );
613  
614 0 ▶ if ( scalar( keys %$possible_labels ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$possible_labels > 0) { } 0 0 0
615 0 return $possible_labels;
616 } else {
617 0 warn "no possible labels generated, no fallback helped:"
618 . " probably there is a bug in the code!"
619 . " get_possible_labels ($edge, $previous_label,"
620 . " $previous_label_score ) "
621 . $edge->parent->fields->[1]
622 . " -> "
623 . $edge->child->fields->[1]
624 . "\n";
625 0 return {};
626 } # end no backoff helped
627 } # end backoff by unigrams for emission and transition scores
628 } # end backoff by unigrams for transition scores
629 } # end $ALGORITHM not in 8,9
630 }
631  
632 0 ▶sub get_possible_labels_internal {
Subroutine get_possible_labels_internal: not called CC=13 SCAR=52.0
Pod documented
633 0 my ( $self, $emission_scores, $transition_scores, $last_state_score ) = @_;
634  
635 # $emission_scores are often not really probs but scores
636  
637 0 my $ALGORITHM = $self->config->labeller_algorithm;
638  
639 0 ▶ if ($ALGORITHM == 8
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19) 0 0 0
Condition0%0/15
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19
l!l&&r!l&&!r
000
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18
l!l&&r!l&&!r
000
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17
l!l&&r!l&&!r
000
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16
l!l&&r!l&&!r
000
$ALGORITHM == 8 or $ALGORITHM == 9
l!l&&r!l&&!r
000
MC/DC0%0/6
$ALGORITHM == 8 or $ALGORITHM == 9 or $ALGORITHM == 16 or $ALGORITHM == 17 or $ALGORITHM == 18 or $ALGORITHM == 19
$ALGORITHM == 8 $ALGORITHM == 9 $ALGORITHM == 16 $ALGORITHM == 17 $ALGORITHM == 18 $ALGORITHM == 19
Truth table0%0/7
A or B or C or D or E or F
A= $ALGORITHM == 8B= $ALGORITHM == 9C= $ALGORITHM == 16D= $ALGORITHM == 17E= $ALGORITHM == 18F= $ALGORITHM == 19
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
640 || $ALGORITHM == 9
641 || $ALGORITHM == 16
642 || $ALGORITHM == 17
643 || $ALGORITHM == 18
644 || $ALGORITHM == 19
645 )
646 {
647  
648 # these algorithms have such a simple way of computing possible labels
649 # they they do not need to have it split into two subroutines
650 0 croak "Labeller->get_possible_labels_internal not implemented"
651 . " for algorithm no $ALGORITHM!";
652 }
653  
654 0 my %possible_labels;
655 0 foreach my $possible_label ( keys %$emission_scores ) {
656 0 my $emission_score = $emission_scores->{$possible_label};
657 0 my $transition_score = $transition_scores->{$possible_label};
658 0 ▶ if ( !defined $transition_score ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $transition_score) 0 0 0
659 0 next;
660 }
661  
662 0 my $possible_state_score;
663 0 ▶ if ( $ALGORITHM == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ALGORITHM == 2) { } 0 0 0
664 0 $possible_state_score
665 = $last_state_score + $emission_score * $transition_score;
666 } else {
667 0 $possible_state_score
668 = $last_state_score * $emission_score * $transition_score;
669 }
670  
671 # if no state like that yet or better than current max,
672 # use this new state
673 0 ▶ if ($possible_state_score > 0
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_state_score > 0 and !$possible_labels{$possible_label} || $possible_labels{$possible_label}{"score"} < $possible_state_score) 0 0 0
Condition0%0/6
$possible_state_score > 0 and !$possible_labels{$possible_label} || $possible_labels{$possible_label}{"score"} < $possible_state_score
!ll&&!rl&&r
000
not $possible_labels{$possible_label} or $possible_labels{$possible_label}{"score"} < $possible_state_score
l!l&&r!l&&!r
000
MC/DC0%0/3
$possible_state_score > 0 and !$possible_labels{$possible_label} || $possible_labels{$possible_label}{"score"} < $possible_state_score
$possible_state_score > 0 not $possible_labels{$possible_label} $possible_labels{$possible_label}{"score"} < $possible_state_score
Truth table0%0/4
A and not(B) or C
A= $possible_state_score > 0B= not $possible_labels{$possible_label}C= $possible_labels{$possible_label}{"score"} < $possible_state_score
ABCresult
0XX0
1000
11X1
1011
674 && (!$possible_labels{$possible_label}
675 || $possible_labels{$possible_label}->{'score'}
676 < $possible_state_score
677 )
678 )
679 {
680 0 $possible_labels{$possible_label} = $possible_state_score;
681 }
682  
683 # else we already have a state with the same key but higher score
684 } # end foreach $possible_label
685  
686 0 return \%possible_labels;
687 }
688  
689 4 1;
690  
691 __END__
« MSTperl/ModelLabelling.pm ↑ Summary MSTperl/TrainerLabelling.pm »