Implements levenshtein as SQLite UDF

Function from Ben
This commit is contained in:
Dan Stillman 2008-06-23 20:28:42 +00:00
parent 632df9cb97
commit cf34397d9e

View file

@ -59,7 +59,7 @@ Zotero.DBConnection = function(dbName) {
this._dbIsCorrupt = null
this._self = this;
}
/////////////////////////////////////////////////////////////////
//
// Public methods
@ -951,6 +951,50 @@ Zotero.DBConnection.prototype._getDBConnection = function () {
observerService.addObserver(this, "xpcom-shutdown", false);
observerService = null;
// Levenshtein distance UDF
//
// Implements mozIStorageFunction
// TODO: move somewhere else
var lev = {
min3: function (a, b, c) {
var min;
min = a;
if (b < min)
min = b;
if (c < min)
min = c;
return min;
},
onFunctionCall: function (arg) {
var a = arg.getUTF8String(0);
var b = arg.getUTF8String(1);
var arr = new Array(a.length+1);
var i, j, cost;
for(i=0; i<=a.length; i++)
arr[i] = new Array(b.length);
for (i = 0; i <= a.length; i++) {
arr[i][0] = i;
}
for (j = 0; j <= b.length; j++) {
arr[0][j] = j;
}
for (i = 1; i <= a.length; i++) {
for (j = 1; j <= b.length; j++) {
cost = (a[i-1] == b[j-1])? 0 : 1;
arr[i][j] = this.min3(arr[i-1][j] + 1, arr[i][j-1] + 1, arr[i-1][j-1] + cost);
}
}
return arr[a.length][b.length];
}
};
this._connection.createFunction('levenshtein', 2, lev);
return this._connection;
}