commit cf34397d9e064cc07b4dd65f95beb12c1e11be65
parent 632df9cb97eff17d17fac603e86fd9d7f32c98c5
Author: Dan Stillman <dstillman@zotero.org>
Date: Mon, 23 Jun 2008 20:28:42 +0000
Implements levenshtein as SQLite UDF
Function from Ben
Diffstat:
1 file changed, 45 insertions(+), 1 deletion(-)
diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js
@@ -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;
}