def _addCloze(self, notes): data = [] notes = list(notes.values()) for orig in notes: # create a foreign note object n = ForeignNote() n.fields = [] fld = orig.get("text", "") fld = re.sub("\r?\n", "<br>", fld) state = dict(n=1) def repl(match): # pylint: disable=cell-var-from-loop # replace [...] with cloze refs res = "{{c%d::%s}}" % (state["n"], match.group(1)) state["n"] += 1 return res fld = re.sub(r"\[(.+?)\]", 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)
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", "") fld = re.sub("\r?\n", "<br>", fld) 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)
def _addCloze(self, notes): data = [] notes = list(notes.values()) for orig in notes: # create a foreign note object n = ForeignNote() n.fields = [] fld = orig.get("text", "") fld = re.sub("\r?\n", "<br>", fld) 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)
def addItemToCards(self,item): "This method actually do conversion" # new anki card note = ForeignNote() # clean Q and A note.fields.append(self._fudgeText(self._decode_htmlescapes(item.Question))) note.fields.append(self._fudgeText(self._decode_htmlescapes(item.Answer))) note.tags = [] # pre-process scheduling data # convert learning data if (not self.META.resetLearningData and int(item.Interval) >= 1 and getattr(item, "LastRepetition", None)): # migration of LearningData algorithm tLastrep = time.mktime(time.strptime(item.LastRepetition, '%d.%m.%Y')) tToday = time.time() card = ForeignCard() card.ivl = int(item.Interval) card.lapses = int(item.Lapses) card.reps = int(item.Repetitions) + int(item.Lapses) nextDue = tLastrep + (float(item.Interval) * 86400.0) remDays = int((nextDue - time.time())/86400) card.due = self.col.sched.today+remDays card.factor = int(self._afactor2efactor(float(item.AFactor.replace(',','.')))*1000) note.cards[0] = card # categories & tags # it's worth to have every theme (tree structure of sm collection) stored in tags, but sometimes not # you can deceide if you are going to tag all toppics or just that containing some pattern tTaggTitle = False for pattern in self.META.pathsToBeTagged: if item.lTitle is not None and pattern.lower() in " ".join(item.lTitle).lower(): tTaggTitle = True break if tTaggTitle or self.META.tagAllTopics: # normalize - remove diacritic punctuation from unicode chars to ascii item.lTitle = [ self._unicode2ascii(topic) for topic in item.lTitle] # Transfrom xyz / aaa / bbb / ccc on Title path to Tag xyzAaaBbbCcc # clean things like [999] or [111-2222] from title path, example: xyz / [1000-1200] zyx / xyz # clean whitespaces # set Capital letters for first char of the word tmp = list(set([ re.sub('(\[[0-9]+\])' , ' ' , i ).replace('_',' ') for i in item.lTitle ])) tmp = list(set([ re.sub('(\W)',' ', i ) for i in tmp ])) tmp = list(set([ re.sub( '^[0-9 ]+$','',i) for i in tmp ])) tmp = list(set([ capwords(i).replace(' ','') for i in tmp ])) tags = [ j[0].lower() + j[1:] for j in tmp if j.strip() != ''] note.tags += tags if self.META.tagMemorizedItems and int(item.Interval) >0: note.tags.append("Memorized") self.logger('Element tags\t- ' + repr(note.tags), level=3) self.notes.append(note)
def addItemToCards(self,item): "This method actually do conversion" # new anki card note = ForeignNote() # clean Q and A note.fields.append(self._fudgeText(self._decode_htmlescapes(item.Question))) note.fields.append(self._fudgeText(self._decode_htmlescapes(item.Answer))) note.tags = [] # pre-process scheduling data # convert learning data if (not self.META.resetLearningData and int(item.Interval) >= 1 and getattr(item, "LastRepetition", None)): # migration of LearningData algorithm tLastrep = time.mktime(time.strptime(item.LastRepetition, '%d.%m.%Y')) tToday = time.time() card = ForeignCard() card.ivl = int(item.Interval) card.lapses = int(item.Lapses) card.reps = int(item.Repetitions) + int(item.Lapses) nextDue = tLastrep + (float(item.Interval) * 86400.0) remDays = int((nextDue - time.time())/86400) card.due = self.col.sched.today+remDays card.factor = int(self._afactor2efactor(float(item.AFactor.replace(',','.')))*1000) note.cards[0] = card # categories & tags # it's worth to have every theme (tree structure of sm collection) stored in tags, but sometimes not # you can deceide if you are going to tag all toppics or just that containing some pattern tTaggTitle = False for pattern in self.META.pathsToBeTagged: if item.lTitle != None and pattern.lower() in " ".join(item.lTitle).lower(): tTaggTitle = True break if tTaggTitle or self.META.tagAllTopics: # normalize - remove diacritic punctuation from unicode chars to ascii item.lTitle = [ self._unicode2ascii(topic) for topic in item.lTitle] # Transfrom xyz / aaa / bbb / ccc on Title path to Tag xyzAaaBbbCcc # clean things like [999] or [111-2222] from title path, example: xyz / [1000-1200] zyx / xyz # clean whitespaces # set Capital letters for first char of the word tmp = list(set([ re.sub('(\[[0-9]+\])' , ' ' , i ).replace('_',' ') for i in item.lTitle ])) tmp = list(set([ re.sub('(\W)',' ', i ) for i in tmp ])) tmp = list(set([ re.sub( '^[0-9 ]+$','',i) for i in tmp ])) tmp = list(set([ capwords(i).replace(' ','') for i in tmp ])) tags = [ j[0].lower() + j[1:] for j in tmp if j.strip() != ''] note.tags += tags if self.META.tagMemorizedItems and int(item.Interval) >0: note.tags.append("Memorized") self.logger('Element tags\t- ' + repr(note.tags), level=3) self.notes.append(note)
def _addFronts(self, notes, model=None, fields=("f", "b")): data = [] for orig in notes: # create a foreign note object n = ForeignNote() n.fields = [] for f in fields: n.fields.append(orig.get(f, '')) n.tags = orig['tags'] n.cards = orig.get('cards', {}) data.append(n) # add a basic model if not model: model = addBasicModel(self.col) model['name'] = "Mnemosyne-FrontOnly" mm = self.col.models mm.save(model) mm.setCurrent(model) self.model = model self._fields = len(model['flds']) self.initMapping() # import self.importNotes(data)
def _addFronts(self, notes, model=None, fields=("f", "b")): data = [] for orig in notes: # create a foreign note object n = ForeignNote() n.fields = [] for f in fields: n.fields.append(orig.get(f, '')) n.tags = orig['tags'] n.cards = orig.get('cards', {}) data.append(n) # add a basic model if not model: model = addBasicModel(self.col) model['name'] = "Mnemosyne-FrontOnly" mm = self.col.models mm.save(model) mm.setCurrent(model) self.model = model self._fields = len(model['flds']) self.initMapping() # import self.importNotes(data)
def _addFronts(self, notes, model=None, fields=("f", "b")): data = [] for orig in notes: # create a foreign note object n = ForeignNote() n.fields = [] for f in fields: fld = self._mungeField(orig.get(f, "")) n.fields.append(fld) n.tags = orig["tags"] n.cards = orig.get("cards", {}) data.append(n) # add a basic model if not model: model = _legacy_add_basic_model(self.col) model["name"] = "Mnemosyne-FrontOnly" mm = self.col.models mm.save(model) mm.set_current(model) self.model = model self._fields = len(model["flds"]) self.initMapping() # import self.importNotes(data)