Tool/EnglishMorpho/Analysis.pm

filter: Statementstmt 21.4% 21 / 98 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 38.4% 5 / 13 Podpod 50.0% 1 / 2 totaltotal 16.5% 27 / 163 CC 24 SCAR 51.7
CC
24
Cov
15%
get_possible_tags
342.0
_load_dictionary
30.0
_load_list
12.0
CRAP 176.6
« Tool/EnglishMorpho/Lemmatizer.pm ↑ Summary Block/W2A/EN/FixTags.pm »
1 package Treex::Tool::EnglishMorpho::Analysis;
2 2 $Treex::Tool::EnglishMorpho::Analysis::VERSION = '2.20151102';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Treex::Core::Resource qw(require_file_from_share);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my @params = qw(my_dict big_dict preterites participles);
11 2 foreach my $param (@params) {
12 8 has $param => (
13 is => 'ro',
14 isa => 'HashRef',
15 lazy => 1,
16 builder => "_build_$param",
17 init_arg => undef,
18 );
19 }
20  
21 2 has 'data_dir' => (
22 is => 'ro',
23 isa => 'Str',
24 default => 'data/models/morpho_analysis/en',
25 );
26  
27 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
28 0 my ($self) = @_;
29  
30 # Force the lazy-built parameters to be loaded.
31 # This should not be needed, but for some strange reasons it is.
32 # Without this Morce::English tags "But" as IN instead of CC.
33 # echo "But" | treex -Len -t W2A::EN::TagMorce Write::CoNLLX
34 # There is some black magic behind, because in both cases
35 # (with this pre-loading and without),
36 # get_possible_tags('But') returns the same list of possible tags.
37 0 $self->my_dict;
38 0 $self->big_dict;
39 0 $self->preterites;
40 0 $self->participles;
41 0 return;
42 }
43  
44 # --------- initialization ---------
45  
46 0 ▶sub _build_my_dict {
Subroutine _build_my_dict: not called CC=1 SCAR=6.9
47 0 my $self = shift;
48 0 return $self->_load_dictionary( require_file_from_share( $self->data_dir . '/muj_slovnik.txt' ) );
49 }
50  
51 0 ▶sub _build_big_dict {
Subroutine _build_big_dict: not called CC=1 SCAR=6.9
52 0 my $self = shift;
53 0 return $self->_load_dictionary( require_file_from_share( $self->data_dir . '/big_slovnik.txt' ) );
54 }
55  
56 0 ▶sub _build_preterites {
Subroutine _build_preterites: not called CC=1 SCAR=6.9
57 0 my $self = shift;
58 0 return $self->_load_list( require_file_from_share( $self->data_dir . '/preterite.tsv' ) );
59 }
60  
61 0 ▶sub _build_participles {
Subroutine _build_participles: not called CC=1 SCAR=6.9
62 0 my $self = shift;
63 0 return $self->_load_list( require_file_from_share( $self->data_dir . '/participle.tsv' ) );
64 }
65  
66 0 ▶sub _load_dictionary {
Subroutine _load_dictionary: not called CC=5 SCAR=34.0
67 0 my $self = shift;
68 0 my $file = shift;
69 0 my $dict;
70 0 ▶ open my $DATA, '<', $file or treex_fatal("Can't open morphology file $file.");
Branch: 1 branch
BranchTrueFalseTotal
open my $DATA, "<", $file or treex_fatal("Can't open morphology file $file.") 0 0 0
71 LOAD:
72 0 ▶ while (<$DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $DATA)) 0 0 0
73 0 chomp;
74 0 ▶ next LOAD if $_ eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "" 0 0 0
75 0 my @items = split qr/ /, $_;
76 0 my $word = lc shift(@items);
77 0 foreach my $tag (@items) {
78 0 $dict->{$word}->{$tag} = 1;
79 }
80 }
81 0 close($DATA);
82 0 return $dict;
83 }
84  
85 0 ▶sub _load_list {
Subroutine _load_list: not called CC=3 SCAR=24.8
86 0 my $self = shift;
87 0 my $file = shift;
88 0 my $list;
89 0 ▶ open my $DATA, '<', $file or treex_fatal("Can't open morphology file $file.");
Branch: 1 branch
BranchTrueFalseTotal
open my $DATA, "<", $file or treex_fatal("Can't open morphology file $file.") 0 0 0
90 0 ▶ while (<$DATA>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $DATA)) 0 0 0
91 0 chomp;
92 0 $list->{$_} = 1;
93 }
94 0 close($DATA);
95 0 return $list;
96 }
97  
98 # --------- interface ---------
99  
100 0 ▶sub get_possible_tags { ## no critic (Subroutines::ProhibitExcessComplexity) this is complex
Subroutine get_possible_tags: not called CC=18 SCAR=58.3
Pod documented
101 0 my $self = shift;
102 0 my $wordform = shift;
103 0 my $lowerform = lc($wordform);
104 0 my @possible;
105  
106 # 1. check my_dict for closed-class words. If yes, this is the last step.
107 0 my $dict_tags = $self->my_dict->{$lowerform};
108 0 ▶ if ($dict_tags){
Branch: 1 branch
BranchTrueFalseTotal
if ($dict_tags) 0 0 0
109 0 @possible = keys %{$dict_tags};
110 0 ▶ if ( $lowerform ne $wordform ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform ne $wordform) 0 0 0
111 0 push @possible, qw(NNP);
112 }
113 0 return @possible;
114 }
115  
116 # 2a. check big_dict
117 0 $dict_tags = $self->big_dict->{$lowerform};
118 0 ▶ if ($dict_tags){
Branch: 1 branch
BranchTrueFalseTotal
if ($dict_tags) { } 0 0 0
119 0 @possible = keys %{$dict_tags};
120 0 ▶ if ( $lowerform ne $wordform ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform ne $wordform) 0 0 0
121 0 push @possible, qw(NNP NNPS);
122 }
123 }
124  
125 # 2b apply morpho guesses, recall is more important than precision
126 else {
127 0 push @possible, qw(FW JJ NN NNS RB);
128  
129 # comparative
130 0 ▶ if ($lowerform =~ /er($|-)|^more-|^less-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /er($|-)|^more-|^less-/) 0 0 0
131 0 push @possible, qw(JJR RBR);
132 }
133  
134 # superlative
135 0 ▶ if ($lowerform =~ /est($|-)|^most-|^least/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /est($|-)|^most-|^least/) 0 0 0
136 0 push @possible, qw(JJS RBS);
137 }
138  
139 # -ing form
140 0 ▶ if ( $lowerform =~ /ing$|[^aeiouy]in$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /ing$|[^aeiouy]in$/) 0 0 0
141 0 push @possible, qw(VBG);
142 }
143  
144 # third person
145 0 ▶ if ( $lowerform =~ /[^s]s$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /[^s]s$/) { } 0 0 0
146 0 push @possible, qw(VBZ);
147 }
148 else {
149 0 push @possible, qw(VB VBP);
150 }
151  
152 # capital-letter (proper nouns and adjactives)
153 0 ▶ if ( $lowerform ne $wordform ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lowerform ne $wordform) { } 0 0 0
elsif ($lowerform =~ /^[0-9']/) { } 0 0 0
154 0 push @possible, qw(NNP NNPS);
155 }
156 elsif ( $lowerform =~ /^[0-9']/ ) {
157 0 push @possible, qw(NNP);
158 }
159  
160 # symbols
161 0 ▶ if ($lowerform =~ /[^a-zA-Z0-9]+/ or $lowerform =~ /^&.*;$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /[^a-zA-Z0-9]+/ or $lowerform =~ /^&.*;$/) 0 0 0
Condition0%0/3
$lowerform =~ /[^a-zA-Z0-9]+/ or $lowerform =~ /^&.*;$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$lowerform =~ /[^a-zA-Z0-9]+/ or $lowerform =~ /^&.*;$/
$lowerform =~ /[^a-zA-Z0-9]+/ $lowerform =~ /^&.*;$/
Truth table0%0/3
A or B
A= $lowerform =~ /[^a-zA-Z0-9]+/B= $lowerform =~ /^&.*;$/
ABresult
1X1
011
000
162 {
163 0 push @possible, qw(SYM);
164 # if ($lowerform =~ /^[^a-zA-Z0-9]+$/){ # variant: make only symbols possible
165 # @possible = ('SYM', ':', ',');
166 # }
167 }
168  
169 # numbers
170 0 ▶ if ( $lowerform =~ /[-0-9]+/ or $lowerform =~ /^[ixvcmd\.]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /[-0-9]+/ or $lowerform =~ /^[ixvcmd\.]+$/) 0 0 0
Condition0%0/3
$lowerform =~ /[-0-9]+/ or $lowerform =~ /^[ixvcmd\.]+$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$lowerform =~ /[-0-9]+/ or $lowerform =~ /^[ixvcmd\.]+$/
$lowerform =~ /[-0-9]+/ $lowerform =~ /^[ixvcmd\.]+$/
Truth table0%0/3
A or B
A= $lowerform =~ /[-0-9]+/B= $lowerform =~ /^[ixvcmd\.]+$/
ABresult
1X1
011
000
171 0 push @possible, qw(CD);
172 }
173 }
174  
175 # 3. check irregular verbs
176 0 ▶ if ( $self->preterites->{$lowerform} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preterites->{$lowerform}) 0 0 0
177 0 push @possible, qw(VBD);
178 }
179 0 ▶ if ( $self->participles->{$lowerform} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->participles->{$lowerform}) 0 0 0
180 0 push @possible, qw(VBN);
181 }
182 0 ▶ if ( $lowerform =~ /ed$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowerform =~ /ed$/) 0 0 0
183 0 push @possible, qw(VBD VBN);
184 }
185  
186 0 return @possible;
187 }
188  
189 2 1;
190  
191 =head1 NAME
192  
193 Treex::Tool::EnglishMorpho::Analysis - rule based morphological analyzer for English
194  
195  
196 =head1 VERSION
197  
198 version 2.20151102
199  
200 =head1 SYNOPSIS
201  
202 use Treex::Tool::EnglishMorpho::Analysis;
203  
204 my $analyser = Treex::Tool::EnglishMorpho::Analysis->new();
205 foreach my $wordform (qw(John loves the yellow ball of his sister .)) {
206 my @tags = $analyser->get_possible_tags($wordform);
207 print "$wordform -> @tags\n";
208 }
209  
210  
211  
212 =head1 DESCRIPTION
213  
214 Method get_possible_tags($wordform) returns the list of PennTreebank-style
215 morphological tags for the given word form.
216  
217 =head1 METHODS
218  
219 =over 4
220  
221 =item get_possible_tags
222  
223 Given wordform returns list of possible POS tags.
224  
225 =back
226  
227 =head1 AUTHORS
228  
229 Johanka Drahomíra Doležalová
230  
231 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
232  
233 Tomáš Kraut <kraut@ufal.mff.cuni.cz>
234  
235 =head1 COPYRIGHT AND LICENSE
236  
237 Copyright © 2008-2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
238  
239 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Tool/EnglishMorpho/Lemmatizer.pm ↑ Summary Block/W2A/EN/FixTags.pm »