Tool/EnglishMorpho/Lemmatizer.pm

filter: Statementstmt 16.5% 31 / 187 Branchbran 0.0% 0 / 238 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 36.8% 7 / 19 Podpod 100% 1 / 1 totaltotal 8.4% 39 / 464 CC 124 SCAR 64.0
CC
124
Cov
7%
_lemmatize_VBG
1190.0
_lemmatize_VBD_VBN
1122.0
_lemmatize_NNS_NNPS
240.0
CRAP 601.4
↑ Summary Tool/EnglishMorpho/Analysis.pm »
1 package Treex::Tool::EnglishMorpho::Lemmatizer;
2 BEGIN {
3 3 ▶ $Treex::Tool::EnglishMorpho::Lemmatizer::VERSION = '0.06903_1';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Treex::Core::Resource qw(require_file_from_share);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 has 'exceptions_filename' => (
12 is => 'ro',
13 init_arg => undef,
14 default => sub {
15 1 ▶ return require_file_from_share('/data/models/lemmatizer/en/exceptions.tsv');
Subroutine __ANON__: called CC=1 SCAR=0.0
16 },
17 3 );
18  
19 has 'negation_filename' => (
20 is => 'ro',
21 init_arg => undef,
22 default => sub {
23 0 ▶ return require_file_from_share('/data/models/lemmatizer/en/negation');
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 },
25 3 );
26  
27 3 has 'exceptions' => (
28 is => 'ro',
29 builder => '_build_exceptions',
30 init_arg => undef,
31 lazy => 1,
32 );
33  
34 3 has 'negation' => (
35 is => 'ro',
36 builder => '_build_negation',
37 init_arg => undef,
38 lazy => 1,
39 );
40  
41 3 has 'cut_negation' => (
42 isa => 'Bool',
43 default => 1,
44 reader => 'cut_negation',
45 );
46  
47 3 has 'lowercase_proper_names' => (
48 isa => 'Bool',
49 default => 0,
50 reader => 'lowercase_proper_names',
51 );
52  
53 3 my $V = qr/[aeiou]/;
54 3 my $VY = qr/[aeiouy]/;
55 3 my $C = qr/[bcdfghjklmnpqrstvwxyz]/;
56 3 my $CXY = qr/[bcdfghjklmnpqrstvwxz]/;
57 3 my $S = qr/([sxz]|[cs]h)/;
58 3 my $S2 = qr/(ss|zz)/;
59 3 my $PRE = qr/(be|ex|in|mis|pre|pro|re)/;
60  
61 #The most importat sub:
62 #Input: word form and POS tag (Penn style)
63 #Output: lemma and was_negative_prefix
64 sub lemmatize {
65 0 ▶ my ( $self, $word, $tag ) = @_;
Subroutine lemmatize: not called CC=6 SCAR=37.4
Pod documented
66 0 my $negative_prefix = 0;
67  
68 0 ▶ if ( ( $tag !~ /^NNP/ || $self->lowercase_proper_names ) && $word ne 'I' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (!($tag =~ /^NNP/) || $self->lowercase_proper_names and $word ne "I") 0 0 0
Condition0%0/6
!($tag =~ /^NNP/) || $self->lowercase_proper_names and $word ne "I"
!ll&&!rl&&r
000
not $tag =~ /^NNP/ or $self->lowercase_proper_names
l!l&&r!l&&!r
000
MC/DC0%0/3
!($tag =~ /^NNP/) || $self->lowercase_proper_names and $word ne "I"
not $tag =~ /^NNP/ $self->lowercase_proper_names $word ne "I"
Truth table0%0/5
not(A) or B and C
A= not $tag =~ /^NNP/B= $self->lowercase_proper_namesC= $word ne "I"
ABCresult
00X0
1X00
0100
1X11
0111
69 0 $word = lc $word;
70 }
71  
72 0 my $entry = $self->exceptions->{$tag}{$word};
73 0 ▶ if ($entry) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry) { } 0 0 0
74 0 return @$entry;
75  
76 }
77 else {
78 0 ▶ if ( $self->cut_negation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cut_negation) 0 0 0
79 0 ( $word, $negative_prefix ) = $self->_cut_negative_prefix( $word, $tag );
80 }
81 0 return ( $self->_lemmatize_by_rules( $word, $tag ), $negative_prefix );
82 }
83 }
84  
85 sub _cut_negative_prefix {
86 0 ▶ my ( $self, $word, $tag ) = @_;
Subroutine _cut_negative_prefix: not called CC=3 SCAR=24.8
87  
88 # We are interested only in adjectives, adverbs and nouns.
89 # English verbs are negated usually by "not" (don't,...).
90 # Proper nouns (NNP,NNPS) are also left unchanged (Disney, Intel, Irvin... Non-IBM).
91 0 ▶ if ( $tag =~ /^(J.*|R.*|NN|NNS)$/ and $word =~ $self->negation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^(J.*|R.*|NN|NNS)$/ and $word =~ $self->negation) 0 0 0
Condition0%0/3
$tag =~ /^(J.*|R.*|NN|NNS)$/ and $word =~ $self->negation
!ll&&!rl&&r
000
MC/DC0%0/2
$tag =~ /^(J.*|R.*|NN|NNS)$/ and $word =~ $self->negation
$tag =~ /^(J.*|R.*|NN|NNS)$/ $word =~ $self->negation
Truth table0%0/3
A and B
A= $tag =~ /^(J.*|R.*|NN|NNS)$/B= $word =~ $self->negation
ABresult
0X0
100
111
92 0 $word =~ s/^(un|in|im|non-?|dis-?|il|ir)//;
93 0 return ( $word, 1 );
94 }
95 0 return ( $word, 0 );
96 }
97  
98 sub _lemmatize_NNS_NNPS {
99 0 ▶ my ( $self, $word ) = @_;
Subroutine _lemmatize_NNS_NNPS: not called CC=15 SCAR=54.8
100 0 ▶ return $word if $word =~ s/men$/man/; #over 600 words (in BNC)
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/men$/man/ 0 0 0
101 0 ▶ return $word if $word =~ s/shoes$/shoe/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/shoes$/shoe/ 0 0 0
102 0 ▶ return $word if $word =~ s/wives$/wife/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/wives$/wife/ 0 0 0
103 0 ▶ return $word if $word =~ s/(${C}us)es$/$1/; #buses bonuses
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${C}us)es$/$1/ 0 0 0
104  
105 0 ▶ return $word if $word =~ s/(${V}se)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${V}se)s$/$1/ 0 0 0
106 0 ▶ return $word if $word =~ s/(.${CXY}z)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.${CXY}z)es$/$1/ 0 0 0
107 0 ▶ return $word if $word =~ s/(${VY}ze)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${VY}ze)s$/$1/ 0 0 0
108 0 ▶ return $word if $word =~ s/($S2)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($S2)es$/$1/ 0 0 0
109 0 ▶ return $word if $word =~ s/(.${V}rse)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.${V}rse)s$/$1/ 0 0 0
110 0 ▶ return $word if $word =~ s/onses$/onse/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/onses$/onse/ 0 0 0
111 0 ▶ return $word if $word =~ s/($S)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($S)es$/$1/ 0 0 0
112  
113 0 ▶ return $word if $word =~ s/(.$C)ies$/$1y/; #ponies vs ties
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.$C)ies$/$1y/ 0 0 0
114 0 ▶ return $word if $word =~ s/(${CXY}o)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}o)es$/$1/ 0 0 0
115 0 ▶ return $word if $word =~ s/s$//;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/s$// 0 0 0
116 0 return $word;
117 }
118  
119 sub _lemmatize_VBG { ## no critic (Subroutines::ProhibitExcessComplexity) this is complex
120 0 ▶ my ( $self, $word ) = @_;
Subroutine _lemmatize_VBG: not called CC=34 SCAR=70.8
121 0 ▶ return $word if $word =~ s/(${CXY}z)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}z)ing$/$1/ 0 0 0
122 0 ▶ return $word if $word =~ s/(${VY}z)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${VY}z)ing$/$1e/ 0 0 0
123 0 ▶ return $word if $word =~ s/($S2)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($S2)ing$/$1/ 0 0 0
124 0 ▶ return $word if $word =~ s/($C${V}ll)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C${V}ll)ing$/$1/ 0 0 0
125 0 ▶ return $word if $word =~ s/($C${V}($CXY)\2)ing$/$1/; #cancel-ling vs call-ing - exception is needed
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C$V($CXY)\2)ing$/$1/ 0 0 0
126 0 ▶ return $word if $word =~ s/^($CXY)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($CXY)ing$/$1/ 0 0 0
127 0 ▶ return $word if $word =~ s/^($PRE*$C${V}ng)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($PRE*$C${V}ng)ing$/$1/ 0 0 0
128 0 ▶ return $word if $word =~ s/icking$/ick/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/icking$/ick/ 0 0 0
129 0 ▶ return $word if $word =~ s/(${C}in)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${C}in)ing$/$1e/ 0 0 0
130 0 ▶ return $word if $word =~ s/($C$V[npwx])ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C${V}[npwx])ing$/$1/ 0 0 0
131 0 ▶ return $word if $word =~ s/(qu$V${C})ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(qu$V$C)ing$/$1e/ 0 0 0
132 0 ▶ return $word if $word =~ s/(u${V}d)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(u${V}d)ing$/$1e/ 0 0 0
133 0 ▶ return $word if $word =~ s/(${C}let)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${C}let)ing$/$1e/ 0 0 0
134 0 ▶ return $word if $word =~ s/^($PRE*$C+[ei]t)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($PRE*$C+[ei]t)ing$/$1e/ 0 0 0
135 0 ▶ return $word if $word =~ s/([ei]t)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([ei]t)ing$/$1/ 0 0 0
136 0 ▶ return $word if $word =~ s/($PRE$CXY${CXY}eat)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($PRE$CXY${CXY}eat)ing$/$1/ 0 0 0
137 0 ▶ return $word if $word =~ s/($V$CXY${CXY}eat)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$CXY${CXY}eat)ing$/$1e/ 0 0 0
138 0 ▶ return $word if $word =~ s/(.[eo]at)ing$/$1/; #treating vs creating
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.[eo]at)ing$/$1/ 0 0 0
139 0 ▶ return $word if $word =~ s/(.${V}at)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.${V}at)ing$/$1e/ 0 0 0
140 0 ▶ return $word if $word =~ s/($V$V[cgsv])ing$/$1e/; #announcing increasing
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V${V}[cgsv])ing$/$1e/ 0 0 0
141 0 ▶ return $word if $word =~ s/($V$V$C)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$V$C)ing$/$1/ 0 0 0
142 0 ▶ return $word if $word =~ s/(.[rw]l)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.[rw]l)ing$/$1/ 0 0 0
143 0 ▶ return $word if $word =~ s/(.th)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.th)ing$/$1e/ 0 0 0
144 0 ▶ return $word if $word =~ s/($CXY[cglsv])ing$/$1e/; #involving
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}[cglsv])ing$/$1e/ 0 0 0
145 0 ▶ return $word if $word =~ s/($CXY$CXY)ing$/$1/; #reporting
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($CXY$CXY)ing$/$1/ 0 0 0
146 0 ▶ return $word if $word =~ s/uing$/ue/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/uing$/ue/ 0 0 0
147 0 ▶ return $word if $word =~ s/($VY$VY)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($VY$VY)ing$/$1/ 0 0 0
148 0 ▶ return $word if $word =~ s/ying$/y/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/ying$/y/ 0 0 0
149 0 ▶ return $word if $word =~ s/(${CXY}o)ing$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}o)ing$/$1/ 0 0 0
150 0 ▶ return $word if $word =~ s/^($PRE*$C+or)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($PRE*$C+or)ing$/$1e/ 0 0 0
151 0 ▶ return $word if $word =~ s/($C[clt]or)ing$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${C}[clt]or)ing$/$1e/ 0 0 0
152 0 ▶ return $word if $word =~ s/([eo]r)ing$/$1/; #offering
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([eo]r)ing$/$1/ 0 0 0
153 0 ▶ return $word if $word =~ s/ing$/e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/ing$/e/ 0 0 0
154 0 return $word;
155 }
156  
157 sub _lemmatize_VBD_VBN { ## no critic (Subroutines::ProhibitExcessComplexity) this is complex
158 0 ▶ my ( $self, $word ) = @_;
Subroutine _lemmatize_VBD_VBN: not called CC=33 SCAR=70.2
159 0 ▶ return $word if $word =~ s/en$/e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/en$/e/ 0 0 0
160 0 ▶ return $word if $word =~ s/(${CXY}z)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}z)ed$/$1/ 0 0 0
161 0 ▶ return $word if $word =~ s/(${VY}z)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${VY}z)ed$/$1e/ 0 0 0
162 0 ▶ return $word if $word =~ s/($S2)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($S2)ed$/$1/ 0 0 0
163 0 ▶ return $word if $word =~ s/($C${V}ll)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C${V}ll)ed$/$1/ 0 0 0
164 0 ▶ return $word if $word =~ s/($C${V}($CXY)\2)ed$/$1/; #cancel-led vs call-ed - wordlist is needed
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C$V($CXY)\2)ed$/$1/ 0 0 0
165 0 ▶ return $word if $word =~ s/^($CXY)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($CXY)ed$/$1/ 0 0 0
166 0 ▶ return $word if $word =~ s/^($PRE*$C${V}ng)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($PRE*$C${V}ng)ed$/$1/ 0 0 0
167 0 ▶ return $word if $word =~ s/icked$/ick/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/icked$/ick/ 0 0 0
168 0 ▶ return $word if $word =~ s/(${C}(in|[clnt]or))ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C(in|[clnt]or))ed$/$1e/ 0 0 0
169 0 ▶ return $word if $word =~ s/($C$V[npwx])ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C${V}[npwx])ed$/$1/ 0 0 0
170 0 ▶ return $word if $word =~ s/^($PRE*$C+or)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($PRE*$C+or)ed$/$1e/ 0 0 0
171 0 ▶ return $word if $word =~ s/([eo]r)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([eo]r)ed$/$1/ 0 0 0
172 0 ▶ return $word if $word =~ s/(${C})ied$/$1y/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C)ied$/$1y/ 0 0 0
173 0 ▶ return $word if $word =~ s/(qu$V${C})ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(qu$V$C)ed$/$1e/ 0 0 0
174 0 ▶ return $word if $word =~ s/(u${V}d)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(u${V}d)ed$/$1e/ 0 0 0
175 0 ▶ return $word if $word =~ s/(${C}let)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${C}let)ed$/$1e/ 0 0 0
176 0 ▶ return $word if $word =~ s/^($PRE*$C+[ei]t)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/^($PRE*$C+[ei]t)ed$/$1e/ 0 0 0
177 0 ▶ return $word if $word =~ s/([ei]t)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([ei]t)ed$/$1/ 0 0 0
178 0 ▶ return $word if $word =~ s/($PRE$CXY${CXY}eat)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($PRE$CXY${CXY}eat)ed$/$1/ 0 0 0
179 0 ▶ return $word if $word =~ s/($V$CXY${CXY}eat)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$CXY${CXY}eat)ed$/$1e/ 0 0 0
180 0 ▶ return $word if $word =~ s/(.[eo]at)ed$/$1/; #treated vs created
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.[eo]at)ed$/$1/ 0 0 0
181 0 ▶ return $word if $word =~ s/(.${V}at)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.${V}at)ed$/$1e/ 0 0 0
182 0 ▶ return $word if $word =~ s/($V$V[cgsv])ed$/$1e/; #announced
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V${V}[cgsv])ed$/$1e/ 0 0 0
183 0 ▶ return $word if $word =~ s/($V$V$C)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$V$C)ed$/$1/ 0 0 0
184 0 ▶ return $word if $word =~ s/(.[rw]l)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.[rw]l)ed$/$1/ 0 0 0
185 0 ▶ return $word if $word =~ s/(.th)ed$/$1e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.th)ed$/$1e/ 0 0 0
186 0 ▶ return $word if $word =~ s/ued$/ue/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/ued$/ue/ 0 0 0
187 0 ▶ return $word if $word =~ s/($CXY[cglsv])ed$/$1e/; #involved
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}[cglsv])ed$/$1e/ 0 0 0
188 0 ▶ return $word if $word =~ s/($CXY$CXY)ed$/$1/; #reported
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($CXY$CXY)ed$/$1/ 0 0 0
189 0 ▶ return $word if $word =~ s/($VY$VY)ed$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($VY$VY)ed$/$1/ 0 0 0
190 0 ▶ return $word if $word =~ s/ed$/e/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/ed$/e/ 0 0 0
191 0 return $word;
192 }
193  
194 sub _lemmatize_VBZ {
195 0 ▶ my ( $self, $word ) = @_;
Subroutine _lemmatize_VBZ: not called CC=11 SCAR=48.8
196 0 ▶ return $word if $word =~ s/(${V}se)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${V}se)s$/$1/ 0 0 0
197 0 ▶ return $word if $word =~ s/(.${CXY}z)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.${CXY}z)es$/$1/ 0 0 0
198 0 ▶ return $word if $word =~ s/(${VY}ze)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${VY}ze)s$/$1/ 0 0 0
199 0 ▶ return $word if $word =~ s/($S2)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($S2)es$/$1/ 0 0 0
200 0 ▶ return $word if $word =~ s/(.${V}rse)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.${V}rse)s$/$1/ 0 0 0
201 0 ▶ return $word if $word =~ s/onses$/onse/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/onses$/onse/ 0 0 0
202 0 ▶ return $word if $word =~ s/($S)es$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($S)es$/$1/ 0 0 0
203  
204 0 ▶ return $word if $word =~ s/(.$C)ies$/$1y/; #tries, relies vs lies
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.$C)ies$/$1y/ 0 0 0
205 0 ▶ return $word if $word =~ s/(${CXY}o)es$/$1/; #does, undergoes
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(${CXY}o)es$/$1/ 0 0 0
206 0 ▶ return $word if $word =~ s/(.)s$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.)s$/$1/ 0 0 0
207 0 return $word;
208 }
209  
210 sub _lemmatize_JJR_RBR {
211 0 ▶ my ( $self, $word ) = @_;
Subroutine _lemmatize_JJR_RBR: not called CC=10 SCAR=47.0
212 0 ▶ return $word if $word =~ s/([^e]ll)er$/$1/; #smaller
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([^e]ll)er$/$1/ 0 0 0
213 0 ▶ return $word if $word =~ s/($C)\1er$/$1/; #bigger
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C)\1er$/$1/ 0 0 0
214 0 ▶ return $word if $word =~ s/ier$/y/; #earlier
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/ier$/y/ 0 0 0
215 0 ▶ return $word if $word =~ s/($V$V$C)er$/$1/; #weaker
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$V$C)er$/$1/ 0 0 0
216 0 ▶ return $word if $word =~ s/($C$V[npwx])er$/$1/; #lower
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C${V}[npwx])er$/$1/ 0 0 0
217 0 ▶ return $word if $word =~ s/($V$C)er$/$1e/; #nicer wider
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$C)er$/$1e/ 0 0 0
218 0 ▶ return $word if $word =~ s/([bcdfghjklmpqrstvwxz][cglsv])er$/$1e/; #larger,stranger vs stronger, younger
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([bcdfghjklmpqrstvwxz][cglsv])er$/$1e/ 0 0 0
219 0 ▶ return $word if $word =~ s/([ue])er$/$1e/; #freer
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([ue])er$/$1e/ 0 0 0
220 0 ▶ return $word if $word =~ s/er$//; #harder
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/er$// 0 0 0
221 0 return $word;
222 }
223  
224 sub _lemmatize_JJS_RBS {
225 0 ▶ my ( $self, $word ) = @_;
Subroutine _lemmatize_JJS_RBS: not called CC=9 SCAR=45.0
226 0 ▶ return $word if $word =~ s/([^e]ll)est$/$1/; #smallest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([^e]ll)est$/$1/ 0 0 0
227 0 ▶ return $word if $word =~ s/(.)\1est$/$1/; #biggest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.)\1est$/$1/ 0 0 0
228 0 ▶ return $word if $word =~ s/iest$/y/; #earliest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/iest$/y/ 0 0 0
229 0 ▶ return $word if $word =~ s/($V$V$C)est$/$1/; #weakest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$V$C)est$/$1/ 0 0 0
230 0 ▶ return $word if $word =~ s/($C$V[npwx])est$/$1/; #lowest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($C${V}[npwx])est$/$1/ 0 0 0
231 0 ▶ return $word if $word =~ s/($V$C)est$/$1e/; #nicest widest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/($V$C)est$/$1e/ 0 0 0
232 0 ▶ return $word if $word =~ s/([bcdfghjklmpqrstvwxz][cglsv])est$/$1e/; #largest vs strongest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/([bcdfghjklmpqrstvwxz][cglsv])est$/$1e/ 0 0 0
233 0 ▶ return $word if $word =~ s/(.{3,})est$/$1/; #hardest
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ s/(.{3,})est$/$1/ 0 0 0
234 0 return $word;
235 }
236  
237 sub _lemmatize_by_rules {
238 0 ▶ my ( $self, $word, $tag ) = @_;
Subroutine _lemmatize_by_rules: not called CC=8 SCAR=42.8
239  
240 0 ▶ my $lemma = $tag =~ /NNP?S/
Branch: 6 branches
BranchTrueFalseTotal
$tag =~ /NNP?S/ ? : 0 0 0
$tag =~ /^VBG/ ? : 0 0 0
$tag =~ /VB[DN]/ ? : 0 0 0
$tag eq 'VBZ' ? : 0 0 0
$tag =~ /JJR|RBR/ ? : 0 0 0
$tag =~ /JJS|RBS/ ? : 0 0 0
241 ? $self->_lemmatize_NNS_NNPS($word)
242 : $tag =~ /^VBG/ ? $self->_lemmatize_VBG($word)
243 : $tag =~ /VB[DN]/ ? $self->_lemmatize_VBD_VBN($word)
244 : $tag eq 'VBZ' ? $self->_lemmatize_VBZ($word)
245 : $tag =~ /JJR|RBR/ ? $self->_lemmatize_JJR_RBR($word)
246 : $tag =~ /JJS|RBS/ ? $self->_lemmatize_JJS_RBS($word)
247 : $word
248 ;
249 0 ▶ return $word if $lemma eq ''; # Otherwise e.g. "est"->""
Branch: 1 branch
BranchTrueFalseTotal
if $lemma eq "" 0 0 0
250 0 return $lemma;
251 }
252  
253 sub _build_exceptions {
254 0 ▶ my $self = shift;
Subroutine _build_exceptions: not called CC=4 SCAR=30.0
255 0 my %exceptions;
256 0 log_debug( $self->exceptions_filename );
257 0 ▶ open my $ex_file, "<:encoding(utf-8)", $self->exceptions_filename or log_fatal($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $ex_file, "<:encoding(utf-8)", $self->exceptions_filename or log_fatal($!) 0 0 0
258 0 ▶ while (<$ex_file>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $ex_file)) 0 0 0
259 0 chomp;
260 0 my ( $word, $tag, $lemma, $negative_prefix ) = split /\t/;
261  
262 0 ▶ $negative_prefix = ( defined $negative_prefix and $negative_prefix eq '1' );
Condition0%0/3
defined $negative_prefix && $negative_prefix eq '1'
!ll&&!rl&&r
000
MC/DC0%0/2
defined $negative_prefix && $negative_prefix eq '1'
defined $negative_prefix $negative_prefix eq '1'
Truth table0%0/3
A && B
A= defined $negative_prefixB= $negative_prefix eq '1'
ABresult
0X0
100
111
263 0 $exceptions{$tag}{$word} = [ $lemma, $negative_prefix ];
264 }
265 0 close $ex_file;
266 0 return \%exceptions;
267 }
268  
269 sub _build_negation {
270 0 ▶ my $self = shift;
Subroutine _build_negation: not called CC=1 SCAR=6.9
271 0 my $pattern = '';
272 0 my @lines = read_file( $self->negation_filename, binmode => ':encoding(utf-8)', err_mode => 'log_fatal' );
273  
274 # or log_fatal('Cannot load lemmatization exceptions from ' . $self->negation_filename);
275 0 chomp(@lines);
276 0 $pattern = join '|', @lines;
277  
278 #$pattern =~ s/-/\-/g;
279 0 my $negation = qr/^($pattern)/;
280 0 return $negation;
281 }
282  
283 3 1;
284  
285 __END__
286  
287 Cutting off negative prefixes is quite discutable.
288 Even if we filter out cases when:
289 a) a word starts with (un|in|im|dis|il|ir) but it is not a prefix (Intel, disaster,...)
290 b) it is a prefix but not negative (indoor, impress,...)
291 Still there are other cases, when etymologicaly it is a negative prefix, but...
292 unease, uneasily, uneasiness,... is definitelly not a negation of ease, easily, easiness
293  
294 indiscriminately ??
295 indiscriminate ??
296  
297 =pod
298  
299 =head1 NAME
300  
301 Treex::Tool::EnglishMorpho::Lemmatizer - rule based lemmatizer for English
302  
303 =head1 VERSION
304  
305 version 0.06903_1
306  
307 =head1 SYNOPSIS
308  
309 use Treex::Tool::EnglishMorpho::Lemmatizer;
310 my ($word, $tag) = qw( goes VBZ );
311 my ($lemma, $neg) = Treex::Tool::EnglishMorpho::Lemmatizer::lemmatize($word, $tag);
312  
313 =head1 METHODS
314  
315 =over 4
316  
317 =item lemmatize
318  
319 Accepts pair of word and tag.
320 Produces pair with its lemma and indication if word was negation
321  
322 =back
323  
324 =head1 DESCRIPTION
325  
326 Covers:
327  
328 =over
329  
330 =item * noun -s (dogs -> dog, ponies -> pony,..., mice -> mouse)
331  
332 =item * verb -s (does -> do,...)
333  
334 =item * verb -ing
335  
336 =item * verb -ed, -en
337  
338 =item * adjective/adverb -er
339  
340 =item * adjective/adverb -est
341  
342 =item * cut off negative prefixes (un|in|im|non|dis|il|ir)
343  
344 =back
345  
346 =head2 Input requirements
347  
348 =over
349  
350 =item Tokenization
351  
352 I<doesn't> should be tokenized as two words: I<does> and I<n't>
353 (It will be lemmatized as I<do> and I<not>).
354  
355 =item Tagging
356  
357 Correct tagging (Penn style) is quite crucial for Lemmatizer to work.
358 For example it doesn't change words with tags NN and NNP
359 (it changes only NNS and NNPS). So (I<pence>, NN) -> I<pence>,
360 but (I<pence>, NNS) -> I<penny>.
361  
362 =back
363  
364 =head2 Differences from the previous implementation
365  
366 Modul C<PEDT::MorphologyAnalysis> uses Morpha (written in Flex)
367 and in some cases gives different lemmatization.
368  
369 =over
370  
371 =item Adverbs and adjectives.
372  
373 Morpha leaves comparatives and superlatives unchanged.
374 C<PEDT::MorphologyAnalysis> does only basic analysis (I<later> -> I<lat>).
375  
376 =item Capitalization of proper names
377  
378 =item Changes of NN
379  
380 =item Latin words
381  
382 Declination of words with latin origin is not covered by any Lemmatizer
383 rules on purpose.
384 There are few widely known english words with latin origin which are
385 (or should be) covered by exception files (f.e. indices NNS -> index).
386 In my opinion, it is better, especially for translation purposes,
387 to leave the other latin words unchanged. Mostly they will have the same
388 form also in the target language (biological terms like Spheniscidae).
389 BTW: Errors made by Morpha latin fallbacks are sometimes funny:
390 sci-fi -> sci-fus, Mitsubishi -> mitsubishus, Shanghai -> shanghaus,...
391  
392 =back
393  
394 =head1 TODO
395  
396 =over
397  
398 =item * this POD documentation !!!
399  
400 =item * better list of exceptions
401  
402 =item * change exceptions format from tsv to stored perl hash
403  
404 =back
405  
406 =head1 AUTHO
407  
408 Martin Popel <popel@ufal.mff.cuni.cz>
409  
410 =head1 COPYRIGHT
411  
412 Copyright © 2008 - 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
413  
414 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
↑ Summary Tool/EnglishMorpho/Analysis.pm »