Exemplo n.º 1
0
def todayscards():
    fields_text = ", ".join([c[0] for c in card.Card.fields])
    cursor = conn.execute('''SELECT id, alpha, beta, t, last FROM vocabulary 
    WHERE learning = true and last IS NOT NULL''')
    counter = 0
    ls = []
    for row in cursor:
        prior = (row[1], row[2], row[3])
        tnow = (today - datetime.strptime(row[4], "%Y-%m-%d").date()).days
        ls.append((row[0], ebisu.predictRecall(prior=prior, tnow=tnow)))
        counter += 1
    if counter < max_reviews:
        cursor = conn.execute(
            '''Select * FROM vocabulary 
        WHERE ID in ({});'''.format(', '.join(['?'] * counter)),
            [el[0] for el in ls])
    else:
        ls.sort(key=lambda x: x[1])
        cursor = conn.execute(
            '''Select * FROM vocabulary 
        WHERE ID in ({});'''.format(', '.join(['?'] * max_reviews)),
            [el[0] for el in ls[:max_reviews]])
    card_list = unpackcursor(cursor, [])
    cursor = conn.execute('''SELECT * FROM vocabulary 
        WHERE learning = false
        ORDER BY id''')
    # LIMIT (?);''', (max_new,))
    card_list = unpackcursor(cursor, card_list)
    shuffle(card_list)
    return card_list
Exemplo n.º 2
0
def get_items_to_learn(user_language, upper_recall_limit=0.5, n=-1):
    result = list()
    now = datetime.now()
    tmp = dict()
    if (user_language[1] is not None):
        words = db.fetchall("SELECT hid, model, last_date FROM spaced_repetition WHERE user=%s and language=%s",
                            (user_language[0], user_language[1]))
    else:
        words = db.fetchall("SELECT hid, model, last_date FROM spaced_repetition WHERE user=%s", (user_language[0],))
    for word in words:
        hid = word[0]
        model = tuple(json.loads(word[1]))
        lastTest = word[2]
        if lastTest is None:
            result.append(hid)
            continue
        lastTest = datetime.strptime(lastTest, "%Y-%m-%dT%H:%M:%S.%f")
        recall = ebisu.predictRecall(model, (now - lastTest) / oneHour, exact=True)
        if recall <= upper_recall_limit:
            tmp[hid] = recall
    recalls = list(tmp.values())
    recalls.sort()
    if n > 0:
        n = min(n, len(recalls))
    else:
        n = len(recalls)
    for r in recalls:
        for hid, recall in tmp.items():
            if len(result) >= n:
                break
            if recall == r:
                result.append(hid)
    return result
Exemplo n.º 3
0
def moveBeta(model: Tuple[float, float, float]):
    """Given a Beta model (2 parameters and time), find the closest Beta model at the halflife
  
  Works bmuch better than `moveGb1`"""
    th = estimate_half_life(model, 0.5)
    m = ebisu.predictRecall(model, th)
    v = ebisu.predictRecallVar(model, th)
    alpha1, beta1 = ebisu.alternate._meanVarToBeta(m, v)
    return (alpha1, beta1, th)
Exemplo n.º 4
0
    def predictAnalysis():
        models = []
        hlmodels = []
        for i in range(1000):
            a = np.random.rand() * 10 + 2
            b = np.random.rand() * 10 + 2
            t = np.random.rand() * 20
            model = [a, b, t]
            hlmodel = moveBeta(model)
            models.append(model)
            hlmodels.append(hlmodel)
        t = np.linspace(.1, 500, 1000)
        t0 = t[50]
        ps = [ebisu.predictRecall(model, t0) for model in hlmodels]
        arg = np.argsort(ps)
        pviat = [model[2] / t0 for model in hlmodels]
        arg2 = np.argsort(pviat)
        plt.figure()
        plt.scatter(ps, pviat, np.array([m[0] for m in hlmodels]))

        from mpl_toolkits.mplot3d import Axes3D
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.scatter(ps, pviat, np.array([m[0] for m in hlmodels]))
        ax.set_xlabel('Precall')
        ax.set_ylabel('t/tnow')
        ax.set_zlabel('alpha')

        deltav = np.logspace(-2, 2)
        deltav = np.linspace(.01, 10)
        alphav = np.linspace(1.2, 40)
        dmesh, amesh = np.meshgrid(deltav, alphav)
        from scipy.special import gammaln
        pmesh = gammaln(2 * amesh) - gammaln(amesh) + gammaln(
            amesh + dmesh) - gammaln(2 * amesh + dmesh)

        def myimshow(x, y, data):
            def extents(f):
                delta = f[1] - f[0]
                return [f[0] - delta / 2, f[-1] + delta / 2]

            ax = plt.imshow(data,
                            aspect='auto',
                            interpolation='none',
                            extent=extents(x) + extents(y),
                            origin='lower')

        plt.figure()
        myimshow(deltav, alphav, pmesh)
        plt.xlabel('delta')
        plt.ylabel('alpha')
        plt.colorbar()
