levenshtein-search
Advanced tools
Comparing version 0.0.2 to 0.1.0
@@ -32,54 +32,4 @@ const { searchExact, reverse } = require('./utils') | ||
if (maxDist * 5 >= a.length) { | ||
return editDistance(a, b) <= maxDist | ||
} | ||
// use n-gram search | ||
const ngramLen = Math.floor(a.length / (maxDist + 1)) | ||
for (let ngramStartIdx = 0; ngramStartIdx <= a.length - ngramLen; ngramStartIdx += ngramLen) { | ||
const ngram = a.slice(ngramStartIdx, ngramStartIdx + ngramLen) | ||
for (const bMatchIdx of searchExact(ngram, b)) { | ||
let back | ||
const maxBack = Math.min(ngramStartIdx, bMatchIdx) | ||
for (back = 0; back < maxBack; back++) { | ||
if (a[ngramStartIdx - (back + 1)] !== b[bMatchIdx - (back + 1)]) { | ||
break | ||
} | ||
} | ||
let forward | ||
const maxForward = Math.min( | ||
a.length - (ngramStartIdx + ngramLen), | ||
b.length - (bMatchIdx + ngramLen) | ||
) | ||
for (forward = 0; forward < maxForward; forward++) { | ||
if ( | ||
a[ngramStartIdx + ngramLen + (forward + 1)] !== | ||
b[bMatchIdx + ngramLen + (forward + 1)] | ||
) { | ||
break | ||
} | ||
} | ||
const aBefore = a.slice(0, ngramStartIdx - back) | ||
const aAfter = a.slice(ngramStartIdx + ngramLen + forward) | ||
const bBefore = b.slice(0, bMatchIdx - back) | ||
const bAfter = b.slice(bMatchIdx + ngramLen + forward) | ||
if (Math.min(aBefore.length, bBefore.length) < Math.min(aAfter.length, bAfter.length)) { | ||
const dist = editDistance(aBefore, bBefore) | ||
if (dist <= maxDist && isEditDistanceNoGreaterThan(aAfter, bAfter, maxDist - dist)) { | ||
return true | ||
} | ||
} else { | ||
const dist = editDistance(aAfter, bAfter) | ||
if (dist <= maxDist && isEditDistanceNoGreaterThan(aBefore, bBefore, maxDist - dist)) { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
const [ dist, length ] = _expand(a, b, maxDist) | ||
return dist + (b.length - length) <= maxDist | ||
} | ||
@@ -86,0 +36,0 @@ |
@@ -25,7 +25,7 @@ { | ||
"scripts": { | ||
"check-only": "! grep 'test.only' tests/test.js -n", | ||
"check-only": "grep -c \"test.only\" test.js | python -c \"import sys; sys.exit(0 if int(sys.stdin.read()) == 0 else 1)\"", | ||
"lint": "standard", | ||
"test": "npm run lint && npm run check-only && jest" | ||
}, | ||
"version": "0.0.2" | ||
"version": "0.1.0" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28350
8
727