[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.

Add detailed readme

FoxxMD (Apr 4, 2023, 1:49 PM EDT) a3987506 c7095119

+61
+61
README.md
··· 1 + # string-sameness 2 + 3 + Generate a score that represents how closely the same two strings are. The comparison function uses an average of: 4 + 5 + * [Dice's Coefficient](https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient) 6 + * [Cosine Similarity](https://en.wikipedia.org/wiki/Cosine_similarity) 7 + * [Levenshtein Distance](https://en.wikipedia.org/wiki/Levenshtein_distance) 8 + 9 + weighted by the length of the content being compared (more weight for longer content). 10 + 11 + The sameness is then given a **score of 0 to 100.** 12 + 13 + * 0 => Totally unique pieces of content 14 + * 100 => Identical content 15 + 16 + ## Install/Usage 17 + 18 + ``` 19 + npm install @foxxmd/string-samness 20 + ``` 21 + 22 + ```js 23 + import compare from '@foxxmd/string-sameness'; 24 + 25 + const result = compare('This is one sentence', 'This is another sentence'); 26 + console.log(result); 27 + // { 28 + // "scores": { 29 + // "dice": 66.66, 30 + // "cosine": 75, 31 + // "leven": 79.16 32 + // }, 33 + // "highScore": 73.61, 34 + // "highScoreWeighted": 83.58 35 + // } 36 + ``` 37 + 38 + ### Normalization 39 + 40 + 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): 41 + 42 + * convert to lowercase 43 + * trim (remove whitespace at beginning/end) 44 + * remove non-alphanumeric characters (punctuation and newlines) 45 + * replace any instances of 2 or more consecutive whitespace with 1 whitespace 46 + 47 + This default set of functions is exported as `defaultStrCompareTransformFuncs`. 48 + 49 + Example of supplying your own transform functions: 50 + 51 + ```js 52 + import compare, {defaultStrCompareTransformFuncs} from '@foxxmd/string-sameness'; 53 + 54 + const myFuncs = [ 55 + ...defaultStrCompareTransformFuncs, 56 + // replace all vowels with the letter e 57 + (str) => str.replace(/[aeiou]/ig, 'e') 58 + ] 59 + 60 + const result = compare('This is one sentence', 'This is another sentence', myFuncs); 61 + ```