blib/lib/PDL/EditDistance.pm

filter: Statementstmt 89.2% 25 / 28 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 90.3% 28 / 31 CC 1 SCAR 0.0
CC
1
Cov
89%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« EditDistance.xs ↑ Summary
1 #
2 # GENERATED WITH PDL::PP from EditDistance.pd! Don't modify!
3 #
4 package PDL::EditDistance;
5  
6 2 our @EXPORT_OK = qw( edit_costs _edit_costs edit_costs_static edit_distance_full _edit_distance_full _edit_distance_full edit_align_full _edit_align_full _edit_align_full edit_distance_static _edit_distance_static _edit_distance_static edit_align_static _edit_align_static _edit_align_static align_op_insert1 align_op_insert1 align_op_insert2 align_op_insert2 align_op_match align_op_match align_op_substitute align_op_substitute align_op_insert align_op_delete align_ops edit_bestpath _edit_bestpath _edit_bestpath edit_pathtrace _edit_pathtrace _edit_pathtrace edit_lcs _edit_lcs _edit_lcs lcs_backtrace _lcs_backtrace _lcs_backtrace );
7 2 our %EXPORT_TAGS = (Func=>\@EXPORT_OK);
8  
9 2 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 2 our $VERSION = '0.10';
15 2 our @ISA = ( 'PDL::Exporter','DynaLoader' );
16 2 push @PDL::Core::PP, __PACKAGE__;
17 2 bootstrap PDL::EditDistance $VERSION;
18  
19  
20  
21  
22  
23  
24  
25  
26 #line 12 "EditDistance.pd"
27  
28 use strict;
29 use version;
30  
31 ## $PDL_ATLEAST_2_014 : avoid in-place reshape() in _edit_pdl() for PDL >= 2.014
32 ## + prior to PDL-2.014, PDL::reshape() returned a new PDL, but modifies
33 ## the calling object in-place for v2.014
34 our $PDL_ATLEAST_2_014 = version->parse($PDL::VERSION) >= version->parse("2.014");
35  
36 =pod
37  
38 =head1 NAME
39  
40 PDL::EditDistance - Wagner-Fischer edit distance and alignment for PDLs.
41  
42 =head1 SYNOPSIS
43  
44 use PDL;
45 use PDL::EditDistance;
46  
47 ##-- input PDLs
48 $a = pdl([map { ord($_) } qw(G U M B O)]);
49 $b = pdl([map { ord($_) } qw(G A M B O L)]);
50  
51 $a1 = pdl([0, map { ord($_) } qw(G U M B O)]);
52 $b1 = pdl([0, map { ord($_) } qw(G A M B O L)]);
53  
54 ##-------------------------------------------------------------
55 ## Levenshtein distance
56 $dist = edit_distance_static($a,$b, 0,1,1,1);
57 ($dist,$align) = edit_align_static($a,$b, 0,1,1,1);
58  
59 ##-------------------------------------------------------------
60 ## Wagner-Fischer distance
61 @costs = ($costMatch=0,$costInsert=1,$costDelete=1,$costSubstitute=2);
62 $dist = edit_distance_static($a,$b, @costs);
63 ($dist,$align) = edit_align_static($a,$b, @costs);
64  
65 ##-------------------------------------------------------------
66 ## General edit distance
67 $costsMatch = random($a->nelem+1, $b->nelem+1);
68 $costsIns = random($a->nelem+1, $b->nelem+1);
69 $costsDel = random($a->nelem+1, $b->nelem+1);
70 $costsSubst = random($a->nelem+1, $b->nelem+1);
71 @costs = ($costsMatch,$costsIns,$costDel,$costsSubst);
72 $dist = edit_distance_full($a,$b,@costs);
73 ($dist,$align) = edit_align_full($a,$b,@costs);
74  
75 ##-------------------------------------------------------------
76 ## Alignment
77 $op_match = align_op_match(); ##-- constant
78 $op_del = align_op_insert1(); ##-- constant
79 $op_ins = align_op_insert2(); ##-- constant
80 $op_subst = align_op_substitute(); ##-- constant
81  
82 ($apath,$bpath,$pathlen) = edit_bestpath($align);
83 ($ai,$bi,$ops,$pathlen) = edit_pathtrace($align);
84  
85 ##-------------------------------------------------------------
86 ## Longest Common Subsequence
87 $lcs = edit_lcs($a,$b);
88 ($ai,$bi,$lcslen) = lcs_backtrace($a,$b,$lcs);
89  
90 =cut
91 #line 92 "EditDistance.pm"
92  
93  
94 =head1 FUNCTIONS
95  
96 =cut
97  
98  
99  
100  
101  
102 #line 124 "EditDistance.pd"
103  
104 =pod
105  
106 =head2 _edit_pdl
107  
108 =for sig
109  
110 Signature: (a(N); [o]apdl(N+1))
111  
112 Convenience method.
113 Returns a pdl $apdl() suitable for representing $a(),
114 which can be specified as a UTF-8 or byte-string, as an arrays of numbers, or as a PDL.
115 $apdl(0) is always set to zero.
116  
117 =cut
118  
119 sub _edit_pdl {
120 if (UNIVERSAL::isa($_[0],'PDL')) {
121 return ($PDL_ATLEAST_2_014 ? $_[0]->pdl : $_[0])->flat->reshape($_[0]->nelem+1)->rotate(1);
122 }
123 #return pdl(byte,[0, map { ord($_) } split(//,$_[0])]) if (!ref($_[0]) && !utf8::is_utf8($_[0])); ##-- byte-string (old)
124 elsif (!ref($_[0])) {
125 return pdl(long,[0, unpack('C0C*',$_[0])]) if (utf8::is_utf8($_[0])); ##-- utf8-string
126 return pdl(byte,[0, unpack('U0C*',$_[0])]); ##-- byte-string
127 }
128 return pdl([0,@{$_[0]}]);
129 }
130  
131 #line 159 "EditDistance.pd"
132  
133 =pod
134  
135 =head2 edit_costs
136  
137 =for sig
138  
139 Signature: (PDL::Type type; int N; int M;
140 [o]costsMatch(N+1,M+1); [o]costsIns(N+1,M+1); [o]costsDel(N+1,M+1); [o]costsSubst(N+1,M+1))
141  
142 Convenience method.
143 Ensures existence and proper dimensionality of cost matrices for inputs
144 of length N and M.
145  
146 =cut
147  
148 sub edit_costs {
149 return _edit_costs($_[0],$_[1]+1,$_[2]+1,@_[3..$#_]);
150 }
151  
152 #line 187 "EditDistance.pd"
153  
154 =pod
155  
156 =head2 _edit_costs
157  
158 =for sig
159  
160 Signature: (PDL::Type type; int N1; int M1;
161 [o]costsMatch(N1,M1); [o]costsIns(N1,M1); [o]costsDel(N1,M1); [o]costsSubst(N1,M1))
162  
163 Low-level method.
164 Ensures existence and proper dimensionality of cost matrices for inputs
165 of length N1-1 and M1-1.
166  
167 =cut
168  
169 sub _edit_costs {
170 #my ($type,$n1,$m1,$costsMatch,$costsIns,$costsDel,$costsSubst) = @_;
171 return (_edit_matrix(@_[0..2],$_[3]),
172 _edit_matrix(@_[0..2],$_[4]),
173 _edit_matrix(@_[0..2],$_[5]),
174 _edit_matrix(@_[0..2],$_[6]));
175 }
176  
177 ##-- $matrix = _edit_matrix($type,$dim0,$dim1,$mat)
178 sub _edit_matrix {
179 return zeroes(@_[0..2]) if (!defined($_[3]));
180 $_[3]->reshape(@_[1,2]) if ($_[3]->ndims != 2 || $_[3]->dim(0) != $_[1] || $_[3]->dim(1) != $_[2]);
181 return $_[3]->type == $_[0] ? $_[3] : $_[3]->convert($_[0]);
182 }
183  
184 #line 226 "EditDistance.pd"
185  
186 =pod
187  
188 =head2 edit_costs_static
189  
190 =for sig
191  
192 Signature: (PDL::Type type; int N; int M;
193 staticCostMatch(); staticCostIns(); staticCostSubst();
194 [o]costsMatch(N+1,M+1); [o]costsIns(N+1,M+1); [o]costsDel(N+1,M+1); [o]costsSubst(N+1,M+1))
195  
196 Convenience method.
197  
198 =cut
199  
200 sub edit_costs_static {
201 #my ($type,$n,$m, $cMatch,$cIns,$cDel,$cSubst, $costsMatch,$costsIns,$costsDel,$costsSubst) = @_;
202 my @costs = edit_costs(@_[0..2],@_[7..$#_]);
203 $costs[$_] .= $_[$_+3] foreach (0..3);
204 return @costs;
205 }
206  
207 #line 258 "EditDistance.pd"
208  
209 =pod
210  
211 =head2 edit_distance_full
212  
213 =for sig
214  
215 Signature: (a(N); b(M);
216 costsMatch(N+1,M+1); costsIns(N+1,M+1); costsDel(N+1,M+1); costsSubst(N+1,M+1);
217 [o]dist(N+1,M+1); [o]align(N+1,M+1))
218  
219 Convenience method.
220 Compute the edit distance matrix for inputs $a() and $b(), and
221 cost matrices $costsMatch(), $costsIns(), $costsDel(), and $costsSubst().
222 $a() and $b() may be specified as PDLs, arrays of numbers, or as strings.
223  
224 =cut
225  
226 sub edit_distance_full {
227 return _edit_distance_full(_edit_pdl($_[0]), _edit_pdl($_[1]), @_[2..$#_]);
228 }
229 #line 230 "EditDistance.pm"
230  
231  
232 =head2 _edit_distance_full
233  
234 =for sig
235  
236 Signature: (a1(N1); b1(M1); costsMatch(N1,M1); costsIns(N1,M1); costsDel(N1,M1); costsSubst(N1,M1); [o]dist(N1,M1))
237 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
238 float double ldouble)
239  
240 =for usage
241  
242 $dist = _edit_distance_full($a1, $b1, $costsMatch, $costsIns, $costsDel, $costsSubst);
243 _edit_distance_full($a1, $b1, $costsMatch, $costsIns, $costsDel, $costsSubst, $dist); # all arguments given
244 $dist = $a1->_edit_distance_full($b1, $costsMatch, $costsIns, $costsDel, $costsSubst); # method call
245 $a1->_edit_distance_full($b1, $costsMatch, $costsIns, $costsDel, $costsSubst, $dist);
246  
247 Low-level method.
248 Compute the edit distance matrix for input PDLs $a1() and $b1() and
249 cost matrices $costsMatch(), $costsIns(), $costsDel(), and $costsSubst().
250  
251 The first elements of $a1() and $b1() are ignored.
252  
253 =pod
254  
255 Broadcasts over its inputs.
256  
257 =for bad
258  
259 C<_edit_distance_full> does not process bad values.
260 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
261  
262 =cut
263  
264  
265  
266  
267 0 *_edit_distance_full = \&PDL::_edit_distance_full;
268  
269  
270  
271  
272  
273 #line 349 "EditDistance.pd"
274  
275 =pod
276  
277 =head2 edit_align_full
278  
279 =for sig
280  
281 Signature: (a(N); b(M);
282 costsMatch(N+1,M+1); costsIns(N+1,M+1); costsDel(N+1,N+1); costsSubst(N+1,M+1);
283 [o]dist(N+1,M+1); [o]align(N+1,M+1))
284  
285 Convenience method.
286 Compute the edit distance and alignment matrices for inputs $a() and $b(), and
287 cost matrices $costsMatch(), $costsIns(), $costsDel(), and $costsSubst().
288 $a() and $b() may be specified as PDLs, arrays of numbers, or as strings.
289  
290 =cut
291  
292 sub edit_align_full {
293 return _edit_align_full(_edit_pdl($_[0]), _edit_pdl($_[1]), @_[2..$#_]);
294 }
295 #line 296 "EditDistance.pm"
296  
297  
298 =head2 _edit_align_full
299  
300 =for sig
301  
302 Signature: (a1(N1); b1(M1); costsMatch(N1,M1); costsIns(N1,M1); costsDel(N1,M1); costsSubst(N1,M1); [o]dist(N1,M1); byte [o]align(N1,M1))
303 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
304 float double ldouble)
305  
306 =for usage
307  
308 ($dist, $align) = _edit_align_full($a1, $b1, $costsMatch, $costsIns, $costsDel, $costsSubst);
309 _edit_align_full($a1, $b1, $costsMatch, $costsIns, $costsDel, $costsSubst, $dist, $align); # all arguments given
310 ($dist, $align) = $a1->_edit_align_full($b1, $costsMatch, $costsIns, $costsDel, $costsSubst); # method call
311 $a1->_edit_align_full($b1, $costsMatch, $costsIns, $costsDel, $costsSubst, $dist, $align);
312  
313 Low-level method.
314 Compute the edit distance and alignment matrix for input PDLs $a1() and $b1() and
315 cost matrices $costsMatch(), $costsIns(), $costsDel(), and $costsSubst().
316  
317 The first elements of $a1() and $b1() are ignored.
318  
319 =pod
320  
321 Broadcasts over its inputs.
322  
323 =for bad
324  
325 C<_edit_align_full> does not process bad values.
326 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
327  
328 =cut
329  
330  
331  
332  
333 2 *_edit_align_full = \&PDL::_edit_align_full;
334  
335  
336  
337  
338  
339 #line 451 "EditDistance.pd"
340  
341 =pod
342  
343 =head2 edit_distance_static
344  
345 =for sig
346  
347 Signature: (a(N); b(M);
348 staticCostMatch(); staticCostIns(); staticCostDel(); staticCostSubst();
349 [o]dist(N+1,M+1))
350  
351 Convenience method.
352 Compute the edit distance matrix for inputs $a() and $b() given
353 a static cost schema @costs = ($staticCostMatch(), $staticCostIns(), $staticCostDel(), and $staticCostSubst()).
354 $a() and $b() may be specified as PDLs, arrays of numbers, or as strings.
355 Functionally equivalent to edit_distance_full($matches,@costs,$dist),
356 but slightly faster.
357  
358 =cut
359  
360 sub edit_distance_static {
361 return _edit_distance_static(_edit_pdl($_[0]), _edit_pdl($_[1]), @_[2..$#_]);
362 }
363 #line 364 "EditDistance.pm"
364  
365  
366 =head2 _edit_distance_static
367  
368 =for sig
369  
370 Signature: (a1(N1); b1(M1); costMatch(); costIns(); costDel(); costSubst(); [o]dist(N1,M1))
371 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
372 float double ldouble)
373  
374 =for usage
375  
376 $dist = _edit_distance_static($a1, $b1, $costMatch, $costIns, $costDel, $costSubst);
377 _edit_distance_static($a1, $b1, $costMatch, $costIns, $costDel, $costSubst, $dist); # all arguments given
378 $dist = $a1->_edit_distance_static($b1, $costMatch, $costIns, $costDel, $costSubst); # method call
379 $a1->_edit_distance_static($b1, $costMatch, $costIns, $costDel, $costSubst, $dist);
380  
381 Low-level method.
382 Compute the edit distance matrix for input PDLs $a1() and $b1() given a
383 static cost schema @costs = ($costMatch(), $costIns(), $costDel(), $costSubst()).
384 Functionally identitical to _edit_distance_matrix_full($matches,@costs,$dist),
385 but slightly faster.
386  
387 The first elements of $a1() and $b1() are ignored.
388  
389 =pod
390  
391 Broadcasts over its inputs.
392  
393 =for bad
394  
395 C<_edit_distance_static> does not process bad values.
396 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
397  
398 =cut
399  
400  
401  
402  
403 2 *_edit_distance_static = \&PDL::_edit_distance_static;
404  
405  
406  
407  
408  
409 #line 540 "EditDistance.pd"
410  
411 =pod
412  
413 =head2 edit_align_static
414  
415 =for sig
416  
417 Signature: (a(N); b(M);
418 staticCostMatch(); staticCostIns(); staticCostDel(); staticCostSubst();
419 [o]dist(N+1,M+1); [o]align(N+1,M+1))
420  
421 Convenience method.
422 Compute the edit distance and alignment matrices for inputs $a() and $b() given
423 a static cost schema @costs = ($staticCostMatch(), $staticCostIns(), $staticCostDel(), and $staticCostSubst()).
424 $a() and $b() may be specified as PDLs, arrays of numbers, or as strings.
425 Functionally equivalent to edit_align_full($matches,@costs,$dist),
426 but slightly faster.
427  
428 =cut
429  
430 sub edit_align_static {
431 return _edit_align_static(_edit_pdl($_[0]), _edit_pdl($_[1]), @_[2..$#_]);
432 }
433 #line 434 "EditDistance.pm"
434  
435  
436 =head2 _edit_align_static
437  
438 =for sig
439  
440 Signature: (a1(N1); b1(M1); costMatch(); costIns(); costDel(); costSubst(); [o]dist(N1,M1); byte [o]align(N1,M1))
441 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
442 float double ldouble)
443  
444 =for usage
445  
446 ($dist, $align) = _edit_align_static($a1, $b1, $costMatch, $costIns, $costDel, $costSubst);
447 _edit_align_static($a1, $b1, $costMatch, $costIns, $costDel, $costSubst, $dist, $align); # all arguments given
448 ($dist, $align) = $a1->_edit_align_static($b1, $costMatch, $costIns, $costDel, $costSubst); # method call
449 $a1->_edit_align_static($b1, $costMatch, $costIns, $costDel, $costSubst, $dist, $align);
450  
451 Low-level method.
452 Compute the edit distance and alignment matrices for input PDLs $a1() and $b1() given a
453 static cost schema @costs = ($costMatch(), $costIns(), $costDel(), $costSubst()).
454 Functionally identitical to _edit_distance_matrix_full($matches,@costs,$dist),
455 but slightly faster.
456  
457 The first elements of $a1() and $b1() are ignored.
458  
459 =pod
460  
461 Broadcasts over its inputs.
462  
463 =for bad
464  
465 C<_edit_align_static> does not process bad values.
466 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
467  
468 =cut
469  
470  
471  
472  
473 2 *_edit_align_static = \&PDL::_edit_align_static;
474  
475  
476  
477  
478  
479  
480 =head2 align_op_insert1
481  
482 =for sig
483  
484 Signature: ([o]a())
485 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
486 float double ldouble)
487  
488 =for usage
489  
490 $a = align_op_insert1();
491 align_op_insert1($a); # all arguments given
492  
493 =for ref
494  
495 Alignment matrix value constant for insertion operations on $a() string.
496  
497 =pod
498  
499 Broadcasts over its inputs.
500  
501 =for bad
502  
503 C<align_op_insert1> does not process bad values.
504 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
505  
506 =cut
507  
508  
509  
510  
511 2 *align_op_insert1 = \&PDL::align_op_insert1;
512  
513  
514  
515  
516  
517  
518 =head2 align_op_insert2
519  
520 =for sig
521  
522 Signature: ([o]a())
523 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
524 float double ldouble)
525  
526 =for usage
527  
528 $a = align_op_insert2();
529 align_op_insert2($a); # all arguments given
530  
531 =for ref
532  
533 Alignment matrix value constant for insertion operations on $a() string.
534  
535 =pod
536  
537 Broadcasts over its inputs.
538  
539 =for bad
540  
541 C<align_op_insert2> does not process bad values.
542 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
543  
544 =cut
545  
546  
547  
548  
549 2 *align_op_insert2 = \&PDL::align_op_insert2;
550  
551  
552  
553  
554  
555  
556 =head2 align_op_match
557  
558 =for sig
559  
560 Signature: ([o]a())
561 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
562 float double ldouble)
563  
564 =for usage
565  
566 $a = align_op_match();
567 align_op_match($a); # all arguments given
568  
569 =for ref
570  
571 Alignment matrix value constant for matches.
572  
573 =pod
574  
575 Broadcasts over its inputs.
576  
577 =for bad
578  
579 C<align_op_match> does not process bad values.
580 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
581  
582 =cut
583  
584  
585  
586  
587 2 *align_op_match = \&PDL::align_op_match;
588  
589  
590  
591  
592  
593  
594 =head2 align_op_substitute
595  
596 =for sig
597  
598 Signature: ([o]a())
599 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
600 float double ldouble)
601  
602 =for usage
603  
604 $a = align_op_substitute();
605 align_op_substitute($a); # all arguments given
606  
607 =for ref
608  
609 Alignment matrix value constant for substitution operations.
610  
611 =pod
612  
613 Broadcasts over its inputs.
614  
615 =for bad
616  
617 C<align_op_substitute> does not process bad values.
618 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
619  
620 =cut
621  
622  
623  
624  
625 2 *align_op_substitute = \&PDL::align_op_substitute;
626  
627  
628  
629  
630  
631 #line 672 "EditDistance.pd"
632  
633 =pod
634  
635 =head2 align_op_delete
636  
637 Alias for align_op_insert1()
638  
639 =head2 align_op_insert
640  
641 Alias for align_op_insert2()
642  
643 =cut
644  
645 *align_op_delete = \&align_op_insert1;
646 *align_op_insert = \&align_op_insert2;
647  
648 #line 693 "EditDistance.pd"
649  
650 =pod
651  
652 =head2 align_ops
653  
654 =for sig
655  
656 Signature: ([o]ops(4))
657  
658 Alignment matrix value constants 4-element pdl (match,insert1,insert2,substitute).a
659  
660 =cut
661  
662 sub align_ops { return PDL->sequence(PDL::byte(),4); }
663  
664 #line 716 "EditDistance.pd"
665  
666 =pod
667  
668 =head2 edit_bestpath
669  
670 =for sig
671  
672 Signature: (align(N+1,M+1); [o]apath(N+M+2); [o]bpath(N+M+2); [o]pathlen())
673  
674 Convenience method.
675 Compute best path through alignment matrix $align().
676 Stores paths for original input strings $a() and $b() in $apath() and $bpath()
677 respectively.
678 Negative values in $apath() and $bpath() indicate insertion/deletion operations.
679 On completion, $pathlen() holds the actual length of the paths.
680  
681 =cut
682  
683 sub edit_bestpath {
684 my ($align,$apath,$bpath,$len) = @_;
685 $len=pdl(long,$align->dim(0)+$align->dim(1)) if (!defined($len));
686 if (!defined($apath)) { $apath=zeroes(long,$len); }
687 else { $apath->reshape($len) if ($apath->nelem < $len); }
688 if (!defined($bpath)) { $bpath = zeroes(long,$len); }
689 else { $bpath->reshape($len) if ($bpath->nelem < $len); }
690 _edit_bestpath($align, $apath, $bpath, $len, $align->dim(0)-1, $align->dim(1)-1);
691 return ($apath,$bpath,$len);
692 }
693 #line 694 "EditDistance.pm"
694  
695  
696 =head2 _edit_bestpath
697  
698 =for sig
699  
700 Signature: (align(N1,M1); int [o]apath(L); int [o]bpath(L); int [o]len(); int ifinal; int jfinal)
701 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
702 float double ldouble)
703  
704 =for usage
705  
706 ($apath, $bpath, $len) = _edit_bestpath($align, $ifinal, $jfinal);
707 _edit_bestpath($align, $apath, $bpath, $len, $ifinal, $jfinal); # all arguments given
708 ($apath, $bpath, $len) = $align->_edit_bestpath($ifinal, $jfinal); # method call
709 $align->_edit_bestpath($apath, $bpath, $len, $ifinal, $jfinal);
710  
711 Low-level method.
712 Compute best path through alignment matrix $align() from final index ($ifinal,$jfinal).
713 Stores paths for (original) input strings $a() and $b() in $apath() and $bpath()
714 respectively.
715 Negative values in $apath() and $bpath() indicate insertion/deletion operations.
716 On completion, $pathlen() holds the actual length of the paths.
717  
718 =pod
719  
720 Broadcasts over its inputs.
721  
722 =for bad
723  
724 C<_edit_bestpath> does not process bad values.
725 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
726  
727 =cut
728  
729  
730  
731  
732 2 *_edit_bestpath = \&PDL::_edit_bestpath;
733  
734  
735  
736  
737  
738 #line 811 "EditDistance.pd"
739  
740 =pod
741  
742 =head2 edit_pathtrace
743  
744 =for sig
745  
746 Signature: ( align(N+1,M+1); [o]ai(L); [o]bi(L); [o]ops(L); [o]$pathlen() )
747  
748 Convenience method.
749 Compute alignment path backtrace through alignment matrix $align() from final index ($ifinal,$jfinal).
750 Stores raw paths for (original) input strings $a() and $b() in $ai() and $bi()
751 respectively.
752 Unlike edit_bestpath(), null-moves for $ai() and $bi() are not stored here as negative values.
753 Returned pdls ($ai,$bi,$ops) are trimmed to the appropriate path length.
754  
755 =cut
756  
757 sub edit_pathtrace {
758 my ($align,$ai,$bi,$ops,$len) = @_;
759 $len=pdl(long,$align->dim(0)+$align->dim(1)) if (!defined($len));
760 if (!defined($ai)) { $ai=zeroes(long,$len); }
761 else { $ai->reshape($len) if ($ai->nelem < $len); }
762 if (!defined($bi)) { $bi = zeroes(long,$len); }
763 else { $bi->reshape($len) if ($bi->nelem < $len); }
764 if (!defined($ops)) { $ops = zeroes(long,$len); }
765 else { $ops->reshape($len) if ($ops->nelem < $len); }
766 _edit_pathtrace($align, $ai,$bi,$ops,$len, $align->dim(0)-1,$align->dim(1)-1);
767 my $lens = ($len->sclr-1);
768 return ((map { $_->slice("0:$lens") } ($ai,$bi,$ops)), $len);
769 }
770 #line 771 "EditDistance.pm"
771  
772  
773 =head2 _edit_pathtrace
774  
775 =for sig
776  
777 Signature: (align(N1,M1); int [o]ai(L); int [o]bi(L); int [o]ops(L); int [o]len(); int ifinal; int jfinal)
778 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
779 float double ldouble)
780  
781 =for usage
782  
783 ($ai, $bi, $ops, $len) = _edit_pathtrace($align, $ifinal, $jfinal);
784 _edit_pathtrace($align, $ai, $bi, $ops, $len, $ifinal, $jfinal); # all arguments given
785 ($ai, $bi, $ops, $len) = $align->_edit_pathtrace($ifinal, $jfinal); # method call
786 $align->_edit_pathtrace($ai, $bi, $ops, $len, $ifinal, $jfinal);
787  
788 Low-level method.
789 Compute alignment path backtrace through alignment matrix $align() from final index ($ifinal,$jfinal).
790 Stores raw paths for (original) input strings $a() and $b() in $ai() and $bi()
791 respectively.
792 Unlike edit_bestpath(), null-moves for $ai() and $bi() are not stored here as negative values.
793 Returned pdls ($ai,$bi,$ops) are trimmed to the appropriate path length.
794  
795 =pod
796  
797 Broadcasts over its inputs.
798  
799 =for bad
800  
801 C<_edit_pathtrace> does not process bad values.
802 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
803  
804 =cut
805  
806  
807  
808  
809 0 *_edit_pathtrace = \&PDL::_edit_pathtrace;
810  
811  
812  
813  
814  
815 #line 905 "EditDistance.pd"
816  
817 =pod
818  
819 =head2 edit_lcs
820  
821 =for sig
822  
823 Signature: (a(N); b(M); int [o]lcs(N+1,M+1);)
824  
825 Convenience method.
826 Compute the longest common subsequence (LCS) matrix for input PDLs $a1() and $b1().
827 The output matrix $lcs() contains at cell ($i+1,$j+1) the length of the LCS
828 between $a1(0..$i) and $b1(0..$j); thus $lcs($N,$M) contains the
829 length of the LCS between $a() and $b().
830  
831 =cut
832  
833 sub edit_lcs {
834 return _edit_lcs(_edit_pdl($_[0]), _edit_pdl($_[1]), @_[2..$#_]);
835 }
836 #line 837 "EditDistance.pm"
837  
838  
839 =head2 _edit_lcs
840  
841 =for sig
842  
843 Signature: (a1(N1); b1(M1); int [o]lcs(N1,M1))
844 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
845 float double ldouble)
846  
847 =for usage
848  
849 $lcs = _edit_lcs($a1, $b1);
850 _edit_lcs($a1, $b1, $lcs); # all arguments given
851 $lcs = $a1->_edit_lcs($b1); # method call
852 $a1->_edit_lcs($b1, $lcs);
853  
854 Low-level method.
855 Compute the longest common subsequence (LCS) matrix for input PDLs $a1() and $b1().
856 The initial (zeroth) elements of $a1() and $b1() are ignored.
857 The output matrix $lcs() contains at cell ($i,$j) the length of the LCS
858 between $a1(1..$i) and $b1(1..$j); thus $lcs($N1-1,$M1-1) contains the
859 length of the LCS between $a1() and $b1().
860  
861 =pod
862  
863 Broadcasts over its inputs.
864  
865 =for bad
866  
867 C<_edit_lcs> does not process bad values.
868 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
869  
870 =cut
871  
872  
873  
874  
875 0 *_edit_lcs = \&PDL::_edit_lcs;
876  
877  
878  
879  
880  
881 #line 970 "EditDistance.pd"
882  
883 =pod
884  
885 =head2 lcs_backtrace
886  
887 =for sig
888  
889 Signature: (a(N); b(M); int lcs(N+1,M+1); int ifinal(); int jfinal(); int [o]ai(L); int [o]bi(L); int [o]len())
890  
891 Convenience method.
892 Compute longest-common-subsequence backtrace through LCS matrix $lcs()
893 for original input strings ($a(),$b()) from final index ($ifinal,$jfinal).
894 Stores raw paths for (original) input strings $a() and $b() in $ai() and $bi()
895 respectively.
896  
897 =cut
898  
899 sub lcs_backtrace {
900 my ($a,$b,$lcs,$ifinal,$jfinal,$ai,$bi,$len) = @_;
901 $len=pdl(long, pdl(long,$lcs->dims)->min) if (!defined($len));
902 if (!defined($ai)) { $ai=zeroes(long,$len); }
903 else { $ai->reshape($len) if ($ai->nelem < $len); }
904 if (!defined($bi)) { $bi = zeroes(long,$len); }
905 else { $bi->reshape($len) if ($bi->nelem < $len); }
906 if (!defined($ifinal)) { $ifinal = $lcs->dim(0)-1; }
907 if (!defined($jfinal)) { $jfinal = $lcs->dim(1)-1; }
908 _lcs_backtrace(_edit_pdl($a),_edit_pdl($b), $lcs,$ifinal,$jfinal, $ai,$bi,$len);
909 my $lens = ($len->sclr-1);
910 return ($ai->slice("0:$lens"),$bi->slice("0:$lens"), $len);
911 }
912 #line 913 "EditDistance.pm"
913  
914  
915 =head2 _lcs_backtrace
916  
917 =for sig
918  
919 Signature: (a1(N1); b1(M1); int lcs(N1,M1); int ifinal(); int jfinal(); [o]ai(L); [o]bi(L); int [o]len())
920 Types: (sbyte byte short ushort long ulong indx ulonglong longlong
921 float double ldouble)
922  
923 =for usage
924  
925 ($ai, $bi, $len) = _lcs_backtrace($a1, $b1, $lcs, $ifinal, $jfinal);
926 _lcs_backtrace($a1, $b1, $lcs, $ifinal, $jfinal, $ai, $bi, $len); # all arguments given
927 ($ai, $bi, $len) = $a1->_lcs_backtrace($b1, $lcs, $ifinal, $jfinal); # method call
928 $a1->_lcs_backtrace($b1, $lcs, $ifinal, $jfinal, $ai, $bi, $len);
929  
930 Low-level method.
931 Compute longest-common-subsequence backtrace through LCS matrix $lcs()
932 for initial-padded strings ($a1(),$b1()) from final index ($ifinal,$jfinal).
933 Stores raw paths for (original) input strings $a() and $b() in $ai() and $bi()
934 respectively.
935  
936 =pod
937  
938 Broadcasts over its inputs.
939  
940 =for bad
941  
942 C<_lcs_backtrace> does not process bad values.
943 It will set the bad-value flag of all output ndarrays if the flag is set for any of the input ndarrays.
944  
945 =cut
946  
947  
948  
949  
950 2 *_lcs_backtrace = \&PDL::_lcs_backtrace;
951  
952  
953  
954  
955  
956 #line 1063 "EditDistance.pd"
957  
958 ##---------------------------------------------------------------------
959 =pod
960  
961 =head1 ACKNOWLEDGEMENTS
962  
963 Perl by Larry Wall.
964  
965 PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.
966  
967 =cut
968  
969 ##----------------------------------------------------------------------
970 =pod
971  
972 =head1 KNOWN BUGS
973  
974 Probably many.
975  
976 =cut
977  
978 ##---------------------------------------------------------------------
979 =pod
980  
981 =head1 AUTHOR
982  
983 Bryan Jurish E<lt>moocow@cpan.orgE<gt>
984  
985 =head2 Copyright Policy
986  
987 Copyright (C) 2006-2015, Bryan Jurish. All rights reserved.
988  
989 This package is free software, and entirely without warranty.
990 You may redistribute it and/or modify it under the same terms
991 as Perl itself, either Perl 5.20.2, or at your option any later
992 version of Perl 5.
993  
994 =head1 SEE ALSO
995  
996 perl(1), PDL(3perl).
997  
998 =cut
999 #line 1000 "EditDistance.pm"
1000  
1001 # Exit with OK status
1002  
1003 2 1;
« EditDistance.xs ↑ Summary