Exemplo n.º 5
0
    def _recall_probability(self, card, exact=True):
        """
        Use the Ebisu algorithm to calculate the recall probability for a card.
        """
        if card.last_review is None:
            return 0

        model, time_from_last_review = self._get_card_model(card)

        recall_probability = ebisu.predictRecall(
            prior=model, tnow=time_from_last_review,
            exact=exact)  # Normalizes the output to a real probability.
        return recall_probability * 100
Exemplo n.º 6
0
def update_item(hid, result):
    time_passed = 0.1
    word = db.fetchone("SELECT model, last_date FROM spaced_repetition WHERE hid=%s", (hid,))
    if word[1] is not None:
        lastTest = datetime.strptime(word[1], "%Y-%m-%dT%H:%M:%S.%f")
        time_passed = (datetime.now() - lastTest) / oneHour
    model = tuple(json.loads(word[0]))
    recall = ebisu.predictRecall(model, time_passed, exact=True)
    print(str(hid), str(recall))
    new_model = ebisu.updateRecall(model, result, time_passed)
    print(hid, result)
    print(model)
    print(new_model)
    db.update_sr_item(hid, json.dumps(new_model), datetime.now().isoformat())
    return True
Exemplo n.º 7
0
def get_items_to_learn(user_language, upper_recall_limit=0.5, n=10):
    result = list()
    now = datetime.now()
    tmp = dict()
    if (user_language[1] is not None):
        words = db.fetchall(
            "SELECT hid, model, last_date FROM spaced_repetition WHERE user=%s and language=%s",
            (user_language[0], user_language[1]))
    else:
        words = db.fetchall(
            "SELECT hid, model, last_date FROM spaced_repetition WHERE user=%s",
            (user_language[0], ))
    for word in words:
        hid = word[0]
        model = tuple(json.loads(word[1]))
        lastTest = word[2]
        if lastTest is None:
            result.append(hid)
            continue
        lastTest = datetime.strptime(lastTest, "%Y-%m-%dT%H:%M:%S.%f")
        recall = ebisu.predictRecall(model, (now - lastTest) / oneHour,
                                     exact=True)
        if recall <= upper_recall_limit:
            tmp[hid] = recall
    recalls = list(tmp.values())
    recalls.sort()
    n = min(n, len(recalls))
    for r in recalls:
        for hid, recall in tmp.items():
            if len(result) >= n:
                break
            if recall == r:
                result.append(hid)
    return result


# date0 = datetime(2019, 9, 23, 23, 26, 0)
# now = date0 + timedelta(hours=11.1)
#

# database = [dict(factID=1, model=defaultModel, lastTest=date0),
#             dict(factID=2, model=defaultModel, lastTest=date0 + timedelta(hours=11))]
#
# print("On {},".format(now))
# for row in database:
#     recall = ebisu.predictRecall(row['model'],
#                                  (now - row['lastTest']) / oneHour,
#                                  exact=True)
#     print("Fact #{} probability of recall: {:0.1f}%".format(row['factID'], recall * 100))
#
#
#
#
# now = date0 + timedelta(hours=26.5)
# print("On {},".format(now))
# for row in database:
#     recall = ebisu.predictRecall(row['model'],
#                                  (now - row['lastTest']) / oneHour,
#                                  exact=True)
#     print("Fact #{} probability of recall: {:0.1f}%".format(row['factID'], recall * 100))
#
#
# row = database[0] # review FIRST question
#
# result = True
# newModel = ebisu.updateRecall(row['model'],
#                               1.0 if result else 0.0,
#                               (now - row['lastTest']) / oneHour)
# print('New model for fact #1:', newModel)
# row['model'] = newModel
# row['lastTest'] = now
#
# row = database[1] # review SECOND question
#
# result = False
# newModel = ebisu.updateRecall(row['model'],
#                               1.0 if result else 0.0,
#                               (now - row['lastTest']) / oneHour)
# print('New model for fact #2:', newModel)
# row['model'] = newModel
# row['lastTest'] = now
#
# for row in database:
#     meanHalflife = ebisu.modelToPercentileDecay(row['model'])
#     print("Fact #{} has half-life of ≈{:0.1f} hours".format(row['factID'], meanHalflife))
Exemplo n.º 8
0
 def trace(model):
     return np.vectorize(lambda t: ebisu.predictRecall(model, t))(t)