MSTperl/ModelUnlabelled.pm

filter: Statementstmt 85.5% 71 / 83 Branchbran 57.1% 8 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 86.6% 13 / 15 Podpod 84.6% 11 / 13 totaltotal 82.4% 103 / 125 CC 11 SCAR 10.1
CC
11
Cov
81%
feature_is_unknown
6.0
load_data_tsv
5.1
get_data_to_store_tsv
3.0
CRAP 2.7
« MSTperl/Edge.pm ↑ Summary MSTperl.pm »
1 package Treex::Tool::Parser::MSTperl::ModelUnlabelled;
2 {
3 5 $Treex::Tool::Parser::MSTperl::ModelUnlabelled::VERSION = '0.11949';
4 }
5  
6 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 extends 'Treex::Tool::Parser::MSTperl::ModelBase';
9  
10 # TODO: features indexed? (i.e. weights would be an ArrayRef etc.)
11 # It would help to push down the size of edge_features_cache
12 # (no speedup or slowdown is expected).
13 has 'weights' => (
14 is => 'rw',
15 isa => 'HashRef',
16 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 5 );
18  
19 2 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
20 2 my ($self) = @_;
21  
22 2 $self->featuresControl( $self->config->unlabelledFeaturesControl );
23  
24 2 return;
25 }
26  
27 # STORING AND LOADING
28  
29 1 ▶sub get_data_to_store {
Subroutine get_data_to_store: called CC=1 SCAR=0.0
Pod documented
30 1 my ($self) = @_;
31  
32 1 return $self->weights;
33 }
34  
35 1 ▶sub get_data_to_store_tsv {
Subroutine get_data_to_store_tsv: called CC=3 SCAR=11.1
Pod documented
36 1 my ($self) = @_;
37  
38 1 my @result;
39 1 foreach my $feature ( keys %{ $self->weights } ) {
40 1444 ▶ if ( $feature =~ /^([0-9]+):(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature =~ /^([0-9]+):(.*)$/) { } 1444 0 1444
41 1444 my $index = $1;
42 1444 my $value = $2;
43 1444 my $code = $self->featuresControl->feature_codes->[$index];
44 1444 my $feature_str = "$code:$value";
45 1444 push @result, $feature_str . "\t" . $self->weights->{$feature};
46 } else {
47 0 print STDERR "Feature $feature is not in correct format!\n";
48 }
49 }
50  
51 1 return [@result];
52 }
53  
54 2 ▶sub load_data {
Subroutine load_data: called CC=2 SCAR=7.2
Pod documented
55  
56 2 my ( $self, $data ) = @_;
57  
58 2 $self->weights($data);
59  
60 2 ▶ if ( scalar( keys %{ $self->weights } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->weights;}) { } 2 0 2
61 2 return 1;
62 } else {
63 0 return 0;
64 }
65 }
66  
67 1 ▶sub load_data_tsv {
Subroutine load_data_tsv: called CC=5 SCAR=16.3
Pod documented
68  
69 1 my ( $self, $data ) = @_;
70  
71 1 my %weights;
72  
73 #precompute feature code to feature index translation table
74 my %code2index;
75 1 my $feature_num = $self->featuresControl->feature_count;
76 1 ▶ for (
Branch: 1 branch
BranchTrueFalseTotal
if ($feature_index < $feature_num) 19 1 20
77 my $feature_index = 0;
78 $feature_index < $feature_num;
79 $feature_index++
80 )
81 {
82 19 my $code = $self->featuresControl->feature_codes->[$feature_index];
83 19 $code2index{$code} = $feature_index;
84 }
85  
86 1 foreach my $line (@$data) {
87 1444 my ( $feature, $weight ) = split /\t/, $line;
88 1444 ▶ if ( $feature =~ /^([^:]+):(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature =~ /^([^:]+):(.*)$/) { } 1444 0 1444
89 1444 my $code = $1;
90 1444 my $value = $2;
91 1444 my $index = $code2index{$code};
92 1444 my $feature_indexed = "$index:$value";
93 1444 $weights{$feature_indexed} = $weight;
94 } else {
95 0 print STDERR "Feature $feature is not in correct format!\n";
96 }
97 }
98  
99 1 $self->weights( \%weights );
100  
101 1 ▶ if ( scalar( keys %{ $self->weights } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$self->weights;}) { } 1 0 1
102 1 return 1;
103 } else {
104 0 return 0;
105 }
106 }
107  
108 # FEATURE WEIGHTS
109  
110 0 ▶sub score_edge {
Subroutine score_edge: not called CC=1 SCAR=6.9
Pod documented
111  
112 # (Treex::Tool::Parser::MSTperl::Edge $edge)
113 0 my ( $self, $edge ) = @_;
114  
115 0 my $features_rf = $self->featuresControl->get_all_features($edge);
116 0 return $self->score_features($features_rf);
117 }
118  
119 28 ▶sub score_sentence {
Subroutine score_sentence: called CC=1 SCAR=0.0
Pod documented
120  
121 # (Treex::Tool::Parser::MSTperl::Sentence $sentence)
122 28 my ( $self, $sentence ) = @_;
123  
124 28 my $score = $self->score_features( $sentence->features );
125  
126 28 return $score;
127 }
128  
129 3763 ▶sub score_features {
Subroutine score_features: called CC=2 SCAR=6.9
Pod documented
130  
131 # (ArrayRef[Str] $features)
132 3763 my ( $self, $features ) = @_;
133  
134 3763 my $score = 0;
135 3763 foreach my $feature ( @{$features} ) {
136 94678 $score += $self->get_feature_weight($feature);
137 }
138  
139 3763 return $score;
140 }
141  
142 94678 ▶sub get_feature_weight {
Subroutine get_feature_weight: called CC=2 SCAR=6.9
Pod documented
143  
144 # (Str $feature)
145 94678 my ( $self, $feature ) = @_;
146  
147 94678 my $weight = $self->weights->{$feature};
148 94678 ▶ if ($weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight) { } 22415 72263 94678
149 22415 return $weight;
150 } else {
151 72263 return 0;
152 }
153 }
154  
155 0 ▶sub feature_is_unknown {
Subroutine feature_is_unknown: not called CC=2 SCAR=17.9
Pod undocumented
156  
157 # (Str $feature)
158 0 my ( $self, $feature ) = @_;
159  
160 0 my $weight = $self->weights->{$feature};
161 0 ▶ if ($weight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weight) { } 0 0 0
162 0 return 0;
163 } else {
164 0 return 1;
165 }
166 }
167  
168 1444 ▶sub set_feature_weight {
Subroutine set_feature_weight: called CC=1 SCAR=0.0
Pod documented
169  
170 # (Str $feature, Num $weight)
171 1444 my ( $self, $feature, $weight ) = @_;
172  
173 1444 $self->weights->{$feature} = $weight;
174  
175 1444 return;
176 }
177  
178 1988 ▶sub update_feature_weight {
Subroutine update_feature_weight: called CC=1 SCAR=0.0
Pod documented
179  
180 # (Str $feature, Num $update)
181 1988 my ( $self, $feature, $update ) = @_;
182  
183 1988 $self->weights->{$feature} += $update;
184  
185 1988 return;
186 }
187  
188 # returns number of features in the model
189 1 ▶sub get_feature_count {
Subroutine get_feature_count: called CC=1 SCAR=0.0
Pod documented
190 1 my ($self) = @_;
191  
192 1 return scalar( keys %{ $self->weights } );
193 }
194  
195 5 1;
196  
197 __END__
« MSTperl/Edge.pm ↑ Summary MSTperl.pm »