add join delimiter

This commit is contained in:
Benjamin Calderon 2024-08-18 22:07:17 -04:00
parent 4e6efead61
commit 6197523735
No known key found for this signature in database

View file

@ -5,6 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import {JOIN_DELIM_OPTIONS} from "../lib/Delim.mjs";
/** /**
* ngram operation * ngram operation
@ -29,6 +30,11 @@ class Ngram extends Operation {
type: "number", type: "number",
value: 3 value: 3
}, },
{
"name": "Join delimiter",
"type": "editableOptionShort",
"value": JOIN_DELIM_OPTIONS
}
]; ];
} }
@ -38,13 +44,15 @@ class Ngram extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const n = args[0]; const nGramSize = args[0],
joinDelim = args[1];
const ngrams = []; const ngrams = [];
for (let i = 0; i <= input.length - n; i++) { for (let i = 0; i <= input.length - nGramSize; i++) {
ngrams.push(input.slice(i, i + n)); ngrams.push(input.slice(i, i + nGramSize));
} }
return ngrams.join("\n"); return ngrams.join(joinDelim);
} }
} }