Finds degree of similarity between two strings, based on Dice's Coefficient, which is mostly better than Levenshtein distance.
Installations
npm install string-similarity
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
14.15.1
NPM Version
6.14.8
Score
99.9
Supply Chain
99.4
Quality
75.5
Maintenance
100
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
aceakash
Download Statistics
Total Downloads
384,365,431
Last Day
420,057
Last Week
1,948,675
Last Month
7,798,181
Last Year
86,347,327
GitHub Statistics
2,524 Stars
63 Commits
129 Forks
31 Watching
11 Branches
9 Contributors
Package Meta Information
Latest Version
4.0.4
Package Id
string-similarity@4.0.4
Size
4.26 kB
NPM Version
6.14.8
Node Version
14.15.1
Publised On
06 Jan 2021
Total Downloads
Cumulative downloads
Total Downloads
384,365,431
Last day
18.6%
420,057
Compared to previous day
Last week
-1.9%
1,948,675
Compared to previous week
Last month
8.1%
7,798,181
Compared to previous month
Last year
-11.2%
86,347,327
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
3
string-similarity
Finds degree of similarity between two strings, based on Dice's Coefficient, which is mostly better than Levenshtein distance.
Table of Contents
Usage
For Node.js
Install using:
1npm install string-similarity --save
In your code:
1var stringSimilarity = require("string-similarity"); 2 3var similarity = stringSimilarity.compareTwoStrings("healed", "sealed"); 4 5var matches = stringSimilarity.findBestMatch("healed", [ 6 "edward", 7 "sealed", 8 "theatre", 9]);
For browser apps
Include <script src="//unpkg.com/string-similarity/umd/string-similarity.min.js"></script>
to get the latest version.
Or <script src="//unpkg.com/string-similarity@4.0.1/umd/string-similarity.min.js"></script>
to get a specific version (4.0.1) in this case.
This exposes a global variable called stringSimilarity
which you can start using.
<script>
stringSimilarity.compareTwoStrings('what!', 'who?');
</script>
(The package is exposed as UMD, so you can consume it as such)
API
The package contains two methods:
compareTwoStrings(string1, string2)
Returns a fraction between 0 and 1, which indicates the degree of similarity between the two strings. 0 indicates completely different strings, 1 indicates identical strings. The comparison is case-sensitive.
Arguments
- string1 (string): The first string
- string2 (string): The second string
Order does not make a difference.
Returns
(number): A fraction from 0 to 1, both inclusive. Higher number indicates more similarity.
Examples
1stringSimilarity.compareTwoStrings("healed", "sealed"); 2// → 0.8 3 4stringSimilarity.compareTwoStrings( 5 "Olive-green table for sale, in extremely good condition.", 6 "For sale: table in very good condition, olive green in colour." 7); 8// → 0.6060606060606061 9 10stringSimilarity.compareTwoStrings( 11 "Olive-green table for sale, in extremely good condition.", 12 "For sale: green Subaru Impreza, 210,000 miles" 13); 14// → 0.2558139534883721 15 16stringSimilarity.compareTwoStrings( 17 "Olive-green table for sale, in extremely good condition.", 18 "Wanted: mountain bike with at least 21 gears." 19); 20// → 0.1411764705882353
findBestMatch(mainString, targetStrings)
Compares mainString
against each string in targetStrings
.
Arguments
- mainString (string): The string to match each target string against.
- targetStrings (Array): Each string in this array will be matched against the main string.
Returns
(Object): An object with a ratings
property, which gives a similarity rating for each target string, a bestMatch
property, which specifies which target string was most similar to the main string, and a bestMatchIndex
property, which specifies the index of the bestMatch in the targetStrings array.
Examples
1stringSimilarity.findBestMatch('Olive-green table for sale, in extremely good condition.', [ 2 'For sale: green Subaru Impreza, 210,000 miles', 3 'For sale: table in very good condition, olive green in colour.', 4 'Wanted: mountain bike with at least 21 gears.' 5]); 6// → 7{ ratings: 8 [ { target: 'For sale: green Subaru Impreza, 210,000 miles', 9 rating: 0.2558139534883721 }, 10 { target: 'For sale: table in very good condition, olive green in colour.', 11 rating: 0.6060606060606061 }, 12 { target: 'Wanted: mountain bike with at least 21 gears.', 13 rating: 0.1411764705882353 } ], 14 bestMatch: 15 { target: 'For sale: table in very good condition, olive green in colour.', 16 rating: 0.6060606060606061 }, 17 bestMatchIndex: 1 18}
Release Notes
2.0.0
- Removed production dependencies
- Updated to ES6 (this breaks backward-compatibility for pre-ES6 apps)
3.0.0
- Performance improvement for
compareTwoStrings(..)
: now O(n) instead of O(n^2) - The algorithm has been tweaked slightly to disregard spaces and word boundaries. This will change the rating values slightly but not enough to make a significant difference
- Adding a
bestMatchIndex
to the results forfindBestMatch(..)
to point to the best match in the suppliedtargetStrings
array
3.0.1
- Refactoring: removed unused functions; used
substring
instead ofsubstr
- Updated dependencies
4.0.1
- Distributing as an UMD build to be used in browsers.
4.0.2
- Update dependencies to latest versions.
4.0.3
- Make compatible with IE and ES5. Also, update deps. (see PR56)
4.0.4
- Simplify some conditional statements. Also, update deps. (see PR50)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
9 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-hc6q-2mpp-qw7j
- Warn: Project is vulnerable to: GHSA-4vvj-4cpr-p986
Reason
Found 1/15 approved changesets -- score normalized to 0
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 19 are checked with a SAST tool
Score
1.9
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to string-similarity
leven
Measure the difference between two strings using the Levenshtein distance algorithm
fastest-levenshtein
Fastest Levenshtein distance implementation in JS.
string-similarity-js
Calculates similarity between two strings
@types/string-similarity
TypeScript definitions for string-similarity