-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
128 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
n-gram.js | ||
n-gram.min.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
module.exports = nGram; | ||
module.exports = nGram | ||
|
||
nGram.bigram = nGram(2); | ||
nGram.trigram = nGram(3); | ||
nGram.bigram = nGram(2) | ||
nGram.trigram = nGram(3) | ||
|
||
/* Factory returning a function that converts a given string | ||
* to n-grams. */ | ||
// Factory returning a function that converts a value string to n-grams. | ||
function nGram(n) { | ||
if (typeof n !== 'number' || isNaN(n) || n < 1 || n === Infinity) { | ||
throw new Error('`' + n + '` is not a valid argument for n-gram'); | ||
throw new Error('`' + n + '` is not a valid argument for n-gram') | ||
} | ||
|
||
return grams; | ||
return grams | ||
|
||
/* Create n-grams from a given value. */ | ||
// Create n-grams from a given value. | ||
function grams(value) { | ||
var nGrams = []; | ||
var index; | ||
var nGrams = [] | ||
var index | ||
|
||
if (value === null || value === undefined) { | ||
return nGrams; | ||
return nGrams | ||
} | ||
|
||
value = value.slice ? value : String(value); | ||
index = value.length - n + 1; | ||
value = value.slice ? value : String(value) | ||
index = value.length - n + 1 | ||
|
||
if (index < 1) { | ||
return nGrams; | ||
return nGrams | ||
} | ||
|
||
while (index--) { | ||
nGrams[index] = value.slice(index, index + n); | ||
nGrams[index] = value.slice(index, index + n) | ||
} | ||
|
||
return nGrams; | ||
return nGrams | ||
} | ||
} |
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
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
Oops, something went wrong.