Instantly share code, notes, and snippets.
Created
June 17, 2018 12:15
-
Save kelciour/2e4cba32edb2155c044bf34ea96d4ba7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: Python ; coding: utf-8 -*- | |
# ' Select Answer Buttons Automatically | |
# https://ankiweb.net/shared/info/932960746 | |
# -- tested with Anki 2.0.36 .. 2.0.51 under Windows 7 SP1 | |
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html | |
# Copyright (c) 2018 Dmitry Mikheev, http://finpapa.ucoz.net/ | |
# No support. Use it AS IS on your own risk. | |
""" | |
# Inspired by | |
# Select_Answer_Buttons_Automatically_For_Cloze_Type | |
# https://ankiweb.net/shared/info/1385432957 | |
# Select Buttons Automatically If Correct Answer, Wrong Answer or Nothing | |
# https://ankiweb.net/shared/info/2074758752 | |
This addon will work with the Anki cards with any number of buttons: | |
2, 3 or 4. | |
If you create a card with <code>{{type:</code> prefix | |
to input an answer from keyboard, | |
addon will compare the typed answer with the given correct answer | |
and select <b>Good</b> as default button if your typing was correct | |
or select <b>Again</b> as default button if your was incorrect. | |
If no answer was given | |
(empty input field, you just pressed <code>Enter</code> key), | |
the default <i>Good</i> button will be selected. | |
This add-on does not destroy the <i>Good</i> selection by default | |
if your card has no <code>#typeans</code> field at all. | |
There is a pair of earlier Add-ons here, but I could not get them to work | |
so I created this one, utilizing some of the previous work and comments. | |
It ignores html markup (e.g. color) in the deposited correct answer | |
so it works better than a previous addon (see below "Inspired by.."). | |
This one also ignores the punctuation marks when comparing, | |
it compares only English and Russian letters and spaces between them. | |
""" | |
# Addon "Select_Buttons_Automatically_For_Cloze_Type_Cards.py" | |
# Amendments by Stefan Zeh to the addon mentioned below. | |
# 2016-12-07 | |
# If you have the "type" option in a cloze deletion card, | |
# this addon will automatically select the "good" button upon hitting <return> | |
# if your typedAnswer has been correct (otherwise "bad" is selected). | |
# It ignores html markup (e.g. color) in the deposited correct answer | |
# so it works better than a previous addon (see below "Inspired by.."). | |
# [I marked feminin answers in red and maskuline answers in blue | |
# (e.g. "une table" in red, "un copain" in blue) and the | |
# addon below did not work any more. | |
# This one is ok as it only compares plain text to select the button | |
# (or "ease level").] | |
from __future__ import unicode_literals | |
from aqt import mw | |
from aqt.reviewer import Reviewer | |
# from aqt.deckconf import DeckConf | |
# from aqt.forms import dconf | |
# from anki.hooks import addHook, wrap | |
from anki.utils import stripHTML | |
from PyQt4 import QtGui | |
# from threading import Timer | |
import re | |
def _defaultEase(self): | |
"from aqt/reviewer.py" | |
if self.mw.col.sched.answerButtons(self.card) == 4: | |
return 3 | |
else: | |
return 2 | |
def MydefaultEase(self): | |
'Compare typedAnswer with deposited answer (typeCorrect)' | |
if self.typeCorrect is None: | |
# non cloze:type cards will have this variable empty | |
if self.mw.col.sched.answerButtons(self.card) == 4: | |
return 3 | |
else: | |
return 2 | |
# quit the function with None and omit stripHTML | |
# as it would issue error | |
else: | |
# Strip html (e.g. color) of typeCorrect | |
# in order to only compare plain text. | |
# Otherwise a colored word inside the {{cloze:MasculineNounsInBlue}} | |
# would be always interpreted as "bad" | |
org = stripHTML(self.mw.col.media.strip(self.typeCorrect)) | |
org = org.strip().lower() | |
org = org.replace('ё', 'е') | |
inp = self.typedAnswer.strip().lower() | |
inp = inp.replace('ё', 'е') | |
if inp == org: | |
if self.mw.col.sched.answerButtons(self.card) == 4: | |
return 3 | |
# good in case of 4 buttons (bad / hard / good / easy) | |
elif self.mw.col.sched.answerButtons(self.card) == 3: | |
return 2 | |
# good in the standard case of 3 buttons (bad / good / easy) | |
elif self.mw.col.sched.answerButtons(self.card) == 2: | |
return 2 | |
# good in the standard case of 2 choices (bad / good) | |
else: | |
return 1 | |
# bad = repeat in <1 min | |
return 1 # return "bad" if nothing of below is true | |
Reviewer._defaultEase = MydefaultEase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment