Skip to content

Commit

Permalink
Set the score of sentence based on the score of the most difficult co…
Browse files Browse the repository at this point in the history
…ntituent word

Signed-off-by: Abhishek Kumar <[email protected]>
  • Loading branch information
abhi-kr-2100 committed Mar 30, 2024
1 parent 0b6389b commit f5a883a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
26 changes: 24 additions & 2 deletions express-backend/src/api/sentences.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { NextFunction, Request, Response, Router } from 'express';

import SentenceScore from '../sentence/sentenceScore';
import WordScore from '../word/wordScore';

import getUpdatedWordScore, { GradeType } from '../word/scoringAlgorithm';
import { SentenceType } from '../sentence';
import { GradeType, getUpdatedSentenceScore } from '../word/scoringAlgorithm';
import { getLanguageModel } from '../language-models';

export async function updateScore(
req: Request,
Expand All @@ -23,9 +26,14 @@ export async function updateScore(
return next('Sentence score was not found.');
}

const updatedScore = getUpdatedWordScore(
const wordScores = await getWordScoresFromSentence(
sentenceScore.sentence,
sentenceScore.owner._id.toString(),
);
const updatedScore = getUpdatedSentenceScore(
grade as GradeType,
sentenceScore.score,
wordScores,
);
sentenceScore.score = updatedScore;
await sentenceScore.save();
Expand All @@ -36,4 +44,18 @@ export async function updateScore(
const router = Router();
router.post('/:id/updateScore', updateScore);

async function getWordScoresFromSentence(
sentence: SentenceType,
ownerId: string,
) {
const lm = getLanguageModel(sentence.textLanguageCode);
const words = lm.getWords(sentence.text);

const storedWordScores = await WordScore.find({
'word.wordText': { $in: words },
'owner._id': ownerId,
});
return storedWordScores;
}

export default router;
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import SentenceScore, {
} from '../../../sentence/sentenceScore';
import { UserProfile, UserProfileType } from '../../../user-profile';
import { updateScore } from '../../../api/sentences';
import WordScore, { WordScoreType } from '../../../word/wordScore';
import Word from '../../../word/word';

describe('Update sentence score', () => {
let mongoDB: MongoMemoryServer;
Expand All @@ -25,6 +27,7 @@ describe('Update sentence score', () => {
let sampleSentence: SentenceType;
let sampleSentenceScore: Document<Types.ObjectId, {}, SentenceScoreType> &
SentenceScoreType;
let sampleWordScore: WordScoreType;

beforeAll(async () => {
mongoDB = await MongoMemoryServer.create();
Expand All @@ -38,6 +41,22 @@ describe('Update sentence score', () => {
text: 'Dummy!',
textLanguageCode: 'en',
});

const sampleWord = await Word.create({
wordText: 'dummy',
languageCode: 'en',
});

sampleWordScore = await WordScore.create({
word: sampleWord,
owner: alice,
score: {
repetitionNumber: 0,
easinessFactor: 1.5,
interRepetitionIntervalInDays: 1,
lastReviewDate: new Date(),
},
});
});

afterAll(async () => {
Expand Down Expand Up @@ -87,6 +106,12 @@ describe('Update sentence score', () => {
expect(updatedSentenceScore.score.lastReviewDate.toDateString()).toBe(
new Date().toDateString(),
);
expect(updatedSentenceScore.score.easinessFactor).toBe(
sampleWordScore.score.easinessFactor,
);
expect(updatedSentenceScore.score.interRepetitionIntervalInDays).toBe(
sampleWordScore.score.interRepetitionIntervalInDays,
);
});

it('should give an error if grade is invalid', async () => {
Expand Down
20 changes: 19 additions & 1 deletion express-backend/src/word/scoringAlgorithm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ScoreType } from './wordScore';
import { ScoreType, WordScoreType } from './wordScore';

export type GradeType = 0 | 1 | 2 | 3 | 4 | 5;

Expand Down Expand Up @@ -34,3 +34,21 @@ export default function getUpdatedWordScore(

return updatedScore;
}

export function getUpdatedSentenceScore(
grade: GradeType,
oldScore: ScoreType,
wordScores: WordScoreType[],
) {
const updatedScore = getUpdatedWordScore(grade, oldScore);
const lowestWordScore = wordScores.reduce((prev, curr) =>
prev.score.easinessFactor < curr.score.easinessFactor ? prev : curr,
);

return {
...updatedScore,
easinessFactor: lowestWordScore.score.easinessFactor,
interRepetitionIntervalInDays:
lowestWordScore.score.interRepetitionIntervalInDays,
};
}
2 changes: 1 addition & 1 deletion webapp/src/PlayPage/Play.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export default function Play({ sentences }: PlayProps) {
afterCheck={async (wasCorrect: boolean, idx: number, wordId: string) => {
const token = await getAccessTokenSilently();
await Promise.all([
updateWordScore(token, wordId, wasCorrect ? 5 : 0),
updateSentenceScore(
token,
sentences[idx].sentence.sentenceScoreId.toString(),
wasCorrect ? 5 : 0,
),
updateWordScore(token, wordId, wasCorrect ? 5 : 0),
]);
}}
onFinish={() => navigate('/lists')}
Expand Down

0 comments on commit f5a883a

Please sign in to comment.