MSTperl/Parser.pm

filter: Statementstmt 77.3% 58 / 75 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 75.0% 3 / 4 totaltotal 75.9% 79 / 104 CC 12 SCAR 23.8
CC
12
Cov
74%
parse_sentence_internal
18.9
parse_sentence
2.0
BEGIN
1.0
CRAP 10.8
« MSTperl/FeaturesControl.pm ↑ Summary MSTperl/TrainerUnlabelled.pm »
1 package Treex::Tool::Parser::MSTperl::Parser;
2 {
3 4 $Treex::Tool::Parser::MSTperl::Parser::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::ModelUnlabelled;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use Graph 0.94;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Graph::ChuLiuEdmonds 0.05; #returns MINIMUM spanning tree
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 has config => (
17 isa => 'Treex::Tool::Parser::MSTperl::Config',
18 is => 'ro',
19 required => '1',
20 );
21  
22 4 has model => (
23 isa => 'Maybe[Treex::Tool::Parser::MSTperl::ModelUnlabelled]',
24 is => 'rw',
25 );
26  
27 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
28 2 my ($self) = @_;
29  
30 2 $self->model(
31 Treex::Tool::Parser::MSTperl::ModelUnlabelled->new(
32 config => $self->config,
33 )
34 );
35  
36 2 return;
37 }
38  
39 1 ▶sub load_model {
Subroutine load_model: called CC=1 SCAR=0.0
Pod documented
40  
41 # (Str $filename)
42 1 my ( $self, $filename ) = @_;
43  
44 1 return $self->model->load($filename);
45 }
46  
47 0 ▶sub parse_sentence {
Subroutine parse_sentence: not called CC=1 SCAR=6.9
Pod documented
48  
49 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
50 0 my ( $self, $sentence ) = @_;
51  
52 # parse sentence (does not modify $sentence)
53 0 my $sentence_parsed = $self->parse_sentence_internal($sentence);
54  
55 0 return $sentence_parsed->toParentOrdsArray();
56 }
57  
58 17 ▶sub parse_sentence_internal {
Subroutine parse_sentence_internal: called CC=12 SCAR=29.4
Pod documented
59  
60 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
61 17 my ( $self, $sentence ) = @_;
62  
63 17 ▶ if ( !$self->model ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->model) 0 17 17
64 0 croak "MSTperl parser error: There is no model for unlabelled parsing!";
65 }
66  
67 # copy the sentence (do not modify $sentence directly)
68 17 my $sentence_working_copy = $sentence->copy_nonparsed();
69 17 my $sentence_length = $sentence_working_copy->len();
70  
71 17 my $graph = Graph->new(
72 vertices => [ ( 0 .. $sentence_length ) ]
73 );
74 17 my @weighted_edges;
75 0 ▶ if ( $self->config->DEBUG >= 2 ) { print "EDGES (parent -> child):\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 17 17
76 17 foreach my $child ( @{ $sentence_working_copy->nodes } ) {
77 231 foreach my $parent ( @{ $sentence_working_copy->nodes_with_root } ) {
78 3966 ▶ if ( $child == $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child == $parent) 231 3735 3966
79 231 next;
80 }
81  
82 3735 my $edge = Treex::Tool::Parser::MSTperl::Edge->new(
83 child => $child,
84 parent => $parent,
85 sentence => $sentence_working_copy
86 );
87  
88 3735 my $features = $self->config->unlabelledFeaturesControl
89 ->get_all_features($edge);
90 3735 my $score = $self->model->score_features($features);
91  
92 # only progress and/or debug info
93 3735 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 3735 3735
94 0 print $parent->ord . ' ' . $parent->fields->[1] .
95 ' -> ' . $child->ord . ' ' . $child->fields->[1] .
96 ' score: ' . $score . "\n";
97 0 print $parent->ord .
98 ' -> ' . $child->ord .
99 ' score: ' . $score . "\n";
100 0 foreach my $feature ( @{$features} ) {
101 0 print $feature . ", ";
102 }
103 0 print "\n";
104 0 print "\n";
105 }
106  
107 # MaxST needed but MinST is computed
108 # -> need to normalize score as -$score
109 3735 push @weighted_edges, ( $parent->ord, $child->ord, -$score );
110 }
111 }
112  
113 # only progress and/or debug info
114 17 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 17 17
115 0 print "GRAPH:\n";
116 0 print join " ", @weighted_edges;
117 0 print "\n";
118 }
119  
120 17 $graph->add_weighted_edges(@weighted_edges);
121  
122 17 my $msts = $graph->MST_ChuLiuEdmonds($graph);
123  
124 0 ▶ if ( $self->config->DEBUG >= 2 ) { print "RESULTS (parent -> child):\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 17 17
125  
126 #results
127 17 foreach my $edge ( $msts->edges ) {
128 231 my ( $parent, $child ) = @$edge;
129 231 $sentence_working_copy->setChildParent( $child, $parent );
130  
131 231 ▶ if ( $self->config->DEBUG >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 2) 0 231 231
132 0 print "$parent -> $child\n";
133 }
134 }
135  
136 17 return $sentence_working_copy;
137 }
138  
139 4 1;
140  
141 __END__
« MSTperl/FeaturesControl.pm ↑ Summary MSTperl/TrainerUnlabelled.pm »