From 1e313915ce9426fc76f0b4025b2217ead30a6d7f Mon Sep 17 00:00:00 2001 From: Alexander Polynomdivision Date: Fri, 14 Sep 2018 17:02:50 +0200 Subject: [PATCH] feat: Implement the Lev. Distance into the review --- src/config.ts | 2 ++ src/pages/review.tsx | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 src/config.ts diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..0170d89 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,2 @@ +// Maximum distance from the answer to be still considered correct +export const LEVENSHTEIN_MAX_DISTANCE = 2; diff --git a/src/pages/review.tsx b/src/pages/review.tsx index 633f745..ca49de1 100644 --- a/src/pages/review.tsx +++ b/src/pages/review.tsx @@ -13,6 +13,9 @@ import { Redirect } from "react-router-dom"; import { IVocab, ReviewMode, VocabType } from "../models/vocab"; +import { levW } from "../algorithms/levenshtein"; +import { LEVENSHTEIN_MAX_DISTANCE } from "../config"; + interface IProps { levelId: number; vocabByLevel: (level: number) => IVocab[]; @@ -60,13 +63,14 @@ export default class ReviewPage extends React.Component { const current = this.currentVocab(); // Check if the given answer is somewhere in the german words - const german = this.currentVocab().german.map((str) => str.toLowerCase()); - const found = german.find((el) => el === this.state.input.toLowerCase()); + const { input } = this.state; + const german = current.german.map((str) => str.toLowerCase()); + const dists = german.map((ger) => levW(input.toLowerCase(), ger)); + const minDist = Math.min(...dists); // Check if the user's answer was correct - // TODO: Levensthein-Distance? - if (found) { - // TODO: Show it's correct + if (minDist === 0) { + // TODO: Show it's correct? // Show the next vocab word if (this.state.current + 1 >= this.vocab.length) { // TODO: Set some data that the summary screen will show @@ -80,6 +84,9 @@ export default class ReviewPage extends React.Component { input: "", }); } + } else if (minDist <= LEVENSHTEIN_MAX_DISTANCE) { + // TODO: Show a hint + console.log("Partially correct"); } else { this.setState({ popoverOpen: true,