fuzzyset.js
Advanced tools
Comparing version 0.0.1 to 0.0.2
(function() { | ||
var FuzzySet = function(arr, useLevenshtein, gramSizeLower, gramSizeUpper) { | ||
var fuzzyset = { | ||
version: '0.0.1' | ||
}; | ||
var fuzzyset = {}; | ||
@@ -12,6 +10,6 @@ // default options | ||
fuzzyset.gramSizeUpper = gramSizeUpper || 3; | ||
fuzzyset.useLevenshtein = useLevenshtein || true; | ||
fuzzyset.useLevenshtein = (typeof useLevenshtein !== 'boolean') ? true : useLevenshtein; | ||
// define all the object functions and attributes | ||
fuzzyset.exactSet = {} | ||
fuzzyset.exactSet = {}; | ||
fuzzyset.matchDict = {}; | ||
@@ -43,4 +41,4 @@ fuzzyset.items = {}; | ||
var _distance = function(str1, str2) { | ||
if (str1 == null && str2 == null) throw 'Trying to compare two null values' | ||
if (str1 == null || str2 == null) return 0; | ||
if (str1 === null && str2 === null) throw 'Trying to compare two null values'; | ||
if (str1 === null || str2 === null) return 0; | ||
str1 = String(str1); str2 = String(str2); | ||
@@ -55,3 +53,3 @@ | ||
}; | ||
var _nonWordRe = /[^\w, ]+/; | ||
var _nonWordRe = /[^a-zA-Z0-9\u00C0-\u00FF, ]+/; | ||
@@ -69,3 +67,3 @@ var _iterateGrams = function(value, gramSize) { | ||
for (var i = 0; i < simplified.length - gramSize + 1; ++i) { | ||
results.push(simplified.slice(i, i + gramSize)) | ||
results.push(simplified.slice(i, i + gramSize)); | ||
} | ||
@@ -76,2 +74,3 @@ return results; | ||
var _gramCounter = function(value, gramSize) { | ||
// return an object where key=gram, value=number of occurrences | ||
gramSize = gramSize || 2; | ||
@@ -93,4 +92,5 @@ var result = {}, | ||
fuzzyset.get = function(value, defaultValue) { | ||
// check for value in set, returning defaultValue or null if none found | ||
var result = this._get(value); | ||
if (!result && defaultValue) { | ||
if (!result && typeof defaultValue !== 'undefined') { | ||
return defaultValue; | ||
@@ -107,4 +107,6 @@ } | ||
} | ||
var results = []; | ||
for (var gramSize = this.gramSizeUpper; gramSize > this.gramSizeLower; --gramSize) { | ||
// start with high gram size and if there are no results, go to lower gram sizes | ||
for (var gramSize = this.gramSizeUpper; gramSize >= this.gramSizeLower; --gramSize) { | ||
results = this.__get(value, gramSize); | ||
@@ -216,3 +218,3 @@ if (results) { | ||
gram, gramCount; | ||
for (var gram in gramCounts) { | ||
for (gram in gramCounts) { | ||
gramCount = gramCounts[gram]; | ||
@@ -233,3 +235,3 @@ sumOfSquareGramCounts += Math.pow(gramCount, 2); | ||
fuzzyset._normalizeStr = function(str) { | ||
if (Object.prototype.toString.call(str) !== '[object String]') throw 'Must use a string as argument to FuzzySet functions' | ||
if (Object.prototype.toString.call(str) !== '[object String]') throw 'Must use a string as argument to FuzzySet functions'; | ||
return str.toLowerCase(); | ||
@@ -266,3 +268,3 @@ }; | ||
if (this.exactSet.hasOwnProperty(prop)) { | ||
values.push(this.exactSet[prop]) | ||
values.push(this.exactSet[prop]); | ||
} | ||
@@ -269,0 +271,0 @@ } |
{ | ||
"name":"fuzzyset.js", | ||
"description":"A fuzzy string set for JavaScript", | ||
"version":"0.0.1", | ||
"version":"0.0.2", | ||
"homepage":"http://Glench.github.com/fuzzyset.js", | ||
@@ -6,0 +6,0 @@ "repository":"git://github.com/Glench/fuzzyset.js.git", |
Sorry, the diff of this file is not supported yet
14693
6
259