fix: Crash when comparing two long words

This commit is contained in:
Alexander Polynomdivision 2018-10-09 12:48:12 +02:00
parent 3834590c5a
commit 7e27203f74
4 changed files with 5 additions and 54 deletions

View File

@ -1,6 +1,6 @@
{
"name": "seminarfach",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "index.js",
"scripts": {
@ -12,6 +12,7 @@
"dependencies": {
"@material-ui/core": "^1.5.1",
"@material-ui/icons": "^2.0.3",
"js-levenshtein": "^1.1.4",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-redux": "^5.0.7",

View File

@ -1,21 +0,0 @@
import { lev, levW } from "../";
test("lev(abc, abc) == 0", () => {
expect(lev("abc", "abc", 3, 3)).toBe(0);
expect(levW("abc", "abc")).toBe(0);
});
test("lev(abc, abd) == 1", () => {
expect(lev("abc", "abd", 3, 3)).toBe(1);
expect(levW("abc", "abd")).toBe(1);
});
test("lev(abc, abcd) == 1", () => {
expect(lev("abc", "abcd", 3, 4)).toBe(1);
expect(levW("abc", "abcd")).toBe(1);
});
test("lev(abcd, bcd) == 1", () => {
expect(lev("abcd", "bcd", 4, 3)).toBe(1);
expect(levW("abcd", "bcd")).toBe(1);
});

View File

@ -1,30 +0,0 @@
function one(a: string, b: string, i: number, j: number): number {
if (a[i] === b[j]) {
return 0;
}
return 1;
}
// Computes the difference between the strings a and b.
// @a, b: The strings to compare
// @i, j: From where to compare (a_i and b_j)
// @ret : The distance in insertions, deletions and changes
export function lev(a: string, b: string, i: number, j: number): number {
if (Math.min(i, j) === 0) {
return Math.max(i, j);
} else {
return Math.min(
lev(a, b, i - 1, j) + 1,
lev(a, b, i, j - 1) + 1,
lev(a, b, i - 1, j - 1) + one(a, b, i, j)
);
}
};
// Computes the difference between the strings a and b.
// @a, b: The strings to compare
// @ret : The distance in insertions, deletions and changes
export function levW(a: string, b: string): number {
return lev(a, b, a.length, b.length);
}

View File

@ -27,7 +27,8 @@ import {
} from "../models/vocab";
import { ReviewType, IReviewMetadata } from "../models/review";
import { levW } from "../algorithms/levenshtein";
//@ts-ignore
import lev from "js-levenshtein";
import {
LEVENSHTEIN_MAX_DISTANCE, MAX_ERROR_THRESHOLD,
REVIEW_HELP_MOD
@ -213,7 +214,7 @@ const ReviewPageWithRouter = withRouter(
// Map all possible answers to lowercase ( => ignore casing)
const answers = current.answers.map((el) => el.toLowerCase());
// Calculate the distances to all possible answers
const dists = answers.map((el) => levW(input.toLowerCase(), el));
const dists = answers.map((el) => lev(input.toLowerCase(), el));
// Find the lowest distance
const minDist = Math.min(...dists);