Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ngrams operation #1877

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@
"Unescape string",
"Pseudo-Random Number Generator",
"Sleep",
"File Tree"
"File Tree",
"N-gram"
]
},
{
Expand Down
60 changes: 60 additions & 0 deletions src/core/operations/Ngram.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @author benjcal [benj.calderon@gmail.com]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import {JOIN_DELIM_OPTIONS} from "../lib/Delim.mjs";

/**
* ngram operation
*/
class Ngram extends Operation {

/**
* Ngram constructor
*/
constructor() {
super();

this.name = "N-gram";
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
},
{
"name": "Join delimiter",
"type": "editableOptionShort",
"value": JOIN_DELIM_OPTIONS
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const nGramSize = args[0],
joinDelim = args[1];

const ngrams = [];
for (let i = 0; i <= input.length - nGramSize; i++) {
ngrams.push(input.slice(i, i + nGramSize));
}

return ngrams.join(joinDelim);
}

}

export default Ngram;
Loading