pax_global_header00006660000000000000000000000064130520013420014500gustar00rootroot0000000000000052 comment=0630566a69b5a73aae2e52bb47ea863892a4b5f0 leven-2.1.0/000077500000000000000000000000001305200134200126115ustar00rootroot00000000000000leven-2.1.0/.editorconfig000066400000000000000000000002761305200134200152730ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 leven-2.1.0/.gitattributes000066400000000000000000000000351305200134200155020ustar00rootroot00000000000000* text=auto *.js text eol=lf leven-2.1.0/.gitignore000066400000000000000000000000151305200134200145750ustar00rootroot00000000000000node_modules leven-2.1.0/.travis.yml000066400000000000000000000001151305200134200147170ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' - '0.12' - '0.10' leven-2.1.0/bench.js000066400000000000000000000030061305200134200142250ustar00rootroot00000000000000/* globals bench suite */ 'use strict'; var levenshteinEditDistance = require('levenshtein-edit-distance'); var fastLevenshtein = require('fast-levenshtein').get; var levenshteinComponent = require('levenshtein-component'); var ld = require('ld').computeDistance; var levdist = require('levdist'); var natural = require('natural').LevenshteinDistance; var levenshtein = require('levenshtein'); var talisman = require('talisman/metrics/distance/levenshtein'); var leven = require('./'); function run(fn) { fn('a', 'b'); fn('ab', 'ac'); fn('ac', 'bc'); fn('abc', 'axc'); fn('kitten', 'sitting'); fn('xabxcdxxefxgx', '1ab2cd34ef5g6'); fn('cat', 'cow'); fn('xabxcdxxefxgx', 'abcdefg'); fn('javawasneat', 'scalaisgreat'); fn('example', 'samples'); fn('sturgeon', 'urgently'); fn('levenshtein', 'frankenstein'); fn('distance', 'difference'); fn('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'); } suite('leven', function () { bench('leven', function () { run(leven); }); bench('talisman', function () { run(talisman); }); bench('levenshtein-edit-distance', function () { run(levenshteinEditDistance); }); bench('fast-levenshtein', function () { run(fastLevenshtein); }); bench('levenshtein-component', function () { run(levenshteinComponent); }); bench('levdist', function () { run(levdist); }); bench('ld', function () { run(ld); }); bench('natural', function () { run(natural); }); bench('levenshtein', function () { run(levenshtein); }); }); leven-2.1.0/index.js000066400000000000000000000030561305200134200142620ustar00rootroot00000000000000/* eslint-disable no-nested-ternary */ 'use strict'; var arr = []; var charCodeCache = []; module.exports = function (a, b) { if (a === b) { return 0; } var swap = a; // Swapping the strings if `a` is longer than `b` so we know which one is the // shortest & which one is the longest if (a.length > b.length) { a = b; b = swap; } var aLen = a.length; var bLen = b.length; if (aLen === 0) { return bLen; } if (bLen === 0) { return aLen; } // Performing suffix trimming: // We can linearly drop suffix common to both strings since they // don't increase distance at all // Note: `~-` is the bitwise way to perform a `- 1` operation while (aLen > 0 && (a.charCodeAt(~-aLen) === b.charCodeAt(~-bLen))) { aLen--; bLen--; } if (aLen === 0) { return bLen; } // Performing prefix trimming // We can linearly drop prefix common to both strings since they // don't increase distance at all var start = 0; while (start < aLen && (a.charCodeAt(start) === b.charCodeAt(start))) { start++; } aLen -= start; bLen -= start; if (aLen === 0) { return bLen; } var bCharCode; var ret; var tmp; var tmp2; var i = 0; var j = 0; while (i < aLen) { charCodeCache[start + i] = a.charCodeAt(start + i); arr[i] = ++i; } while (j < bLen) { bCharCode = b.charCodeAt(start + j); tmp = j++; ret = j; for (i = 0; i < aLen; i++) { tmp2 = bCharCode === charCodeCache[start + i] ? tmp : tmp + 1; tmp = arr[i]; ret = arr[i] = tmp > ret ? tmp2 > ret ? ret + 1 : tmp2 : tmp2 > tmp ? tmp + 1 : tmp2; } } return ret; }; leven-2.1.0/license000066400000000000000000000021371305200134200141610ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) 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 "AS IS", 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. leven-2.1.0/package.json000066400000000000000000000021501305200134200150750ustar00rootroot00000000000000{ "name": "leven", "version": "2.1.0", "description": "Measure the difference between two strings using the fastest JS implementation of the Levenshtein distance algorithm", "license": "MIT", "repository": "sindresorhus/leven", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "xo && ava", "bench": "matcha bench.js" }, "files": [ "index.js" ], "keywords": [ "leven", "levenshtein", "distance", "algorithm", "algo", "string", "difference", "diff", "fast", "fuzzy", "similar", "similarity", "compare", "comparison", "edit", "text", "match", "matching" ], "devDependencies": { "ava": "^0.17.0", "fast-levenshtein": "^2.0.5", "ld": "^0.1.0", "levdist": "^2.0.0", "levenshtein": "^1.0.4", "levenshtein-component": "0.0.1", "levenshtein-edit-distance": "^2.0.0", "matcha": "^0.7.0", "natural": "^0.4.0", "talisman": "^0.18.0", "xo": "^0.16.0" } } leven-2.1.0/readme.md000066400000000000000000000017161305200134200143750ustar00rootroot00000000000000# leven [![Build Status](https://travis-ci.org/sindresorhus/leven.svg?branch=master)](https://travis-ci.org/sindresorhus/leven) > Measure the difference between two strings
> The fastest JS implementation of the [Levenshtein distance](http://en.wikipedia.org/wiki/Levenshtein_distance) algorithm ## Install ``` $ npm install --save leven ``` ## Usage ```js const leven = require('leven'); leven('cat', 'cow'); //=> 2 ``` ## Benchmark ``` $ npm run bench ``` ``` 401,487 op/s » leven 371,707 op/s » talisman 264,191 op/s » levenshtein-edit-distance 152,923 op/s » fast-levenshtein 57,267 op/s » levenshtein-component 19,915 op/s » levdist 21,802 op/s » ld 18,079 op/s » natural 11,761 op/s » levenshtein ``` ## Related - [leven-cli](https://github.com/sindresorhus/leven-cli) - CLI for this module ## License MIT © [Sindre Sorhus](https://sindresorhus.com) leven-2.1.0/test.js000066400000000000000000000011461305200134200141300ustar00rootroot00000000000000import test from 'ava'; import m from './'; test(t => { t.is(m('a', 'b'), 1); t.is(m('ab', 'ac'), 1); t.is(m('ac', 'bc'), 1); t.is(m('abc', 'axc'), 1); t.is(m('kitten', 'sitting'), 3); t.is(m('xabxcdxxefxgx', '1ab2cd34ef5g6'), 6); t.is(m('cat', 'cow'), 2); t.is(m('xabxcdxxefxgx', 'abcdefg'), 6); t.is(m('javawasneat', 'scalaisgreat'), 7); t.is(m('example', 'samples'), 3); t.is(m('sturgeon', 'urgently'), 6); t.is(m('levenshtein', 'frankenstein'), 6); t.is(m('distance', 'difference'), 5); t.is(m('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'), 2); });