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 new operation: Levenshtein Distance #1498

Merged
merged 1 commit into from
Mar 23, 2023
Merged
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
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
"Fuzzy Match",
"Offset checker",
"Hamming Distance",
"Levenshtein Distance",
"Convert distance",
"Convert area",
"Convert mass",
Expand Down
98 changes: 98 additions & 0 deletions src/core/operations/LevenshteinDistance.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* @author mikecat
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* Levenshtein Distance operation
*/
class LevenshteinDistance extends Operation {

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

this.name = "Levenshtein Distance";
this.module = "Default";
this.description = "Levenshtein Distance (also known as Edit Distance) is a string metric to measure a difference between two strings that counts operations (insertions, deletions, and substitutions) on single character that are required to change one string to another.";
this.infoURL = "https://wikipedia.org/wiki/Levenshtein_distance";
this.inputType = "string";
this.outputType = "number";
this.args = [
{
name: "Sample delimiter",
type: "binaryString",
value: "\\n"
},
{
name: "Insertion cost",
type: "number",
value: 1
},
{
name: "Deletion cost",
type: "number",
value: 1
},
{
name: "Substitution cost",
type: "number",
value: 1
},
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {number}
*/
run(input, args) {
const [delim, insCost, delCost, subCost] = args;
const samples = input.split(delim);
if (samples.length !== 2) {
throw new OperationError("Incorrect number of samples. Check your input and/or delimiter.");
}
if (insCost < 0 || delCost < 0 || subCost < 0) {
throw new OperationError("Negative costs are not allowed.");
}
const src = samples[0], dest = samples[1];
let currentCost = new Array(src.length + 1);
let nextCost = new Array(src.length + 1);
for (let i = 0; i < currentCost.length; i++) {
currentCost[i] = delCost * i;
}
for (let i = 0; i < dest.length; i++) {
const destc = dest.charAt(i);
nextCost[0] = currentCost[0] + insCost;
for (let j = 0; j < src.length; j++) {
let candidate;
// insertion
let optCost = currentCost[j + 1] + insCost;
// deletion
candidate = nextCost[j] + delCost;
if (candidate < optCost) optCost = candidate;
// substitution or matched character
candidate = currentCost[j];
if (src.charAt(j) !== destc) candidate += subCost;
if (candidate < optCost) optCost = candidate;
// store calculated cost
nextCost[j + 1] = optCost;
}
const tempCost = nextCost;
nextCost = currentCost;
currentCost = tempCost;
}

return currentCost[currentCost.length - 1];
}

}

export default LevenshteinDistance;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ import "./tests/FletcherChecksum.mjs";
import "./tests/CMAC.mjs";
import "./tests/AESKeyWrap.mjs";
import "./tests/Rabbit.mjs";
import "./tests/LevenshteinDistance.mjs";

// Cannot test operations that use the File type yet
// import "./tests/SplitColourChannels.mjs";
Expand Down
165 changes: 165 additions & 0 deletions tests/operations/tests/LevenshteinDistance.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* @author mikecat
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
"name": "Levenshtein Distance: Wikipedia example 1",
"input": "kitten\nsitting",
"expectedOutput": "3",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, 1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: Wikipedia example 2",
"input": "saturday\nsunday",
"expectedOutput": "3",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, 1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: Wikipedia example 1 with substitution cost 2",
"input": "kitten\nsitting",
"expectedOutput": "5",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, 1, 2,
],
},
],
},
{
"name": "Levenshtein Distance: varied costs 1",
"input": "kitten\nsitting",
"expectedOutput": "230",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 10, 100, 1000,
],
},
],
},
{
"name": "Levenshtein Distance: varied costs 2",
"input": "kitten\nsitting",
"expectedOutput": "1020",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1000, 100, 10,
],
},
],
},
{
"name": "Levenshtein Distance: another delimiter",
"input": "kitten sitting",
"expectedOutput": "3",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
" ", 1, 1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: too few samples",
"input": "kitten",
"expectedOutput": "Incorrect number of samples. Check your input and/or delimiter.",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, 1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: too many samples",
"input": "kitten\nsitting\nkitchen",
"expectedOutput": "Incorrect number of samples. Check your input and/or delimiter.",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, 1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: negative insertion cost",
"input": "kitten\nsitting",
"expectedOutput": "Negative costs are not allowed.",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", -1, 1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: negative deletion cost",
"input": "kitten\nsitting",
"expectedOutput": "Negative costs are not allowed.",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, -1, 1,
],
},
],
},
{
"name": "Levenshtein Distance: negative substitution cost",
"input": "kitten\nsitting",
"expectedOutput": "Negative costs are not allowed.",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 1, 1, -1,
],
},
],
},
{
"name": "Levenshtein Distance: cost zero",
"input": "kitten\nsitting",
"expectedOutput": "0",
"recipeConfig": [
{
"op": "Levenshtein Distance",
"args": [
"\\n", 0, 0, 0,
],
},
],
},
]);