From f81beea264c0db1b3f68d0ff7ac19b7cab62aa76 Mon Sep 17 00:00:00 2001 From: Benjamin Calderon Date: Sun, 18 Aug 2024 21:57:48 -0400 Subject: [PATCH] initial --- src/core/config/Categories.json | 3 +- src/core/operations/Ngram.mjs | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/Ngram.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index bebdd6a5..79ff6007 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -320,7 +320,8 @@ "Unescape string", "Pseudo-Random Number Generator", "Sleep", - "File Tree" + "File Tree", + "Ngram" ] }, { diff --git a/src/core/operations/Ngram.mjs b/src/core/operations/Ngram.mjs new file mode 100644 index 00000000..fbcbfce7 --- /dev/null +++ b/src/core/operations/Ngram.mjs @@ -0,0 +1,52 @@ +/** + * @author benjcal [benj.calderon@gmail.com] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * ngram operation + */ +class Ngram extends Operation { + + /** + * Ngram constructor + */ + constructor() { + super(); + + this.name = "Ngram"; + this.module = "Default"; + this.description = "Extracts n-grams from the input text. N-grams are contiguous sequences of n characters from a given text sample."; + this.infoURL = "https://wikipedia.org/wiki/N-gram"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "N-gram size", + type: "number", + value: 3 + }, + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const n = args[0]; + const ngrams = []; + for (let i = 0; i <= input.length - n; i++) { + ngrams.push(input.slice(i, i + n)); + } + + return ngrams.join("\n"); + } + +} + +export default Ngram;