summaryrefslogtreecommitdiff
path: root/src/study/SpacedRepetitionModel.cpp
blob: e10299a6a8abdaa51b17cfaef773daf4cc9d2801 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "SpacedRepetitionModel.h"

#include <time.h>
#include <QtDebug>
#include <QtCore>

#include "../dictionary/Dictionary.h"
#include "../dictionary/Card.h"
#include "../dictionary/CardPack.h"
#include "../utils/IRandomGenerator.h"
#include "../utils/TimeProvider.h"

SpacedRepetitionModel::SpacedRepetitionModel(CardPack* aCardPack, IRandomGenerator* random):
    IStudyModel(aCardPack),
    curCard(NULL), settings(StudySettings::inst()), random(random)
    {
    srand(time(NULL));
    curCardNum = 0;
    connect(aCardPack, SIGNAL(cardsGenerated()), SLOT(updateStudyState()));
    updateStudyState();
    }

SpacedRepetitionModel::~SpacedRepetitionModel()
    {
    saveStudy();
	delete random;
    }

void SpacedRepetitionModel::saveStudy()
    {
    if(!cardPack)
        return;
    if(!cardPack->dictionary())
        return;
    cardPack->dictionary()->saveStudy();
    }

// Called after all cards are generated
void SpacedRepetitionModel::updateStudyState()
    {
    curCard = cardPack->getCard( cardPack->findLastReviewedCard() );
    curCardNum = 0;
    if( !cardPack->curCardName().isEmpty() )
        {
        prevCard = curCard;
        curCard = cardPack->getCard( cardPack->curCardName() );
        if( curCard )
            {
            answerTime.start();
            emit curCardUpdated();
            }
        else
            {
            curCard = prevCard;
            pickNextCardAndNotify();
            }
        }
    else
        pickNextCardAndNotify();
    }

void SpacedRepetitionModel::scheduleCard(int newGrade)
    {
    if(!curCard)
        return;
    saveStudyRecord(createNewStudyRecord(newGrade));
    curCardNum++;
    pickNextCardAndNotify();
    }

StudyRecord SpacedRepetitionModel::createNewStudyRecord(int newGrade)
{
    StudyRecord prevStudy = curCard->getStudyRecord();
    StudyRecord newStudy;

    newStudy.grade = newGrade;
    newStudy.level = getNewLevel(prevStudy, newGrade);
    newStudy.easiness = getNewEasiness(prevStudy, newGrade);
    newStudy.interval = getNextInterval(prevStudy, newStudy);
    newStudy.date = TimeProvider::get();
    newStudy.setRecallTime(curRecallTime / 1000.);
    newStudy.setAnswerTime(answerTime.elapsed() / 1000.);
    return newStudy;
}

int SpacedRepetitionModel::getNewLevel(const StudyRecord& prevStudy, int newGrade)
{
    int level = prevStudy.level;
    switch(newGrade)
    {
    case StudyRecord::Unknown:
    case StudyRecord::Incorrect:
        level = StudyRecord::ShortLearning;
        break;
    case StudyRecord::Difficult:
    case  StudyRecord::Good:
        if(prevStudy.isOneDayOld())
            level = StudyRecord::Repeating;
        else
            level++;
        break;
    case  StudyRecord::Easy:
        level += 2;
        break;
    }
    if(level > StudyRecord::LongLearning)
        level = StudyRecord::Repeating;
    return level;
}

double SpacedRepetitionModel::getNewEasiness(const StudyRecord& prevStudy,
        int newGrade)
{
    switch(prevStudy.level)
    {
    case StudyRecord::ShortLearning:
    case StudyRecord::LongLearning:
        if(prevStudy.isOneDayOld())
            return getChangeableEasiness(prevStudy, newGrade);
        else
            return prevStudy.easiness;
    case StudyRecord::Repeating:
        return getChangeableEasiness(prevStudy, newGrade);
    default:
        return prevStudy.easiness;
    }
}

