1.0.2 • Published 5 years ago

cos1ne-similarity v1.0.2

Weekly downloads
2
License
ISC
Repository
gitlab
Last release
5 years ago

Cos1ne Similarity

Measures similarity between two Strings calculating the cosine of the angle between them.

Returns a value between 0 (completely different) and 1 (same string).

ParamTypeDescription
string1stringfirst string to compare
string2stringsecond string to compare
termFrequencybooleanConsiders Terms instead Chars if is set. False by default.

Install

npm i cos1ne-similarity

Examples

const cosSimilarity= require('cos1ne-similarity'); 

const text= 'I want eat';

const pool = [
    'Drink me!',
    'Eat me!',
    'I would like something to eat'
];

console.log('#Comparing:', text);
for (let i = 0; i < pool.length; i++) {
    const similarity = cosSimilarity(text, pool[i]);
    console.log('#TEXT:', pool[i], '#->', similarity);
}

// #Comparing: I want eat
// #TEXT: Drink me! #-> 0.30618621784789724
// #TEXT: Eat me! #-> 0.6123724356957945
// #TEXT: I would like something to eat #-> 0.6531972647421809

console.log('#Comparing:', text);
for (let i = 0; i < pool.length; i++) {
    const similarity = cosSimilarity(text, pool[i], true);
    console.log('#TEXT:', pool[i], '#->', similarity);
}

// #Comparing: I want eat
// #TEXT: Drink me! #-> 0
// #TEXT: Eat me! #-> 0.33333333333333337
// #TEXT: I would like something to eat #-> 0.47140452079103173

More info

@see https://en.wikipedia.org/wiki/Cosine_similarity