lib/Text/WagnerFischer/Armenian.pm

filter: Statementstmt 88.1% 89 / 101 Branchbran 81.2% 26 / 32 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 88.3% 136 / 154 CC 24 SCAR 24.5
CC
24
Cov
87%
distance
18.7
_am_weight
9.0
am_lc
2.0
CRAP 11.6
↑ Summary
1 package Text::WagnerFischer::Armenian;
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Text::WagnerFischer::Armenian - a variation on Text::WagnerFischer for Armenian-language strings
8  
9 =head1 SYNOPSIS
10  
11 use Text::WagnerFischer::Armenian qw( distance );
12 use utf8; # for the Armenian characters in the source code
13  
14 print distance("ձեռն", "ձեռան") . "\n";
15 # "dzerrn -> dzerran"; prints 1
16 print distance("ձեռն", "ձերն") . "\n";
17 # "dzerrn -> dzern"; prints 0.5
18 print distance("կինք", "կին") . "\n";
19 # "kin" -> "kink'"; prints 0.5
20 my @words = qw( զօրսն Զորս զզօրսն );
21 my @distances = distance( "զօրս", @words );
22 print "@distances\n";
23 # "zors" -> "zorsn, Zors, zzorsn"
24 # prints "0.5 0.25 1"
25  
26 # Change the cost of a letter case mismatch to 1
27 my $edit_values = [ 0, 1, 1, 1, 0.5, 0.5, 0.5 ],
28 print distance( $edit_values, "ձեռն", "Ձեռն" ) . "\n";
29 # "dzerrn" -> "DZerrn"; prints 1
30  
31 =head1 DESCRIPTION
32  
33 This module implements the Wagner-Fischer distance algorithm modified
34 for Armenian strings. The Armenian language has a number of
35 single-letter prefixes and suffixes which, while not changing the
36 basic meaning of the word, function as definite articles,
37 prepositions, or grammatical markers. These changes, and letter
38 substitutions that represent vocalic equivalence, should be counted as
39 a smaller edit distance than a change that is a normal character
40 substitution.
41  
42 The Armenian weight function recognizes four extra edit types:
43  
44 / a: x = y (cost for letter match)
45 | b: x = - or y = - (cost for letter insertion/deletion)
46 w( x, y ) = | c: x != y (cost for letter mismatch)
47 | d: x = X (cost for case mismatch)
48 | e: x ~ y (cost for letter vocalic equivalence)
49 | f: x = (z|y|ts) && y = - (or vice versa)
50 | (cost for grammatic prefix)
51 | g: x = (n|k'|s|d) && y = - (or vice versa)
52 \ (cost for grammatic suffix)
53  
54  
55 =cut
56  
57 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use Text::WagnerFischer;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 1 my( %VocalicEquivalence, @Prefixes, @Suffixes, $REFC );
65  
66 1 our $VERSION = "0.04";
67 1 our @EXPORT_OK = qw( &distance &am_lc );
68  
69 # Set new default costs:
70 #
71 # WagnerFischer : equal, insert/delete, mismatch,
72 # LetterCaseEquiv : same word, case mismatch
73 # VocalicEquiv : letter that changed with pronunciation shift
74 # PrefixAddDrop : same word, one has prefix e.g. preposition form "y-"
75 # SuffixAddDrop : same word, one has suffix e.g. definite article "-n"
76 1 $REFC = [ 0, 1, 1, 0.25, 0.5, 0.5, 0.5 ]; # mid-word: no pre/suffix
77  
78 1 %VocalicEquivalence = (
79 'բ' => [ 'պ' ],
80 'գ' => [ 'ք', 'կ' ],
81 'դ' => [ 'տ' ],
82 'ե' => [ 'է' ],
83 'է' => [ 'ե' ],
84 'թ' => [ 'տ' ],
85 'լ' => [ 'ղ' ],
86 'կ' => [ 'գ', 'ք' ],
87 'ղ' => [ 'լ' ],
88 'յ' => [ '՛' ], # Only in manuscripts
89 'ո' => [ 'օ' ],
90 'պ' => [ 'բ', 'փ' ],
91 'ռ' => [ 'ր' ],
92 'վ' => [ 'ւ' ],
93 'տ' => [ 'դ', 'թ'],
94 'ր' => [ 'ռ' ],
95 'ւ' => [ 'վ' ],
96 'փ' => [ 'պ', 'ֆ' ],
97 'ք' => [ 'գ', 'կ' ],
98 'օ' => [ 'ո' ],
99 'ֆ' => [ 'փ' ],
100 '՛' => [ 'յ' ], # Only in manuscripts
101 );
102  
103 1 @Prefixes = qw( զ ց յ );
104 1 @Suffixes = qw( ն ս դ ք );
105  
106 sub _am_weight
107 1858 ▶{
Subroutine _am_weight: called CC=9 SCAR=22.0
108 1858 my ($x,$y,$refc)=@_;
109  
110 1858 ▶ if ($x eq $y) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x eq $y) { } 104 1754 1858
elsif (am_lc($x) eq am_lc($y)) { } 2 1752 1754
111 # Simple case: exact match.
112 104 return $refc->[0];
113 } elsif( am_lc( $x ) eq am_lc( $y ) ) {
114 # Almost as simple: case difference.
115 2 return $refc->[3]; # Vocalic equivalence.
116 }
117  
118 # Got this far? We have to play games with prefixes, suffixes,
119 # similar-letter substitution, and the like.
120  
121 # Downcase both of them.
122 1752 $x = am_lc( $x );
123 1752 $y = am_lc( $y );
124  
125 1752 ▶ if ( ($x eq '-') or ($y eq '-') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq '-' or $y eq '-') { } 1316 436 1752
Condition100%3/3
$x eq "-" or $y eq "-"
l!l&&r!l&&!r
658658436
MC/DC100%2/2
$x eq "-" or $y eq "-"
$x eq "-" $y eq "-"
Truth table100%3/3
A or B
A= $x eq "-"B= $y eq "-"
ABresult
1X1
011
000
126 # Are we dealing with a prefix or a suffix?
127 # print STDERR "x is $x; y is $y;\n";
128 1316 ▶ if( grep( /(\Q$x\E|\Q$y\E)/, @Prefixes ) > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep(/(\Q$x\E|\Q$y\E)/, @Prefixes) > 0) { } 108 1208 1316
elsif (grep(/(\Q$x\E|\Q$y\E)/, @Suffixes) > 0) { } 130 1078 1208
129 108 return $refc->[5];
130 } elsif( grep( /(\Q$x\E|\Q$y\E)/, @Suffixes ) > 0 ) {
131 130 return $refc->[6];
132 } else {
133 # Normal insert/delete
134 1078 return $refc->[1];
135 }
136 } else {
137 436 ▶ if( exists( $VocalicEquivalence{$x} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $VocalicEquivalence{$x}) { } 156 280 436
138 # Same word, vocalic shift?
139 # N.B. This will mistakenly give less weight to a few genuinely
140 # different words, e.g. the verbs "գամ" vs. "կամ". I can live with that.
141 156 my @equivs = @{$VocalicEquivalence{$x}};
142 156 ▶ my $val = grep (/$y/, @equivs ) ? $refc->[4] : $refc->[2];
Branch: 1 branch
BranchTrueFalseTotal
grep(/$y/, @equivs) ? : 8 148 156
143 156 return $val;
144 } else {
145 280 return $refc->[2];
146 }
147 }
148 }
149  
150 # Annoyingly, I need to copy this whole damn thing because I need to change
151 # the refc mid-stream.
152  
153 =head1 SUBROUTINES
154  
155 =over
156  
157 =item B<distance>( \@editweight, $string1, $string2, [ .. $stringN ] );
158  
159 =item B<distance>( $string1, $string2, [ .. $stringN ] );
160  
161 The main exported function of this module. Takes a list of two or
162 more strings and returns the edit distance between the first string
163 and each of the others. The "edit_distances" array is an optional
164 first argument, with which users may override the default edit
165 penalties, as described above.
166  
167 =cut
168  
169 26 ▶sub distance {
Subroutine distance: called CC=15 SCAR=29.3
Pod documented
170 26 my ($refcarg,$s,@t)=@_;
171  
172 # The refc values are as documented above:
173 # 0. x,x; 1. x,''; 2. x,y; 3. x,X; 4. d,t; 5. x,zx; 6. x,xn
174 # 6 only applies at beginnings of words, and 7 only applies at
175 # ends.
176  
177 26 my $refc = [];
178 26 ▶ if (!@t) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @t) { } 13 13 26
elsif (ref $refcarg ne 'ARRAY') { } 0 13 13
179 # Two args...
180 13 ▶ if (ref($refcarg) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $refcarg ne 'ARRAY') { } 13 0 13
181 # the first of which is a string...
182 13 ▶ if (ref($s) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s ne 'ARRAY') { } 13 0 13
183 # ...and the second of which is a string.
184 # Use default refc set.
185 13 $t[0]=$s;
186 13 $s=$refcarg;
187 13 push( @$refc, @$REFC );
188 } else {
189 # ...one of which is an array. Croak.
190 0 require Carp;
191 0 Carp::croak("Text::WagnerFischer: second string is needed");
192 }
193 } else {
194 # one refc, and one string. Croak.
195 0 require Carp;
196 0 Carp::croak("Text::WagnerFischer: second string is needed");
197 }
198 } elsif (ref($refcarg) ne "ARRAY") {
199 # Three or more args, all strings.
200 # Use default refc set.
201 0 unshift @t,$s;
202 0 $s=$refcarg;
203 0 push( @$refc, @$REFC );
204 } else {
205 # A refc array and (presumably) some strings.
206 # Copy the passed array into our own array, because
207 # we are going to mutate our copy.
208 13 push( @$refc, @$refcarg );
209 }
210
211 # Set up the refc arrays in three different formats - one for word
212 # beginnings, one for word ends, and one for everything else.
213 26 my( $refc_start, $refc_end ) = ( [], [] );
214 26 push( @$refc_start, @$refc );
215 # Count suffixes as normal add/del.
216 26 $refc_start->[6] = $refc->[1];
217 26 push( @$refc_end, @$refc );
218 26 $refc_end->[5] = $refc->[1];
219  
220 # Now alter our main refc, which should no longer
221 # care about prefixes or suffixes.
222 26 $refc->[5] = $refc->[1];
223 26 $refc->[6] = $refc->[1];
224
225  
226 # binmode STDERR, ":utf8"; # for debugging
227 # Start the real string comparison.
228 26 my $n=length($s);
229 26 my @result;
230
231 26 foreach my $t (@t) {
232
233 26 my @d;
234
235 26 my $m=length($t);
236 0 ▶ if(!$n) {push @result,$m*$refc->[1];next}
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 26 26
237 0 ▶ if(!$m) {push @result,$n*$refc->[1];next}
Branch: 1 branch
BranchTrueFalseTotal
unless ($m) 0 26 26
238
239 26 $d[0][0]=0;
240
241 # Populate the zero row.
242 # Cannot assume that blank vs. 1st letter is "add". Might
243 # be "prefix."
244 26 my $f_i = substr($s,0,1);
245 26 foreach my $i (1 .. $n) {$d[$i][0]=$i*&_am_weight('-',$f_i,$refc_start);}
246 26 my $f_j = substr($t,0,1);
247 26 foreach my $j (1 .. $m) {$d[0][$j]=$j*&_am_weight($f_j,'-',$refc_start);}
248
249 26 foreach my $i (1 .. $n) {
250 116 my $s_i=substr($s,$i-1,1);
251 116 foreach my $j (1 .. $m) {
252 # Switch to suffix refc if we are to end of either word.
253 542 ▶ $refc = $refc_end if( $i == $n || $j == $m );
Branch: 1 branch
BranchTrueFalseTotal
if $i == $n or $j == $m 206 336 542
Condition100%3/3
$i == $n or $j == $m
l!l&&r!l&&!r
11690336
MC/DC100%2/2
$i == $n or $j == $m
$i == $n $j == $m
Truth table100%3/3
A or B
A= $i == $nB= $j == $m
ABresult
1X1
011
000
254 542 my $t_i=substr($t,$j-1,1);
255
256 542 $d[$i][$j]=Text::WagnerFischer::_min($d[$i-1][$j]+_am_weight($s_i,'-',$refc),
257 $d[$i][$j-1]+_am_weight('-',$t_i,$refc),
258 $d[$i-1][$j-1]+_am_weight($s_i,$t_i,$refc));
259 }
260 }
261
262 26 my $r = $d[$n][$m];
263 ## Round up to get an integer result.
264 ## On second thought, don't.
265 # if( $r - int( $r ) > 0 ) {
266 # $r = int( $r ) + 1;
267 # }
268  
269 26 push @result, $r;
270  
271 ## Debugging statements
272 # print "\nARRAY for $s / $t\n";
273 # foreach my $arr ( @d ) {
274 # print join( " ", @$arr ) . "\n"
275 # }
276 }
277 0 ▶ if (wantarray) {return @result} else {return $result[0]}
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 26 26
278 }
279
280  
281 =item B<am_lc>( $char )
282  
283 A small utility function, useful for Armenian text. Returns the
284 lowercase version of the character passed in.
285  
286 =back
287  
288 =cut
289  
290 7012 ▶sub am_lc {
Subroutine am_lc: called CC=2 SCAR=6.9
Pod documented
291 7012 my $char = shift;
292 # Is it in the uppercase Armenian range?
293 7012 ▶ if( $char =~ /[\x{531}-\x{556}]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /[\x{531}-\x{556}]/) 158 6854 7012
294 158 my $codepoint = unpack( "U", $char );
295 158 $codepoint += 48;
296 158 $char = pack( "U", $codepoint );
297 }
298 7012 return $char;
299 }
300  
301 =head1 LIMITATIONS
302  
303 There are many cases of Armenian word equivalence that are not
304 perfectly handled by this; it is meant to be a rough heuristic for
305 comparing transcriptions of handwriting. In particular, multi-letter
306 suffixes, and some orthographic equivalence e.g "o" -> "aw", are not
307 handled at all.
308  
309 =head1 LICENSE
310  
311 This package is free software and is provided "as is" without express
312 or implied warranty. You can redistribute it and/or modify it under
313 the same terms as Perl itself.
314  
315 =head1 AUTHOR
316  
317 Tara L Andrews, L<aurum@cpan.org>
318  
319 =cut
320  
321 1 1;
↑ Summary