[READ-ONLY] Mirror of https://github.com/FoxxMD/string-sameness. Compare the sameness of two strings foxxmd.github.io/string-sameness/
compare cosine-similarity dice-coefficient levenshtein-distance sameness string text typescript
0

Configure Feed

Select the types of activity you want to include in your feed.

4 2 0

Clone this repository

https://tangled.org/foxxmd.dev/string-sameness https://tangled.org/did:plc:ho6znieqd4yxc6uqwcgabtg5
git@tangled.org:foxxmd.dev/string-sameness git@tangled.org:did:plc:ho6znieqd4yxc6uqwcgabtg5

For self-hosted knots, clone URLs may differ based on your setup.



README.md

string-sameness#

Generate a score that represents how closely the same two strings are. The comparison function uses an average of:

weighted by the length of the content being compared (more weight for longer content).

The sameness is then given a score of 0 to 100.

  • 0 => Totally unique pieces of content
  • 100 => Identical content

Install/Usage#

npm install @foxxmd/string-sameness
import compare from '@foxxmd/string-sameness';

const result =  compare('This is one sentence', 'This is another sentence');
console.log(result);
// {
//     "scores": {
//         "dice": 66.66,
//         "cosine": 75,
//         "leven": 79.16
//      },
//     "highScore": 73.61,
//     "highScoreWeighted": 83.58
//  }

Normalization#

A third argument may be passed to compare to provide a list of functions to apply to the strings to compare. When not explicitly provided a default set of functions is applied to normalize the strings (to remove trivial differences):

  • convert to lowercase
  • trim (remove whitespace at beginning/end)
  • remove non-alphanumeric characters (punctuation and newlines)
  • replace any instances of 2 or more consecutive whitespace with 1 whitespace

This default set of functions is exported as defaultStrCompareTransformFuncs.

Example of supplying your own transform functions:

import compare, {defaultStrCompareTransformFuncs} from '@foxxmd/string-sameness';

const myFuncs = [
    ...defaultStrCompareTransformFuncs,
    // replace all vowels with the letter e
    (str) => str.replace(/[aeiou]/ig, 'e')
]

const result =  compare('This is one sentence', 'This is another sentence', myFuncs);