Created
July 4, 2018 20:19
-
-
Save stevekrouse/e4c6d782144539907fc30400438243f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_nonWordRe = /[^a-zA-Z0-9\u00C0-\u00FF, ]+/g; | |
_iterateGrams = function(value, gramSize) { | |
gramSize = gramSize || 2; | |
var simplified = '-' + value.toLowerCase().replace(_nonWordRe, '') + '-', | |
lenDiff = gramSize - simplified.length, | |
results = []; | |
if (lenDiff > 0) { | |
for (var i = 0; i < lenDiff; ++i) { | |
value += '-'; | |
} | |
} | |
for (var i = 0; i < simplified.length - gramSize + 1; ++i) { | |
results.push(simplified.slice(i, i + gramSize)); | |
} | |
return results; | |
}; | |
_gramCounter = function(value, gramSize) { | |
// return an object where key=gram, value=number of occurrences | |
gramSize = gramSize || 2; | |
var result = {}, | |
grams = _iterateGrams(value, gramSize), | |
i = 0; | |
for (i; i < grams.length; ++i) { | |
if (grams[i] in result) { | |
result[grams[i]] += 1; | |
} else { | |
result[grams[i]] = 1; | |
} | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment