;;;; levenshtein-transpose-byte.scm -*- Scheme -*- ;;;; Kon Lovett, Mar '20 ;;;; Kon Lovett, Apr '12 ;;;; Kon Lovett, Sep '05 (module levenshtein-transpose-byte (;export levenshtein-distance/transpose-byte) (import scheme (chicken base) (chicken type) (chicken foreign) (chicken blob) (only type-errors-basic error-argument-type)) (define-type byte-sequence (or string blob)) (: levenshtein-distance/transpose-byte (byte-sequence byte-sequence -> fixnum)) (define (levenshtein-distance/transpose-byte s t) (unless (or (string? s) (blob? s)) (error-argument-type 'levenshtein-distance/transpose-byte s "string or blob" "source") ) (unless (or (string? t) (blob? t)) (error-argument-type 'levenshtein-distance/transpose-byte t "string or blob" "target") ) ((foreign-lambda* unsigned-int ((scheme-object os) (scheme-object ot)) #< for a discussion */ /* of some useful properties of the Levenshtein distance when not extended */ /* to include-relative transposition as an edit operation. */ /* Step 6A: Cover transposition, in addition to deletion, */ /* insertion and substitution. This step is taken from: */ /* Berghel, Hal ; Roach, David : `An Extension of Ukkonen's */ /* Enhanced Dynamic Programming ASM Algorithm' */ /* (http://www.acm.org/~hlb/publications/asm/asm.html) */ # ifndef TRANS_COST # define TRANS_COST 1 # endif if (i >= 2 && j >= 2) { uint32_t trans = D(i-2, j-2) + TRANS_COST; if (s[i-2] != t_j) trans += TRANS_COST; if (s_i != t[j-2]) trans += TRANS_COST; cost = MIN(cost, trans); } # undef TRANS_COST # endif /* Step 6 */ D(i, j) = cost; } } /* Step 7 */ uint32_t distance = D(n, m); MAT_FREE(d); return(distance); # undef MIN # undef MIN3 # undef MAT_ALLOC # undef MAT_ELM_DEREF # undef MAT_FREE # undef INS_COST # undef DEL_COST # undef SUBST_COST # undef D # undef USE_TRANSPOSITION EOF ) s t) ) ) ;module levenshtein-transpose-byte