Skip to content

Commit

Permalink
Adds the missing levenshteinDistanceTo: method (fixes #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeZeilen committed May 6, 2019
1 parent e6fa1a4 commit 968d7ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
*Home-Core
levenshteinDistanceTo: aString

| sourceString targetString costs |

sourceString := self asLowercase.
targetString := aString asLowercase.

costs := Matrix rows: sourceString size + 1 columns: targetString size + 1 element: 0.

costs atColumn: 1 put: (0 to: sourceString size).
costs atRow: 1 put: (0 to: targetString size).

targetString withIndexDo: [:targetCharacter :targetIndex |
sourceString withIndexDo: [:sourceCharacter :sourceIndex | | cost |
cost := targetCharacter = sourceCharacter ifTrue: [0] ifFalse: [1].

costs at: sourceIndex + 1 at: targetIndex + 1 put: ({
(costs at: sourceIndex + 1 at: targetIndex) + 1 . "deletion"
(costs at: sourceIndex at: targetIndex + 1) + 1 . "insertion"
(costs at: sourceIndex at: targetIndex) + cost . "potential substitution"
} min) ]].

^ costs at: costs rowCount at: costs columnCount
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"class" : {
},
"instance" : {
"levenshteinDistanceTo:" : "pre 7/13/2017 12:53",
"objectEditorBeforeChangeConversion" : "pre 8/23/2018 11:00",
"objectEditorPrintString" : "pre 6/8/2018 19:22",
"objectEditorWidgetFor:" : "pre 6/28/2018 16:23" } }

0 comments on commit 968d7ca

Please sign in to comment.