double SpacedRepetitionModel::getChangeableEasiness(const StudyRecord& prevStudy,
        int newGrade) const
{
    double eas = prevStudy.easiness;
    switch(newGrade)
    {
    case StudyRecord::Difficult:
        eas += settings->difficultDelta;
        break;
    case StudyRecord::Easy:
        eas += settings->easyDelta;
        break;
    default:
        return prevStudy.easiness;
    }
    return limitEasiness(eas);
}

double SpacedRepetitionModel::limitEasiness(double eas) const
{
    if(eas < settings->minEasiness)
        eas = settings->minEasiness;
    else if(eas > settings->maxEasiness)
        eas = settings->maxEasiness;
    return eas;
}

double SpacedRepetitionModel::getNextInterval(const StudyRecord& prevStudy,
        const StudyRecord& newStudy)
{
    switch(newStudy.level)
    {
    case StudyRecord::ShortLearning:
        if(newStudy.grade == StudyRecord::Incorrect)
            return settings->incorrectInterval;
        else
            return settings->unknownInterval;
    case StudyRecord::LongLearning:
        return settings->learningInterval;
    case StudyRecord::Repeating:
        return getNextRepeatingInterval(prevStudy, newStudy);
    default:
        return 0;
    }
}

double SpacedRepetitionModel::getNextRepeatingInterval(const StudyRecord& prevStudy,
        const StudyRecord& newStudy)
{
    switch(prevStudy.level)
    {
    case StudyRecord::ShortLearning:
        return getNextRepeatingIntervalForShortLearning(prevStudy, newStudy);
    case StudyRecord::LongLearning:
        return getNextRepeatingIntervalForLongLearning(prevStudy, newStudy);
    case StudyRecord::Repeating:
        return getIncreasedInterval(prevStudy.interval, newStudy.easiness);
    default:
        return settings->nextDayInterval;
    }
}

double SpacedRepetitionModel::getIncreasedInterval(double prevInterval,
        double newEasiness)
{
    double interval = prevInterval * newEasiness;
    interval += interval *
            settings->schedRandomness * random->getInRange_11();
    return interval;
}

double SpacedRepetitionModel::getNextRepeatingIntervalForShortLearning(
        const StudyRecord& prevStudy,
        const StudyRecord& newStudy)
{
    if(prevStudy.isOneDayOld())
        return getIncreasedInterval(settings->nextDayInterval, newStudy.easiness);
    else
        return settings->nextDayInterval;
}

double SpacedRepetitionModel::getNextRepeatingIntervalForLongLearning(
        const StudyRecord& prevStudy,
        const StudyRecord& newStudy)
{
    if(prevStudy.isOneDayOld())
        return getIncreasedInterval(settings->nextDayInterval, newStudy.easiness);
    else if(newStudy.grade == StudyRecord::Easy)
        return settings->twoDaysInterval;
    else
        return settings->nextDayInterval;
}

void SpacedRepetitionModel::saveStudyRecord(const StudyRecord& newStudy)
{
    cardPack->addStudyRecord(curCard->getQuestion(), newStudy);
}

QList<int> SpacedRepetitionModel::getAvailableGrades() const
{
    if(!curCard)
        return {};
    StudyRecord study = curCard->getStudyRecord();
    switch(study.level)
    {
    case StudyRecord::New:
        return {4, 5};
    case StudyRecord::ShortLearning:
    case StudyRecord::LongLearning:
        if(study.isOneDayOld())
            return {1, 2, 3, 4, 5};
        else
            return {1, 2, 4, 5};
    case StudyRecord::Repeating:
        return {1, 2, 3, 4, 5};
    default:
        return {};
    }
}

bool SpacedRepetitionModel::isNew() const
{
    return curCard->getStudyRecord().level == StudyRecord::New;
}

void SpacedRepetitionModel::pickNextCardAndNotify()
    {
    answerTime.start();
    pickNextCard();
    if(curCard)
        cardPack->setCurCard(curCard->getQuestion());
    else
        cardPack->setCurCard("");
    // Notify the study window to show the selected card.
    emit nextCardSelected();
    }

