Resemblance
This package contains functions to get the percentage of resemblance (likeness, similarity) between two strings or two objects.
The algorithm used is actually quite simple : it does a case-insensitive levenshtein distance, and also ignores non-alphanumerical characters (such as spaces, dashes, dots, etc).
Quick jump to examples :
How to use ?
The package contains 3 functions :
var resemblance = ;// this modules has 3 functions :// resemblance.compareStrings(a, b)// resemblance.compareObjects(a, b, weights)// resemblance.getSimilar(obj, set, threshold, weights)
Examples
Simple string comparison (percentage of resemblance)
// the following two strings are a perfect match// 100% resemblance :resemblance;
// 90% resemblance :resemblance;
// 50% resemblance :resemblance;
// 0% resemblance :resemblance;
Object comparison (percentage of resemblance)
// 100% resemblance :var a = name: 'David' ;var b = name: 'David' ;var weights = name: 100 ;resemblance;
// 67% resemblance :resemblance;// a is 100% similar,// b is 100% different,// c does not matter (weight = 0)
Get similar objects from a set
In this example, we would like to find similar address
objects. the set
parameter can be fetched from a database, for example.
// object to comparevar address = num: '17' way: 'Boulevard Antoine de Saint-Exupéry' zip: '69009' city: 'Lyon' country: 'France'; // object setvar addresses = num: '17' way: 'BOULEVARD SAINT EXUPERY' zip: '69009' city: 'LYON' country: 'FR' geoloc: lat: 45 lon: 119 num: '17' way: 'Rue Antoine de Saint-Exupéry' zip: '69140' city: 'Rillieux-la-Pape' country: 'France' ; // weights of each properties// note that property names can be deeper than one// level for example, we could put some weight on// the 'geoloc.lat' propertyvar weights = num: 20 way: 35 zip: 15 city: 20 country: 10 geoloc: 0; // get addresses that are more than 50% similarresemblance // returns : resemblance: 08283333333333333 obj: num: '17' way: 'BOULEVARD SAINT EXUPERY' zip: '69009' city: 'LYON' country: 'FR' geoloc: lat: 45 lon: 119 resemblance: 06426190476190476 obj: num: '17' way: 'Rue Antoine de Saint-Exupéry' zip: '69140' city: 'Rillieux-la-Pape' country: 'France'