Merge d80b5e28cb5adf89cff643722431b514af60b097 into ec4f1f560566886562d85c368c8d738429f6d9ed
This commit is contained in:
commit
bc43a27d04
@ -348,6 +348,7 @@
|
|||||||
"Pseudo-Random Number Generator",
|
"Pseudo-Random Number Generator",
|
||||||
"Sleep",
|
"Sleep",
|
||||||
"File Tree",
|
"File Tree",
|
||||||
|
"N-gram"
|
||||||
"Take nth bytes",
|
"Take nth bytes",
|
||||||
"Drop nth bytes"
|
"Drop nth bytes"
|
||||||
]
|
]
|
||||||
|
|||||||
60
src/core/operations/Ngram.mjs
Normal file
60
src/core/operations/Ngram.mjs
Normal file
@ -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;
|
||||||
@ -122,6 +122,7 @@ import "./tests/MS.mjs";
|
|||||||
import "./tests/MultipleBombe.mjs";
|
import "./tests/MultipleBombe.mjs";
|
||||||
import "./tests/MurmurHash3.mjs";
|
import "./tests/MurmurHash3.mjs";
|
||||||
import "./tests/NetBIOS.mjs";
|
import "./tests/NetBIOS.mjs";
|
||||||
|
import "./tests/Ngram.mjs";
|
||||||
import "./tests/NormaliseUnicode.mjs";
|
import "./tests/NormaliseUnicode.mjs";
|
||||||
import "./tests/NTLM.mjs";
|
import "./tests/NTLM.mjs";
|
||||||
import "./tests/OTP.mjs";
|
import "./tests/OTP.mjs";
|
||||||
|
|||||||
34
tests/operations/tests/Ngram.mjs
Normal file
34
tests/operations/tests/Ngram.mjs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Ngram tests.
|
||||||
|
*
|
||||||
|
* @author jg42526
|
||||||
|
*
|
||||||
|
* @copyright Crown Copyright 2017
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Ngram 3",
|
||||||
|
input: "hello",
|
||||||
|
expectedOutput: "hel,ell,llo",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "N-gram",
|
||||||
|
args: ["3", ","],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Ngram longer than input",
|
||||||
|
input: "hello",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "N-gram",
|
||||||
|
args: ["6", ","],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
||||||
Loading…
x
Reference in New Issue
Block a user