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

feat: Disable cosine for low token count strings

* Score is inaccurate when there are only a few words in the string so disable in order to keep combined strat score more accurate
* Add disclaimer to cosineSimilarity export docs
* Add matchingStrategies module to typedoc for visibility

FoxxMD (Oct 26, 2023, 1:44 PM EDT) 566b9058 112606f5

+46 -6
+5 -3
src/index.ts
··· 1 - import {cosineStrategy, levenStrategy, diceStrategy} from "./matchingStrategies/index.js"; 1 + import {cosineStrategy, levenStrategy, diceStrategy, cosineStrategyAggressive} from "./matchingStrategies/index.js"; 2 2 import { 3 3 ComparisonStrategyResult, 4 4 NamedComparisonStrategyObjectResult, ··· 84 84 const strategies = { 85 85 diceStrategy, 86 86 levenStrategy, 87 - cosineStrategy 87 + cosineStrategy, 88 + cosineStrategyAggressive 88 89 }; 89 90 90 91 export { ··· 94 95 createStringSameness, 95 96 defaultStrategies, 96 97 strategies, 97 - defaultStrCompareTransformFuncs 98 + defaultStrCompareTransformFuncs, 99 + ComparisonStrategyResult 98 100 }
+11
src/matchingStrategies/cosineSimilarity.d.ts
··· 1 1 import { ComparisonStrategy, ComparisonStrategyResultObject } from "../atomic.js"; 2 2 export declare const calculateCosineSimilarity: (strA: string, strB: string) => number; 3 + /** 4 + * Compares whole tokens (words) within a string independent of order 5 + * 6 + * This strategy is automatically disabled for strings with less than 4 words because it can lead to inaccurate scores due to not comparing characters IE it is not very useful for short sentences and comparing single words with typos 7 + * 8 + * If you'd like to use it even in these scenarios build your own strategy array using cosineStrategyAggressive instead of this one 9 + * */ 3 10 export declare const cosineStrategy: ComparisonStrategy<ComparisonStrategyResultObject>; 11 + /** 12 + * Always runs (strings are always valid) which may lead to inaccurate scores in low token-count strings 13 + * */ 14 + export declare const cosineStrategyAggressive: ComparisonStrategy<ComparisonStrategyResultObject>;
+27 -1
src/matchingStrategies/cosineSimilarity.ts
··· 69 69 return cosineSimilarity(termFreqVecA, termFreqVecB); 70 70 } 71 71 72 - export const cosineStrategy: ComparisonStrategy<ComparisonStrategyResultObject> = { 72 + const cosineBaseStrategy: ComparisonStrategy<ComparisonStrategyResultObject> = { 73 73 name: 'cosine', 74 74 strategy: (valA: string, valB: string) => { 75 75 let res = calculateCosineSimilarity(valA, valB); ··· 82 82 } 83 83 } 84 84 } 85 + 86 + /** 87 + * Compares whole tokens (words) within a string independent of order 88 + * 89 + * This strategy is automatically disabled for strings with less than 4 words because it can lead to inaccurate scores due to not comparing characters IE it is not very useful for short sentences and comparing single words with typos 90 + * 91 + * If you'd like to use it even in these scenarios build your own strategy array using cosineStrategyAggressive instead of this one 92 + * */ 93 + export const cosineStrategy: ComparisonStrategy<ComparisonStrategyResultObject> = { 94 + ...cosineBaseStrategy, 95 + isValid: (valA: string, valB: string) => { 96 + // cosine only compares full tokens (words), rather than characters, in a string 97 + // which makes its score very inaccurate when comparing low token-count strings (short sentences and/or words with typos) 98 + // so disable its usage if there are less than 4 tokens 99 + const valATokenLength = valA.split(' ').length; 100 + const valBTokenLength = valB.split(' ').length; 101 + return valATokenLength < 4 || valBTokenLength < 4; 102 + } 103 + } 104 + 105 + /** 106 + * Always runs (strings are always valid) which may lead to inaccurate scores in low token-count strings 107 + * */ 108 + export const cosineStrategyAggressive: ComparisonStrategy<ComparisonStrategyResultObject> = { 109 + ...cosineBaseStrategy 110 + }
+2 -1
src/matchingStrategies/index.ts
··· 1 1 import {levenStrategy} from "./levenSimilarity.js"; 2 - import {cosineStrategy} from "./cosineSimilarity.js"; 2 + import {cosineStrategy, cosineStrategyAggressive} from "./cosineSimilarity.js"; 3 3 import {diceStrategy} from "./diceSimilarity.js"; 4 4 import {ComparisonStrategy, ComparisonStrategyResultValue, ComparisonStrategyResultObject, StrategyFunc} from '../atomic.js'; 5 5 6 6 export { 7 7 levenStrategy, 8 8 cosineStrategy, 9 + cosineStrategyAggressive, 9 10 diceStrategy, 10 11 ComparisonStrategy, 11 12 ComparisonStrategyResultObject,
+1 -1
typedoc.json
··· 1 1 { 2 2 "$schema": "https://typedoc.org/schema.json", 3 3 "name": "string-sameness Docs", 4 - "entryPoints": ["./src"], 4 + "entryPoints": ["./src","./src/matchingStrategies/index.ts"], 5 5 "sort": ["source-order"], 6 6 "categorizeByGroup": false, 7 7 "searchGroupBoosts": {