Skip to content

Instantly share code, notes, and snippets.

@antoniogarrote
Created April 25, 2011 20:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antoniogarrote/941172 to your computer and use it in GitHub Desktop.
Save antoniogarrote/941172 to your computer and use it in GitHub Desktop.
print("connected");
var mapFn = function(){
var minSim = 0.1;
var fieldName = "text";
var queryText = "ola";
var maxSimilarity = function(a,b) {
var aSize = a.length;
var bSize = b.length;
if(aSize > bSize) {
return 1 - ((aSize - bSize) / aSize)
} else {
return 1 - ((bSize - aSize) / bSize)
}
};
var levenshtein = function(a, b){
if(a == b)return 0;
if(!a.length || !b.length)return b.length || a.length;
var len1 = a.length + 1,
len2 = b.length + 1,
I = 0,
i = 0,
d = [[0]],
c, j, J;
while(++i < len2)
d[0][i] = i;
i = 0;
while(++i < len1){
J = j = 0;
c = a[I];
d[i] = [i];
while(++j < len2){
d[i][j] = Math.min(d[I][j] + 1, d[i][J] + 1, d[I][J] + (c != b[J]));
++J;
};
++I;
};
return d[len1 - 1][len2 - 1];
};
if(maxSimilarity(this[fieldName],queryText) > minSim) {
var dist = levenshtein(this[fieldName],queryText);
var metric = 1 - (dist / Math.max(this[fieldName].length, queryText.length))
if(metric > minSim) {
var match = {score: metric, match:this };
emit(this._id, match);
}
}
};
db.testsmr.mapReduce(
mapFn,
function(k,vals){
return vals[0];
},
{out: { inline : 1}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment