mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
improved type in answer support from Bernhard Ibertsberger
This commit is contained in:
parent
5e1c98d516
commit
27a78bbde6
1 changed files with 28 additions and 22 deletions
|
@ -9,7 +9,7 @@ from anki.sound import playFromText, stripSounds
|
||||||
from anki.latex import renderLatex, stripLatex
|
from anki.latex import renderLatex, stripLatex
|
||||||
from anki.utils import stripHTML
|
from anki.utils import stripHTML
|
||||||
from anki.hooks import runHook
|
from anki.hooks import runHook
|
||||||
import types, time, re, os, urllib, sys
|
import types, time, re, os, urllib, sys, difflib
|
||||||
from ankiqt import ui
|
from ankiqt import ui
|
||||||
from ankiqt.ui.utils import mungeQA
|
from ankiqt.ui.utils import mungeQA
|
||||||
from anki.utils import fmtTimeSpan
|
from anki.utils import fmtTimeSpan
|
||||||
|
@ -132,6 +132,29 @@ class View(object):
|
||||||
if self.state != self.oldState and not nosound:
|
if self.state != self.oldState and not nosound:
|
||||||
playFromText(q)
|
playFromText(q)
|
||||||
|
|
||||||
|
def correct(self, a, b):
|
||||||
|
ret = "";
|
||||||
|
s = difflib.SequenceMatcher(None, b, a)
|
||||||
|
|
||||||
|
sz = self.main.currentCard.cardModel.answerFontSize
|
||||||
|
ok = "background: %s; color: #000; font-size: %dpx" % (
|
||||||
|
passedCharColour, sz)
|
||||||
|
bad = "background: %s; color: #000; font-size: %dpx;" % (
|
||||||
|
failedCharColour, sz)
|
||||||
|
|
||||||
|
for tag, i1, i2, j1, j2 in s.get_opcodes():
|
||||||
|
if tag == "equal":
|
||||||
|
ret += ("<span style='%s'>%s</span>" % (ok, b[i1:i2]))
|
||||||
|
elif tag == "replace":
|
||||||
|
ret += ("<span style='%s'>%s</span>"
|
||||||
|
% (bad, b[i1:i2] + (" " * ((j2 - j1) - (i2 - i1)))))
|
||||||
|
elif tag == "delete":
|
||||||
|
ret += ("<span style='%s'>%s</span>" % (bad, b[i1:i2]))
|
||||||
|
elif tag == "insert":
|
||||||
|
ret += ("<span style='%s'>%s</span>" % (bad, " " * (j2 - j1)))
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def drawAnswer(self):
|
def drawAnswer(self):
|
||||||
"Show the answer."
|
"Show the answer."
|
||||||
a = self.main.currentCard.htmlAnswer()
|
a = self.main.currentCard.htmlAnswer()
|
||||||
|
@ -143,27 +166,10 @@ class View(object):
|
||||||
cor = ""
|
cor = ""
|
||||||
if cor:
|
if cor:
|
||||||
given = unicode(self.main.typeAnswerField.text())
|
given = unicode(self.main.typeAnswerField.text())
|
||||||
res = []
|
res = self.correct(cor, given)
|
||||||
if len(given) < len(cor):
|
a = res + "<br>" + a
|
||||||
given += " " * (len(cor) - len(given))
|
self.write(self.center('<span id=answer />'
|
||||||
sz = self.main.currentCard.cardModel.answerFontSize
|
+ self.mungeQA(self.main.deck, a)))
|
||||||
ok = "background: %s; color: #000; font-size: %dpx" % (
|
|
||||||
passedCharColour, sz)
|
|
||||||
bad = "background: %s; color: #000; font-size: %dpx;" % (
|
|
||||||
failedCharColour, sz)
|
|
||||||
for (i, c) in enumerate(given):
|
|
||||||
try:
|
|
||||||
yes = c == cor[i]
|
|
||||||
except IndexError:
|
|
||||||
yes = False
|
|
||||||
if yes:
|
|
||||||
res.append(
|
|
||||||
"<span style='%s'>%s</span>" % (ok, c))
|
|
||||||
else:
|
|
||||||
res.append("<span style='%s'>%s</span>" % (bad, c))
|
|
||||||
a = "".join(res) + "<br>" + a
|
|
||||||
self.write(self.center('<span id=answer />' +
|
|
||||||
self.mungeQA(self.main.deck, a)))
|
|
||||||
if self.state != self.oldState:
|
if self.state != self.oldState:
|
||||||
playFromText(a)
|
playFromText(a)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue