Tool/Tagger/Featurama.pm

filter: Statementstmt 34.8% 31 / 89 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 6 / 18 Podpod 50.0% 1 / 2 totaltotal 30.8% 38 / 123 CC 11 SCAR 21.5
CC
11
Cov
30%
_get_model_file
20.0
_tag
20.0
_build_perc
12.0
CRAP 8.6
« Tool/ProcessUtils.pm ↑ Summary Tool/Tagger/MorphoDiTa.pm »
1 package Treex::Tool::Tagger::Featurama;
2 1 $Treex::Tool::Tagger::Featurama::VERSION = '2.20151102';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 with 'Treex::Tool::Tagger::Role';
9  
10 1 Moose::Exporter->setup_import_methods(
11 as_is => ['tag_sentence'],
12 );
13  
14 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 has path => (
16 is => 'ro',
17 isa => 'Str', #or isa=> 'Path' ?
18 predicate => '_path_given',
19 documentation => q{Path to models relative to share.},
20 );
21  
22 1 has local_path => (
23 is => 'ro',
24 isa => 'Str',
25 predicate => '_local_path_given',
26 documentation => q{Local path to models.},
27 );
28  
29 1 has alpha => (
30 is => 'ro',
31 isa => 'Str',
32 builder => '_build_alpha_file',
33 lazy => 1,
34 );
35  
36 1 has dict => (
37 is => 'ro',
38 isa => 'Str',
39 builder => '_build_dict_file',
40 lazy => 1,
41 );
42  
43 1 has feature => (
44 is => 'ro',
45 isa => 'Str',
46 builder => '_build_feature_file',
47 lazy => 1,
48 );
49  
50 1 has prune => (
51 is => 'ro',
52 isa => 'Int',
53 default => 0,
54 documentation => q{Indicates whether we will be pruning Viterbi states},
55 );
56  
57 1 has n_best => (
58 is => 'ro',
59 isa => 'Int',
60 default => 1,
61 documentation => q{How many n-best results to use},
62 );
63  
64 1 has order => (
65 is => 'ro',
66 isa => 'Int',
67 default => 3,
68 documentation => q{Order ot the N-grams},
69 );
70  
71 1 has perc => (
72 is => 'ro',
73 isa => 'Featurama::Perc',
74 builder => '_build_perc',
75 lazy => 1,
76 init_arg => undef,
77 documentation => q{Wrapped C object},
78 );
79  
80 0 ▶sub _get_model_file {
Subroutine _get_model_file: not called CC=4 SCAR=30.0
81 0 my $self = shift;
82 0 my $suffix = shift;
83 0 ▶ if ( $self->_local_path_given ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_local_path_given) { } 0 0 0
elsif ($self->_path_given) { } 0 0 0
84 0 return $self->local_path . '.' . $suffix;
85 }
86 elsif ( $self->_path_given ) {
87 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Treex::Core::Resource; 1 } } or confess("Treex share not available. Consider providing 'local_path' parameter") 0 0 0
88 0 require Treex::Core::Resource;
89 0 1;
90 } or confess q(Treex share not available. Consider providing 'local_path' parameter);
91 0 return Treex::Core::Resource::require_file_from_share( $self->path . '.' . $suffix );
92 }
93 else {
94 0 confess q(You have to provide at least one of the 'path' and 'local_path' parameters);
95 }
96 }
97  
98 0 ▶sub _build_alpha_file {
Subroutine _build_alpha_file: not called CC=1 SCAR=6.9
99 0 my $self = shift;
100 0 return $self->_get_model_file('alpha');
101 }
102  
103 0 ▶sub _build_dict_file {
Subroutine _build_dict_file: not called CC=1 SCAR=6.9
104 0 my $self = shift;
105 0 return $self->_get_model_file('dict');
106 }
107  
108 0 ▶sub _build_feature_file {
Subroutine _build_feature_file: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110 0 return $self->_get_model_file('f');
111 }
112  
113 0 ▶sub _build_perc {
Subroutine _build_perc: not called CC=3 SCAR=24.8
114 0 my $self = shift;
115 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Featurama::Perc; 1 } } or log_fatal("Cannot load Featurama::Perc. Please check whether it is installed") 0 0 0
116 0 require Featurama::Perc; #when featurama will be on CPAN, it will be probably in different namespace
117 0 1;
118 } or log_fatal('Cannot load Featurama::Perc. Please check whether it is installed');
119 0 my $perc = Featurama::Perc->new();
120 0 my $header = join "\t", $self->_get_feature_names();
121  
122 0 ▶ if ( not $perc->testInit( $self->feature, $self->dict, $self->alpha, $header, $self->prune, $self->n_best, $self->order ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($perc->testInit($self->feature, $self->dict, $self->alpha, $header, $self->prune, $self->n_best, $self->order)) 0 0 0
123 0 log_fatal("Cannot initialize Featurama::Perc");
124 }
125 0 return $perc;
126 }
127  
128 0 ▶sub DEMOLISH {
Subroutine DEMOLISH: not called CC=2 SCAR=17.9
Pod undocumented
129 0 my $self = shift;
130  
131 # We must prevent lazy building of $self->perc during DEMOLISH
132 # because in that case "require Featurama::Perc" fails (we are in cleanup).
133 # Therefore, we cannot use $self->perc in the condition below.
134 0 ▶ if ( $self->{perc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perc"}) 0 0 0
135 0 $self->perc->testFinish();
136 }
137 0 return;
138 }
139  
140 0 ▶sub tag_sentence {
Subroutine tag_sentence: not called CC=2 SCAR=17.9
Pod documented
141 0 my ( $self, $wordforms_rf, $analyses_rf ) = @_;
142  
143 0 ▶ if ( !$analyses_rf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($analyses_rf) 0 0 0
144  
145 # array of analyses, each analysis is an array of lemma/tag info stored in a hash
146 0 my @temp = @{$wordforms_rf};
147 0 my @analyses = map { [ $self->_analyze($_) ] } @temp;
148  
149 0 $analyses_rf = \@analyses;
150 }
151  
152 # tagging
153 0 return $self->_tag( $wordforms_rf, $analyses_rf );
154 }
155  
156 0 ▶sub _get_feature_names {
Subroutine _get_feature_names: not called CC=1 SCAR=6.9
157 0 log_fatal( 'This method has to be overriden: ' . ( caller(0) )[3] );
158 }
159  
160 0 ▶sub _get_features {
Subroutine _get_features: not called CC=1 SCAR=6.9
161 0 log_fatal( 'This method has to be overriden: ' . ( caller(0) )[3] );
162 }
163  
164 0 ▶sub _extract_tag_and_lemma {
Subroutine _extract_tag_and_lemma: not called CC=1 SCAR=6.9
165 0 log_fatal( 'This method has to be overriden: ' . ( caller(0) )[3] );
166 }
167  
168 0 ▶sub _analyze {
Subroutine _analyze: not called CC=1 SCAR=6.9
169 0 log_fatal( 'This method has to be overriden: ' . ( caller(0) )[3] );
170 }
171  
172 0 ▶sub _tag {
Subroutine _tag: not called CC=4 SCAR=30.0
173 0 my ( $self, $forms, $analyses ) = @_;
174  
175 0 my @sent;
176  
177 # extract features
178 0 foreach my $i ( 0 .. $#{$forms} ) { #go through word forms and analyses
179  
180 0 my @word = $self->_get_features( $forms, $analyses, $i ); #load features
181 0 push( @sent, \@word );
182 }
183  
184 0 $self->perc->beginSentence();
185 0 foreach my $word_rf (@sent) {
186 0 $self->perc->appendWord( join( "\t", @{$word_rf} ) );
187 }
188 0 $self->perc->endSentence();
189  
190 0 my ( @tags, @lemmas );
191 0 foreach my $i ( 0 .. $#{$forms} ) {
192 0 my $tag_and_lemma = $self->_extract_tag_and_lemma( $i, $forms->[$i] );
193 0 push( @tags, $tag_and_lemma->{tag} );
194 0 push( @lemmas, $tag_and_lemma->{lemma} );
195 }
196  
197 0 return ( \@tags, \@lemmas );
198 }
199  
200 1 1;
201  
202 __END__
« Tool/ProcessUtils.pm ↑ Summary Tool/Tagger/MorphoDiTa.pm »