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

Levenshtein distance #306

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 16 additions & 0 deletions src/widgets/MetricsWidget/levenshtein.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function computeLevenshtein(actualValues: string, assignedValues: string): number {
if (actualValues.length === 0) {
return assignedValues.length;
}
if (assignedValues.length === 0) {
return actualValues.length;
}

const cost = assignedValues[0] === actualValues[0] ? 0 : 1;

const deletion = computeLevenshtein(actualValues.slice(1), assignedValues) + 1;
const insertion = computeLevenshtein(actualValues, assignedValues.slice(1)) + 1;
const substitution = computeLevenshtein(actualValues.slice(1), assignedValues.slice(1)) + cost;

return Math.min(deletion, insertion, substitution);
}
19 changes: 19 additions & 0 deletions src/widgets/MetricsWidget/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash';
import { Metric } from './types';
import { computeConfusion } from './confusion';
import { computeLevenshtein } from './levenshtein';

export const METRICS: Record<string, Metric> = {
sum: {
Expand Down Expand Up @@ -97,4 +98,22 @@
);
},
},
Levenshtein: {
signature: {
X: ['str'],
Y: ['str'],
},
compute: ([actualValues, assignedValues]) => {
const numValues = actualValues.length;
var sumLevenshtein = 0;

Check failure on line 108 in src/widgets/MetricsWidget/metrics.ts

View workflow job for this annotation

GitHub Actions / 🔍 Check

Unexpected var, use let or const instead

for (let i = 0; i < actualValues.length; i++) {
const actual = actualValues[i];
const assigned = assignedValues[i];
sumLevenshtein += computeLevenshtein(actual as string, assigned as string);
}

return sumLevenshtein / numValues;
},
},
};
2 changes: 1 addition & 1 deletion src/widgets/MetricsWidget/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataKind } from '../../datatypes';

export type ValueArray = number[] | Int32Array | boolean[];
export type ValueArray = number[] | Int32Array | boolean[] | string[];

export interface Metric {
signature: Record<string, DataKind | DataKind[]>;
Expand Down
Loading