mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
mnemosyne importer improvements
- support cloze type - use proper names for front/back, etc - change <audio> to [sound:] - change mnemosyne-style latex references
This commit is contained in:
parent
161f8bcb31
commit
a4b7a93bcc
1 changed files with 49 additions and 4 deletions
|
@ -5,7 +5,7 @@
|
||||||
import time, re
|
import time, re
|
||||||
from anki.db import DB
|
from anki.db import DB
|
||||||
from anki.importing.noteimp import NoteImporter, ForeignNote, ForeignCard
|
from anki.importing.noteimp import NoteImporter, ForeignNote, ForeignCard
|
||||||
from anki.stdmodels import addBasicModel
|
from anki.stdmodels import addBasicModel, addClozeModel
|
||||||
from anki.lang import ngettext
|
from anki.lang import ngettext
|
||||||
|
|
||||||
class MnemosyneImporter(NoteImporter):
|
class MnemosyneImporter(NoteImporter):
|
||||||
|
@ -38,9 +38,10 @@ f._id=d._fact_id"""):
|
||||||
front = []
|
front = []
|
||||||
frontback = []
|
frontback = []
|
||||||
vocabulary = []
|
vocabulary = []
|
||||||
|
cloze = {}
|
||||||
for row in db.execute("""
|
for row in db.execute("""
|
||||||
select _fact_id, fact_view_id, tags, next_rep, last_rep, easiness,
|
select _fact_id, fact_view_id, tags, next_rep, last_rep, easiness,
|
||||||
acq_reps+ret_reps, lapses from cards"""):
|
acq_reps+ret_reps, lapses, card_type_id from cards"""):
|
||||||
# categorize note
|
# categorize note
|
||||||
note = notes[row[0]]
|
note = notes[row[0]]
|
||||||
if row[1].endswith(".1"):
|
if row[1].endswith(".1"):
|
||||||
|
@ -50,6 +51,8 @@ acq_reps+ret_reps, lapses from cards"""):
|
||||||
frontback.append(note)
|
frontback.append(note)
|
||||||
elif row[1].startswith("3.") or row[1].startswith("3::"):
|
elif row[1].startswith("3.") or row[1].startswith("3::"):
|
||||||
vocabulary.append(note)
|
vocabulary.append(note)
|
||||||
|
elif row[1].startswith("5.1"):
|
||||||
|
cloze[row[0]] = note
|
||||||
# merge tags into note
|
# merge tags into note
|
||||||
tags = row[2].replace(", ", "\x1f").replace(" ", "_")
|
tags = row[2].replace(", ", "\x1f").replace(" ", "_")
|
||||||
tags = tags.replace("\x1f", " ")
|
tags = tags.replace("\x1f", " ")
|
||||||
|
@ -83,11 +86,20 @@ acq_reps+ret_reps, lapses from cards"""):
|
||||||
total += self.total
|
total += self.total
|
||||||
self._addVocabulary(vocabulary)
|
self._addVocabulary(vocabulary)
|
||||||
self.total += total
|
self.total += total
|
||||||
|
self._addCloze(cloze)
|
||||||
|
self.total += total
|
||||||
self.log.append(ngettext("%d note imported.", "%d notes imported.", self.total) % self.total)
|
self.log.append(ngettext("%d note imported.", "%d notes imported.", self.total) % self.total)
|
||||||
|
|
||||||
def fields(self):
|
def fields(self):
|
||||||
return self._fields
|
return self._fields
|
||||||
|
|
||||||
|
def _mungeField(self, fld):
|
||||||
|
# latex differences
|
||||||
|
fld = re.sub("(?i)<(/?(\$|\$\$|latex))>", "[\\1]", fld)
|
||||||
|
# audio differences
|
||||||
|
fld = re.sub("<audio src=\"(.+?)\">(</audio>)?", "[sound:\\1]", fld)
|
||||||
|
return fld
|
||||||
|
|
||||||
def _addFronts(self, notes, model=None, fields=("f", "b")):
|
def _addFronts(self, notes, model=None, fields=("f", "b")):
|
||||||
data = []
|
data = []
|
||||||
for orig in notes:
|
for orig in notes:
|
||||||
|
@ -95,7 +107,8 @@ acq_reps+ret_reps, lapses from cards"""):
|
||||||
n = ForeignNote()
|
n = ForeignNote()
|
||||||
n.fields = []
|
n.fields = []
|
||||||
for f in fields:
|
for f in fields:
|
||||||
n.fields.append(orig.get(f, ''))
|
fld = self._mungeField(orig.get(f, ''))
|
||||||
|
n.fields.append(fld)
|
||||||
n.tags = orig['tags']
|
n.tags = orig['tags']
|
||||||
n.cards = orig.get('cards', {})
|
n.cards = orig.get('cards', {})
|
||||||
data.append(n)
|
data.append(n)
|
||||||
|
@ -140,3 +153,35 @@ acq_reps+ret_reps, lapses from cards"""):
|
||||||
mm.addTemplate(m, t)
|
mm.addTemplate(m, t)
|
||||||
mm.add(m)
|
mm.add(m)
|
||||||
self._addFronts(notes, m, fields=("f", "p_1", "m_1", "n"))
|
self._addFronts(notes, m, fields=("f", "p_1", "m_1", "n"))
|
||||||
|
|
||||||
|
def _addCloze(self, notes):
|
||||||
|
data = []
|
||||||
|
notes = notes.values()
|
||||||
|
for orig in notes:
|
||||||
|
# create a foreign note object
|
||||||
|
n = ForeignNote()
|
||||||
|
n.fields = []
|
||||||
|
fld = orig.get("text", "")
|
||||||
|
state = dict(n=1)
|
||||||
|
def repl(match):
|
||||||
|
# replace [...] with cloze refs
|
||||||
|
res = ("{{c%d::%s}}" % (state['n'], match.group(1)))
|
||||||
|
state['n'] += 1
|
||||||
|
return res
|
||||||
|
fld = re.sub("\[(.+)\]", repl, fld)
|
||||||
|
fld = self._mungeField(fld)
|
||||||
|
n.fields.append(fld)
|
||||||
|
n.fields.append("") # extra
|
||||||
|
n.tags = orig['tags']
|
||||||
|
n.cards = orig.get('cards', {})
|
||||||
|
data.append(n)
|
||||||
|
# add cloze model
|
||||||
|
model = addClozeModel(self.col)
|
||||||
|
model['name'] = "Mnemosyne-Cloze"
|
||||||
|
mm = self.col.models
|
||||||
|
mm.save(model)
|
||||||
|
mm.setCurrent(model)
|
||||||
|
self.model = model
|
||||||
|
self._fields = len(model['flds'])
|
||||||
|
self.initMapping()
|
||||||
|
self.importNotes(data)
|
||||||
|
|
Loading…
Reference in a new issue