void SpacedRepetitionModel::pickNextCard()
{
    prevCard = curCard;
    curCard = NULL;
    pickActiveCard() ||
        pickNewCard() ||
        pickLearningCard();
}

bool SpacedRepetitionModel::mustRandomPickScheduledCard() const
{
    return random->getInRange_01() > settings->newCardsShare;
}

bool SpacedRepetitionModel::reachedNewCardsDayLimit() const
{
    return cardPack->getTodayNewCardsNum() >= settings->newCardsDayLimit;
}

bool SpacedRepetitionModel::pickActiveCard()
{
    QStringList activeCards = cardPack->getActiveCards();
    if(activeCards.isEmpty())
        return false;
    if(pickPriorityActiveCard())
        return true;
    if(!mustPickScheduledCard())
        return false;
    curCard = cardPack->getCard(getRandomStr(activeCards));
    return true;
}

bool SpacedRepetitionModel::pickPriorityActiveCard()
{
    QStringList priorityCards = cardPack->getPriorityActiveCards();
    if(priorityCards.isEmpty())
        return false;
    QStringList smallestIntervals = cardPack->getSmallestIntervalCards(priorityCards);
    curCard = cardPack->getCard(getRandomStr(smallestIntervals));
    return true;
}

bool SpacedRepetitionModel::mustPickScheduledCard()
{
    bool noNewCards = cardPack->getNewCards().isEmpty();
    if(noNewCards || reachedNewCardsDayLimit() ||
        tooManyScheduledCards())
        return true;
    else
        return mustRandomPickScheduledCard();
}

bool SpacedRepetitionModel::tooManyScheduledCards() const
{
    return cardPack->countScheduledForTodayCards() >= settings->limitForAddingNewCards;
}

bool SpacedRepetitionModel::pickNewCard()
    {
    if(reachedNewCardsDayLimit())
        return false;
    QStringList newCards = cardPack->getNewCards();
    if(newCards.isEmpty())
        return false;
    QString cardName;
    if(settings->showRandomly)
        cardName = getRandomStr(newCards);
    else
        cardName = newCards.first();
    curCard = cardPack->getCard(cardName);
    return true;
    }

bool SpacedRepetitionModel::pickLearningCard()
{
    QStringList learningCards = cardPack->getLearningCards();
    if(learningCards.isEmpty())
        return false;
    QStringList smallestIntervals = cardPack->getSmallestIntervalCards(learningCards);
    curCard = cardPack->getCard(getRandomStr(smallestIntervals));
    return true;
}

QString SpacedRepetitionModel::getRandomStr(const QStringList& list) const
{
    return list.at(random->getRand(list.size()));
}

/// New cards inside the reviewed ones still today
int SpacedRepetitionModel::estimatedNewReviewedCardsToday() const
    {
    if(tooManyScheduledCards())
        return 0;
    int scheduledToday = cardPack->countScheduledForTodayCards();
    int newRev = 0;
    if( scheduledToday > 0 )
        {
        float newShare = settings->newCardsShare;
        newRev = qRound( scheduledToday * newShare );
        }
    else
        return 0;
    
    // Check for remained new cards in pack
    int newCardsNum = cardPack->getNewCards().size();
    if( newRev > newCardsNum )
        newRev = newCardsNum;
    
    // Check for new cards day limit
    int newCardsDayLimit = settings->newCardsDayLimit;
    int todayReviewedNewCards = cardPack->getTodayNewCardsNum();
    int remainedNewCardsLimit = newCardsDayLimit - todayReviewedNewCards;
    if( newRev > remainedNewCardsLimit )
        newRev = remainedNewCardsLimit;
    
    return newRev;
    }

/** Calculates number of candidate cards to be shown in the current session.
  */
int SpacedRepetitionModel::countTodayRemainingCards() const
    {
    int timeTriggered = cardPack->getActiveCards().size();
    return timeTriggered + estimatedNewReviewedCardsToday();
    }