-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the missing levenshteinDistanceTo: method (fixes #16)
- Loading branch information
1 parent
e6fa1a4
commit 968d7ca
Showing
2 changed files
with
25 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
repository/Home.package/String.extension/instance/levenshteinDistanceTo..st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters