Skip to content

Commit

Permalink
Merge pull request #387 from Renumics/feature/levenshtein-metric
Browse files Browse the repository at this point in the history
Feature/levenshtein metric
  • Loading branch information
neindochoh authored Dec 8, 2023
2 parents b022ff8 + 1866154 commit e0aff67
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"d3-zoom": "^3.0.0",
"detect-gpu": "^5.0.12",
"dompurify": "^3.0.3",
"fast-levenshtein": "^3.0.0",
"file-saver": "^2.0.5",
"flexlayout-react": "^0.7.6",
"fuse.js": "^6.6.2",
Expand Down Expand Up @@ -98,6 +99,7 @@
"@types/chroma-js": "^2.4.0",
"@types/d3": "^7.4.0",
"@types/d3-cloud": "^1.2.5",
"@types/fast-levenshtein": "^0.0.4",
"@types/file-saver": "^2.0.5",
"@types/jest": "^29.4.0",
"@types/lodash": "^4.14.191",
Expand Down
66 changes: 43 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/widgets/MetricsWidget/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import levenshtein from 'fast-levenshtein';
import { Metric } from './types';
import { computeConfusion } from './confusion';

Expand Down Expand Up @@ -97,4 +98,23 @@ export const METRICS: Record<string, Metric> = {
);
},
},
levenshtein: {
signature: {
X: ['str'],
Y: ['str'],
},
compute: ([actualValues, assignedValues]) => {
let sum = 0;

for (let i = 0; i < actualValues.length; i++) {
const actual = actualValues[i];
const assigned = assignedValues[i];

const dist = levenshtein.get(actual as string, assigned as string);
sum += dist;
}

return sum / actualValues.length;
},
},
};
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

0 comments on commit e0aff67

Please sign in to comment.