Skip to content

Commit

Permalink
Refactor out function to choose a masked word
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Kumar <[email protected]>
  • Loading branch information
abhi-kr-2100 committed Apr 7, 2024
1 parent ac6ba65 commit 716a7d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
14 changes: 8 additions & 6 deletions webapp/src/PlayPage/Play.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ export interface PlayProps {
export interface SentenceData {
sentence: SentenceType & { sentenceScoreId: Types.ObjectId };
translations: SentenceType[];
// According to Mongoose lastReviewDate is of type Date, but it's actually
// a string
words: (Omit<WordScoreType, 'score'> & {
_id: string;
score: Omit<ScoreType, 'lastReviewDate'> & { lastReviewDate: string };
})[];
words: CorrectedWordScoreType[];
}

// According to Mongoose lastReviewDate is of type Date, but it's actually a
// string
export type CorrectedWordScoreType = Omit<WordScoreType, 'score'> & {
_id: string;
score: Omit<ScoreType, 'lastReviewDate'> & { lastReviewDate: string };
};
53 changes: 32 additions & 21 deletions webapp/src/PlayPage/Questions.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { SentenceData } from './Play';
import { CorrectedWordScoreType, SentenceData } from './Play';
import FillInTheBlanks from '../components/FillInTheBlanks';
import { Box, Button } from '@mui/material';
import {
Expand Down Expand Up @@ -161,37 +161,48 @@ function useSolution(
}

function getFillInTheBlanksQuestion(question: SentenceData, lm: LanguageModel) {
const weights = question.words.map((word) => {
const lastReviewDate = word.score.lastReviewDate
? DateTime.fromISO(word.score.lastReviewDate)
const { wordToMask, intervalToMask } = chooseMaskedWordWeighted(
question.sentence.text!,
question.words,
lm,
);

const textBefore = question.sentence.text!.slice(0, intervalToMask[0]);
const textAfter = question.sentence.text!.slice(intervalToMask[1]);

return {
textBefore,
maskedWord: wordToMask.word!.wordText!,
textAfter,
maskedWordId: wordToMask._id,
};
}

function chooseMaskedWordWeighted(
text: string,
wordScores: CorrectedWordScoreType[],
lm: LanguageModel,
) {
const weights = wordScores.map((wordScore) => {
const lastReviewDate = wordScore.score?.lastReviewDate
? DateTime.fromISO(wordScore.score.lastReviewDate)
: DateTime.now();
const daysSinceLastReview = Math.round(
-lastReviewDate.diffNow('days').days,
);
const level = Math.max(
1,
word.score.interRepetitionIntervalInDays! - daysSinceLastReview,
wordScore.score.interRepetitionIntervalInDays! - daysSinceLastReview,
);

return 1.0 / (level * word.score.easinessFactor);
return 1.0 / (level * wordScore.score.easinessFactor);
});

console.log(weights);

const wordToMask = chance.weighted(question.words, weights);
const occurrences = lm.findWord(
question.sentence.text!,
wordToMask.word!.wordText!,
);
const wordToMask = chance.weighted(wordScores, weights);
const occurrences = lm.findWord(text, wordToMask.word!.wordText!);
const occurrenceToMask = sample(occurrences)!;

const textBefore = question.sentence.text!.slice(0, occurrenceToMask[0]);
const textAfter = question.sentence.text!.slice(occurrenceToMask[1]);

return {
textBefore,
maskedWord: wordToMask.word!.wordText!,
textAfter,
maskedWordId: wordToMask._id,
wordToMask,
intervalToMask: occurrenceToMask,
};
}

0 comments on commit 716a7d8

Please sign in to comment.