Difference between revisions of "Script:levenshteinDistance"
From Roll20 Wiki
Andreas J. (Talk | contribs) m (add category) |
|||
(2 intermediate revisions by one user not shown) | |||
Line 2: | Line 2: | ||
|name=levenshteinDistance | |name=levenshteinDistance | ||
|author={{user profile|235259|Brian}} | |author={{user profile|235259|Brian}} | ||
− | |version=1. | + | |version=1.1 |
− | |lastmodified= | + | |lastmodified=2016-03-09 |
}} | }} | ||
Line 51: | Line 51: | ||
}); | }); | ||
</pre> | </pre> | ||
+ | |||
+ | === Changelog === | ||
+ | {{changelog version|1.1|2016-03-09|* Update for one-click install}} | ||
+ | {{changelog version|1.0|2015-01-08|* Release}} | ||
+ | |||
+ | [[Category:API Meta Scripts]] |
Latest revision as of 14:16, 4 January 2022
Version: 1.1
Last Modified: 2016-03-09
Code: levenshteinDistance
Dependencies: None
Conflicts: None
levenshteinDistance is a string comparison function. The metric was developed by Vladimir Levenshtein in 1965. The function tracks the difference between two strings based on single-character changes. In other words, the number of insertions, deletions, and substitutions required to change the first string into the second. This metric is useful for deciding what strings are "close" to being the same, such as finding a partial match against a player or character name.
The levenshteinDistance
function is added to String.prototype
, meaning it can be treated as a function of string objects.
Original JavaScript implementation from wikibooks.org.
Contents |
[edit] Syntax
str.levenshteinDistance(otherStr)
[edit] Parameters
- otherStr
- The string to compare against str.
[edit] Return Value
levenshteinDistance
returns an integer of the total number of insertions, deletions, and substitutions required to change str into otherStr.
[edit] Example
The distance between "kitten" and "sitting" is 3:
- kitten → sitten (substitute "s" for "k")
- sitten → sittin (substitute "i" for "e")
- sittin → sitting (insert "g")
on('chat:message', function(msg) { var params = msg.content.splitArgs(), command = params.shift().substring(1).toLowerCase(), characterName = params[0], potentialCharacters, foundCharacter; if (msg.type === 'api' && command === 'echocharacter') { potentialCharacters = filterObjs(function(obj) { return obj.get('type') === 'character' && obj.get('name').indexOf(who) >= 0; }); foundCharacter = _.sortBy(potentialCharacters, function(chr) { return chr.get('name').toLowerCase().levenshteinDistance(who.toLowerCase()); })[0]; if (foundCharacter) { sendChat('System', characterName + ' interpreted as ' + foundCharacter.get('name')); } else { sendChat('System', 'Could not find character for ' + characterName); } } });
[edit] Changelog
v1.1 (2016-03-09)
- Update for one-click install
v1.0 (2015-01-08)
- Release