string-compare-diff
Advanced tools
Comparing version 0.0.8 to 0.0.9
57
app.js
'use strict' | ||
/* | ||
** strSimilarity takes two strings and returns a decimal 0 to 1, 0 no character's match, 1 all characters match, Between 0 & 1 is percentage match (as a decimal) | ||
** example console.log(strSimilarity("Daniel", "Dan")) | ||
** getDistance takes two strings and returns a decimal 0 to 1, 0 no character's match, 1 all characters match, Between 0 & 1 is percentage match (as a decimal) | ||
** example console.log(getDistance("Daniel", "Dan")) | ||
** | ||
*/ | ||
module.exports = { | ||
strSimilarity: function(s1, s2){ | ||
var longer = s1 | ||
var shorter = s2 | ||
if (s1.length < s2.length) { | ||
longer = s2 | ||
shorter = s1 | ||
} | ||
var longerLength = longer.length | ||
if (longerLength == 0) { | ||
return 1.0 | ||
} | ||
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength) | ||
const getDistance = (s1, s2) => { | ||
let longer = s1 | ||
let shorter = s2 | ||
if (s1.length < s2.length) { | ||
longer = s2 | ||
shorter = s1 | ||
} | ||
} | ||
const longerLength = longer.length | ||
if (longerLength == 0) return 1.0; | ||
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength); | ||
}; | ||
const editDistance = (s1, s2) => { | ||
s1 = s1.toLowerCase() | ||
s2 = s2.toLowerCase() | ||
s1 = s1.toLowerCase(); | ||
s2 = s2.toLowerCase(); | ||
var costs = new Array() | ||
for (var i = 0; i <= s1.length; i++) { | ||
var lastValue = i | ||
const costs = new Array(); | ||
for (let i = 0; i <= s1.length; i++) { | ||
let lastValue = i | ||
for (var j = 0; j <= s2.length; j++) { | ||
if (i == 0) | ||
costs[j] = j | ||
else { | ||
if (i == 0) { | ||
costs[j] = j; | ||
} else { | ||
if (j > 0) { | ||
var newValue = costs[j - 1] | ||
let newValue = costs[j - 1] | ||
if (s1.charAt(i - 1) != s2.charAt(j - 1)) | ||
newValue = Math.min(Math.min(newValue, lastValue), | ||
costs[j]) + 1 | ||
costs[j - 1] = lastValue | ||
lastValue = newValue | ||
costs[j]) + 1; | ||
costs[j - 1] = lastValue; | ||
lastValue = newValue; | ||
} | ||
@@ -49,2 +46,4 @@ } | ||
return costs[s2.length] | ||
} | ||
}; | ||
module.exports = { getDistance }; |
{ | ||
"name": "string-compare-diff", | ||
"version": "0.0.8", | ||
"description": "Compare the difference between two strings using distance. Returns a number 0 to 1 showing exactly how related the two strings are.", | ||
"version": "0.0.9", | ||
"description": "Compare the difference between two strings using Levenshtein Distance. Returns a number 0 to 1 showing exactly how related the two strings are.", | ||
"main": "app.js", | ||
@@ -11,3 +11,3 @@ "scripts": { | ||
"type": "git", | ||
"url": "git@github-github_personal:DanielDwyerPersonal/string-compare-diff.git" | ||
"url": "https://github.com/DanielDwyer/string-compare-diff" | ||
}, | ||
@@ -14,0 +14,0 @@ "keywords": [ |
# string-compare-diff | ||
**Stable LTS** | ||
The Levenshtein distance or edit distance between two strings is given by the minimum number of operations needed to transform one string into the other, where an operation is an insertion, deletion, or substitution | ||
![Downloads](https://img.shields.io/npm/dw/string-compare-diff.svg) | ||
@@ -11,8 +12,8 @@ ![Downloads](https://img.shields.io/npm/dm/string-compare-diff.svg) | ||
What is Levenshtein Distance? | ||
[What is Levenshtein Distance? | ||
"In information theory, linguistics and computer science, the Levenshtein distance is a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other. It is named after the Soviet mathematician Vladimir Levenshtein, who considered this distance in 1965.[1] | ||
Levenshtein distance may also be referred to as edit distance, although that term may also denote a larger family of distance metrics.[2]:32 It is closely related to pairwise string alignments." | ||
Levenshtein distance may also be referred to as edit distance, although that term may also denote a larger family of distance metrics.[2]:32 It is closely related to pairwise string alignments."](https://en.wikipedia.org/wiki/Levenshtein_distance) | ||
- I found the above description at: https://en.wikipedia.org/wiki/Levenshtein_distance | ||
- https://en.wikipedia.org/wiki/Levenshtein_distance | ||
@@ -38,3 +39,3 @@ The Levenshtein distance algorithm has been used in: | ||
```shell | ||
const compareStr = require('string-compare-diff') | ||
const SCD = require('string-compare-diff'); | ||
``` | ||
@@ -44,11 +45,11 @@ | ||
```js | ||
compareStr.strSimilarity("Dan", "Daniel") | ||
SCD.getDistance('Dan', 'Daniel'); | ||
//returns 0.5 | ||
``` | ||
```js | ||
compareStr.strSimilarity("America The Beautiful", "Canada") | ||
SCD.getDistance('America The Beautiful', 'Canada'); | ||
//returns 0.14285714285714285 | ||
``` | ||
```js | ||
compareStr.strSimilarity("Donald", "Trump") | ||
SCD.getDistance('Donald', 'Trump'); | ||
//returns 0 | ||
@@ -59,3 +60,3 @@ ``` | ||
MIT | ||
Copyright 2018 Daniel P. Dwyer | ||
Copyright 2020 Daniel P. Dwyer | ||
@@ -62,0 +63,0 @@ 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: |
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
4936
64
43