Finds degree of similarity between strings, based on Dice's Coefficient.
npm install dice-string-similaritycompare function:
javascript
const { compare } = require('dice-string-similarity');
const similarityScore = compare('string1', 'string2');
console.log(similarityScore); // Outputs the similarity score between 0 and 1
`
$3
To find the best match for a string from an array of target strings, use the findBestMatch function:
`javascript
const { findBestMatch } = require('dice-string-similarity');
const mainString = 'main string to compare';
const targetStrings = ['target string 1', 'target string 2', 'target string 3'];
const bestMatch = findBestMatch(mainString, targetStrings);
console.log(bestMatch); // Outputs the best match object containing ratings, bestMatch, and bestMatchIndex
`
API Reference
$3
- Parameters
- first (string): The first string to compare.
- second (string): The second string to compare.
- Returns
- A number: The similarity score between the two strings, ranging from 0 to 1.
$3
- Parameters
- mainString (string): The main string to compare against.
- targetStrings (Array): An array of strings to compare the main string with.
- Returns
- An object containing:
- ratings: An array of objects, each containing a target string and its rating.
- bestMatch: The object from ratings with the highest rating.
- bestMatchIndex: The index of bestMatch in the ratings` array.