feat: Implement the Lev. Distance into the review

This commit is contained in:
Alexander Polynomdivision 2018-09-14 17:02:50 +02:00
parent e555b89f8f
commit 1e313915ce
2 changed files with 14 additions and 5 deletions

2
src/config.ts Normal file
View File

@ -0,0 +1,2 @@
// Maximum distance from the answer to be still considered correct
export const LEVENSHTEIN_MAX_DISTANCE = 2;

View File

@ -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<IProps, IState> {
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<IProps, IState> {
input: "",
});
}
} else if (minDist <= LEVENSHTEIN_MAX_DISTANCE) {
// TODO: Show a hint
console.log("Partially correct");
} else {
this.setState({
popoverOpen: true,