Levenshtein edit distance
- 8-bit Values Only
- Any "Sequence"
- Only Vector - Baroque & Slow
- Interface Implementation Modules
- Edit Operators
- Path Iterator
- Path Matrix Print
- Bugs & Limitations
- Version history
Levenshtein is a collection of procedures providing various forms of the Levenshtein edit distance calculation.
The Levenshtein edit distance has been used for areas as diverse as soil sample and language dialect analysis. Not just for text strings.
Performs edit distance calculation for byte strings & blobs. All return the total edit cost.
- levenshtein-distance/byte SOURCE TARGETprocedure
Calculates the edit distance from the SOURCE to the TARGET. All costs are unitary.
- levenshtein-distance/transpose-byte SOURCE TARGETprocedure
Calculates the edit distance from the SOURCE to the TARGET, taking into account the Transpose operation. All costs are unitary.
By using the Transpose operation the total edit cost is not at least the difference of the sizes of the two strings.
A functor implementing an edit distance algorithm parameterized by a cost and sequence operation modules.
See Examples for Usage
- (levenshtein-distance/sequence SOURCE TARGET [:insert-cost INSERT-COST] [#:delete-cost DELETE-COST] [#:substitute-cost SUBSTITUTE-COST] (#:get-work-vector GET-WORK-VECTOR) [#:elm-eql ELM-EQL] [#:limit-cost LIMIT-COST])procedure
- number, default 1.
- number, default 1.
- number, default 1.
- procedure; (-> object object boolean), default eqv?. The equality predicate.
- procedure, default make-vector.
- number or #f, default #f. Quit when cost over limit.
The SOURCE & TARGET must be the same type, which the instantiating sequence module supports.
Note that the element comparison procedure is passed via the argument list, and not via the sequence implementation module. Annoying when using strings but useful when using vectors.
A functor implementing an edit distance algorithm parameterized by a cost operation module.
Performs edit distance calculation for vectors. Allows definition of new edit operations. Will keep track of edit operations performed. Primarily a toy.
See Examples for Usage
- (levenshtein-distance/vector* SOURCE TARGET [EDIT-OPER ...] [#:elm-eql ELM-EQL] [#:operations? OPERATIONS])procedure
Calculates the edit distance from the source vector SOURCE to the target vector TARGET. Returns the total edit cost or (values <total edit cost> <performed operations matrix>).
- levenshtein-operator. Edit operation definitions to apply. Defaults are the basic Insert, Delete, and Substitute.
- procedure; (-> object object boolean), default char=?. The equality predicate.
- boolean. Include the matrix of edit operations performed? Default #f.
levenshtein-cost-fixnum.scm levenshtein-cost-generic.scm levenshtein-cost-numbers.scm
levenshtein-sequence-string.scm levenshtein-sequence-utf8.scm levenshtein-sequence-vector.scm
Edit operation specification. A set of base operations is predefined, but may be overridden. The base set is identified by the keys Insert, Delete, Substitute, and Transpose. A printer and reader are provided for edit operations.
- levenshtein-operator-key OPERprocedure
- levenshtein-operator-name OPERprocedure
- levenshtein-operator-cost OPERprocedure
- levenshtein-operator-above OPERprocedure
- levenshtein-operator-left OPERprocedure
- make-levenshtein-operator KEY NAME COST ABOVE LEFTprocedure
Returns a new edit operator.
- symbol. Key for the operation.
- string. Describes the operation.
- number. The cost of the operation.
- non-negative-fixnum. How far back in the source.
- non-negative-fixnum. How far back in the target.
- levenshtein-operator? OBJECTprocedure
Is the OBJECT a levenshtein operator?
- (clone-levenshtein-operator EDIT-OPERATION [#:key KEY] [#:name NAME] [#:cost COST] [#:above ABOVE] [#:left LEFT])procedure
Returns a duplicate of the EDIT-OPERATION, with field values provided by the optional keyword arguments. EDIT-OPERATION may be the key of the already defined edit operation.
- levenshtein-operator-ref KEYprocedure
Get the definition of an edit operation.
- levenshtein-operator-set! EDIT-OPERATIONprocedure
Define an edit operation.
- levenshtein-operator-delete! EDIT-OPERATIONprocedure
Removes the EDIT-OPERATION definition. EDIT-OPERATION may be the KEY of the already defined edit operation.
Restore defined edit operations to the base set.
- levenshtein-operator=? A Bprocedure
- levenshtein-path-iterator PATH-MATRIXprocedure
Creates an optimal edit distance operation path iterator over the performed operations matrix PATH-MATRIX. The matrix is usually the result of an invocation of (levenshtein-distance/vector* ... operations: #t).
Each invocation of the iterator will generate a list of the form: ((cost source-index target-index levenshtein-operator) ...). The last invocation will return #f.
- print-levenshtein-matrix PATH-MATRIXprocedure
Displays a readable representation of the PATH-MATRIX on the current-output-port.
- The functors are not available before Chicken 4.7.2, and so all support modules are also unavailable.
- To instantiate any of the functors the implementation module(s) must be included. Since the modules are installed in Chicken Repository, and the repository is not usually along the include path, the repository must be added to the include path. Using the environment variable CHICKEN_INCLUDE_PATH is convenient.
- levenshtein-print assumes a levenshtein-operator-key print-name is <= 15 characters and that the cost prints in <= 2 characters.
(import levenshtein-byte) (import levenshtein-transpose-byte) (levenshtein-distance/byte "ctas" "cats") ;=> 2 (levenshtein-distance/transpose-byte "ctas" "cats") ;=> 1 ;cause of transpose
; Instantiate the distance measure algorithm (import levenshtein-path-iterator) (import levenshtein-vector-functor) (include "levenshtein-cost-fixnum") (module levenshtein-vector-fixnum = (levenshtein-vector-functor levenshtein-cost-fixnum)) (import (prefix levenshtein-vector-fixnum fx:)) (define iter (levenshtein-path-iterator (fx:levenshtein-distance/vector* "YWCQPGK" "LAWYQQKPGKA" operations: #t)) ; ignoring interpreter feedback & we know the distance is 6 (define r0 (iter)) (define t0 r0) (define r1 (iter)) (define r2 (iter)) (define r3 (iter)) (define r4 (iter)) (define r5 (iter)) (iter) ; r0 now has #f, since the iterator finishes by returning to the initial caller, ; which is the body of '(define r0 (iter))', thus re-binding r0. However, t0 has ; the original returned value.
(import levenshtein-sequence-functor) (include "levenshtein-cost-fixnum") (include "levenshtein-sequence-vector") (module levenshtein-sequence-fixnum-vector = (levenshtein-sequence-functor levenshtein-cost-fixnum levenshtein-sequence-vector)) (import (prefix levenshtein-sequence-fixnum-vector fxvc:)) ; Now have 'fxvc:levenshtein-distance/sequence', be sure to verify the #:elm-eql ; keyword parameter default is what is wanted (include "levenshtein-cost-numbers") (include "levenshtein-sequence-utf8") (module levenshtein-sequence-numbers-utf8 = (levenshtein-sequence-functor levenshtein-cost-numbers levenshtein-sequence-utf8)) (import (prefix levenshtein-sequence-numbers-utf8 fnu8:)) ; Now have 'fnu8:levenshtein-distance/sequence', be sure to pass the #:elm-eql ; keyword parameter char=?
- Added types. Re-flow.
- Added an "egg tag".
- Drop "format-compiler".
- Chicken 4 release.
Copyright (c) 2012-2017, Kon Lovett. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.