py/gadict_srs_anki.py
changeset 678 28e04408c0c0
parent 677 e330e7f195b7
child 686 a9e3b6050544
equal deleted inserted replaced
677:e330e7f195b7 678:28e04408c0c0
   221         model['css'] = MODEL_CSS
   221         model['css'] = MODEL_CSS
   222 
   222 
   223         collection.models.addField(model, collection.models.newField('Front'))
   223         collection.models.addField(model, collection.models.newField('Front'))
   224         collection.models.addField(model, collection.models.newField('Back'))
   224         collection.models.addField(model, collection.models.newField('Back'))
   225         collection.models.addField(model, collection.models.newField('SafeBack'))
   225         collection.models.addField(model, collection.models.newField('SafeBack'))
       
   226         collection.models.addField(model, collection.models.newField('Freq'))
   226 
   227 
   227         tmpl = collection.models.newTemplate('Front -> Back')
   228         tmpl = collection.models.newTemplate('Front -> Back')
   228         tmpl['qfmt'] = '<div class="front">{{Front}}</div>'
   229         tmpl['qfmt'] = '<div class="front">{{Front}}</div><div class="freq">{{Freq}}</div>'
   229         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Back}}</div>'
   230         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Back}}</div>'
   230         collection.models.addTemplate(model, tmpl)
   231         collection.models.addTemplate(model, tmpl)
   231 
   232 
   232         tmpl = collection.models.newTemplate('SafeBack -> Front')
   233         tmpl = collection.models.newTemplate('SafeBack -> Front')
   233         tmpl['qfmt'] = '<div class="safe-back">{{SafeBack}}</div>'
   234         tmpl['qfmt'] = '<div class="safe-back">{{SafeBack}}</div>'
   234         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="front">{{Front}}</div>'
   235         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="front">{{Front}}</div><div class="freq">{{Freq}}</div>'
   235         collection.models.addTemplate(model, tmpl)
   236         collection.models.addTemplate(model, tmpl)
   236 
   237 
   237         # Equivalent of ``collection.models.add(model)`` without setting
   238         # Equivalent of ``collection.models.add(model)`` without setting
   238         # auto-generated ID.
   239         # auto-generated ID.
   239         # Increment +1 is for keeping model['id'] unique from previous v0.9 release.
   240         # Increment +1 is for keeping model['id'] unique from previous v0.9 release.
   257         collection.models.addField(model, collection.models.newField('V3'))
   258         collection.models.addField(model, collection.models.newField('V3'))
   258         collection.models.addField(model, collection.models.newField('V2alt'))
   259         collection.models.addField(model, collection.models.newField('V2alt'))
   259         collection.models.addField(model, collection.models.newField('V3alt'))
   260         collection.models.addField(model, collection.models.newField('V3alt'))
   260         collection.models.addField(model, collection.models.newField('Front'))
   261         collection.models.addField(model, collection.models.newField('Front'))
   261         collection.models.addField(model, collection.models.newField('Back'))
   262         collection.models.addField(model, collection.models.newField('Back'))
       
   263         collection.models.addField(model, collection.models.newField('Freq'))
   262 
   264 
   263         question = u"<div class='ask'>Find irregular verb:</div>"
   265         question = u"<div class='ask'>Find irregular verb:</div>"
   264 
   266 
   265         tmpl = collection.models.newTemplate('V1 -> Back')
   267         tmpl = collection.models.newTemplate('V1 -> Back')
   266         tmpl['qfmt'] = question + '<div class="front">{{V1}}</div>'
   268         tmpl['qfmt'] = question + '<div class="front">{{V1}}</div>'
   267         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   269         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   268         collection.models.addTemplate(model, tmpl)
   270         collection.models.addTemplate(model, tmpl)
   269 
   271 
   270         tmpl = collection.models.newTemplate('V2 -> Back')
   272         tmpl = collection.models.newTemplate('V2 -> Back')
   271         tmpl['qfmt'] = question + '<div class="front">{{V2}}</div>'
   273         tmpl['qfmt'] = question + '<div class="front">{{V2}}</div>'
   272         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   274         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   273         collection.models.addTemplate(model, tmpl)
   275         collection.models.addTemplate(model, tmpl)
   274 
   276 
   275         tmpl = collection.models.newTemplate('V3 -> Back')
   277         tmpl = collection.models.newTemplate('V3 -> Back')
   276         tmpl['qfmt'] = question + '<div class="front">{{V3}}</div>'
   278         tmpl['qfmt'] = question + '<div class="front">{{V3}}</div>'
   277         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   279         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   278         collection.models.addTemplate(model, tmpl)
   280         collection.models.addTemplate(model, tmpl)
   279 
   281 
   280         tmpl = collection.models.newTemplate('V2alt -> Back')
   282         tmpl = collection.models.newTemplate('V2alt -> Back')
   281         tmpl['qfmt'] = question + '<div class="front">{{V2alt}}</div>'
   283         tmpl['qfmt'] = question + '<div class="front">{{V2alt}}</div>'
   282         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   284         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   283         collection.models.addTemplate(model, tmpl)
   285         collection.models.addTemplate(model, tmpl)
   284 
   286 
   285         tmpl = collection.models.newTemplate('V3alt -> Back')
   287         tmpl = collection.models.newTemplate('V3alt -> Back')
   286         tmpl['qfmt'] = question + '<div class="front">{{V3alt}}</div>'
   288         tmpl['qfmt'] = question + '<div class="front">{{V3alt}}</div>'
   287         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   289         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   288         collection.models.addTemplate(model, tmpl)
   290         collection.models.addTemplate(model, tmpl)
   289 
   291 
   290         model['id'] = MODEL_ID + 2          # Keep model['id'] unique.
   292         model['id'] = MODEL_ID + 2          # Keep model['id'] unique.
   291         collection.models.update(model)
   293         collection.models.update(model)
   292         collection.models.save(model)
   294         collection.models.save(model)
   300 
   302 
   301         collection.models.addField(model, collection.models.newField('Singular'))
   303         collection.models.addField(model, collection.models.newField('Singular'))
   302         collection.models.addField(model, collection.models.newField('Plural'))
   304         collection.models.addField(model, collection.models.newField('Plural'))
   303         collection.models.addField(model, collection.models.newField('Front'))
   305         collection.models.addField(model, collection.models.newField('Front'))
   304         collection.models.addField(model, collection.models.newField('Back'))
   306         collection.models.addField(model, collection.models.newField('Back'))
       
   307         collection.models.addField(model, collection.models.newField('Freq'))
   305 
   308 
   306         question = u"<div class='ask'>Find singular/plural form:</div>"
   309         question = u"<div class='ask'>Find singular/plural form:</div>"
   307 
   310 
   308         tmpl = collection.models.newTemplate('Singular -> Back')
   311         tmpl = collection.models.newTemplate('Singular -> Back')
   309         tmpl['qfmt'] = question + '<div class="front">{{Singular}}</div>'
   312         tmpl['qfmt'] = question + '<div class="front">{{Singular}}</div>'
   310         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   313         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   311         collection.models.addTemplate(model, tmpl)
   314         collection.models.addTemplate(model, tmpl)
   312 
   315 
   313         tmpl = collection.models.newTemplate('Plural -> Back')
   316         tmpl = collection.models.newTemplate('Plural -> Back')
   314         tmpl['qfmt'] = question + '<div class="front">{{Plural}}</div>'
   317         tmpl['qfmt'] = question + '<div class="front">{{Plural}}</div>'
   315         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
   318         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
   316         collection.models.addTemplate(model, tmpl)
   319         collection.models.addTemplate(model, tmpl)
   317 
   320 
   318         model['id'] = MODEL_ID + 3          # Keep model['id'] unique.
   321         model['id'] = MODEL_ID + 3          # Keep model['id'] unique.
   319         collection.models.update(model)
   322         collection.models.update(model)
   320         collection.models.save(model)
   323         collection.models.save(model)
   326         :nodetype  used to generate different notes from same headword
   329         :nodetype  used to generate different notes from same headword
   327         """
   330         """
   328         h = hashlib.md5(":".join((self.name, nodetype, headword)))
   331         h = hashlib.md5(":".join((self.name, nodetype, headword)))
   329         return h.hexdigest()
   332         return h.hexdigest()
   330 
   333 
   331     def add_note(self, headword, front, back, safeback, tags = None):
   334     def add_note(self, headword, front, back, safeback, freq, tags = None):
   332         note = anki.notes.Note(self.collection, self.model)
   335         note = anki.notes.Note(self.collection, self.model)
   333         note['Front'] = front
   336         note['Front'] = front
   334         note['Back'] = back
   337         note['Back'] = back
   335         note['SafeBack'] = safeback
   338         note['SafeBack'] = safeback
       
   339         note['Freq'] = freq
   336         note_add_tags(note, tags)
   340         note_add_tags(note, tags)
   337         note.guid = self.guid("front/back", headword)
   341         note.guid = self.guid("front/back", headword)
   338         self.collection.addNote(note)
   342         self.collection.addNote(note)
   339 
   343 
   340     def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, tags = None):
   344     def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, freq, tags = None):
   341         note = anki.notes.Note(self.collection, self.model_irr)
   345         note = anki.notes.Note(self.collection, self.model_irr)
   342         note['V1'] = v1
   346         note['V1'] = v1
   343         note['V2'] = v2
   347         note['V2'] = v2
   344         note['V3'] = v3
   348         note['V3'] = v3
   345         note['V2alt'] = v2alt
   349         note['V2alt'] = v2alt
   346         note['V3alt'] = v3alt
   350         note['V3alt'] = v3alt
   347         note['Front'] = front
   351         note['Front'] = front
   348         note['Back'] = back
   352         note['Back'] = back
       
   353         note['Freq'] = freq
   349         note_add_tags(note, tags)
   354         note_add_tags(note, tags)
   350         note.guid = self.guid("irregular verb", headword)
   355         note.guid = self.guid("irregular verb", headword)
   351         self.collection.addNote(note)
   356         self.collection.addNote(note)
   352 
   357 
   353     def add_note_pl(self, headword, singular, plural, front, back, tags = None):
   358     def add_note_pl(self, headword, singular, plural, front, back, freq, tags = None):
   354         note = anki.notes.Note(self.collection, self.model_pl)
   359         note = anki.notes.Note(self.collection, self.model_pl)
   355         note['Singular'] = singular
   360         note['Singular'] = singular
   356         note['Plural'] = plural
   361         note['Plural'] = plural
   357         note['Front'] = front
   362         note['Front'] = front
   358         note['Back'] = back
   363         note['Back'] = back
       
   364         note['Freq'] = freq
   359         note_add_tags(note, tags)
   365         note_add_tags(note, tags)
   360         note.guid = self.guid("singular/plural noun", headword)
   366         note.guid = self.guid("singular/plural noun", headword)
   361         self.collection.addNote(note)
   367         self.collection.addNote(note)
   362 
   368 
   363     def export(self, fname):
   369     def export(self, fname):
   445             continue
   451             continue
   446         freqtags = []
   452         freqtags = []
   447         for (freqtag, freqset) in FREQ_SOURCES:
   453         for (freqtag, freqset) in FREQ_SOURCES:
   448             if identity in freqset:
   454             if identity in freqset:
   449                 freqtags.append(freqtag)
   455                 freqtags.append(freqtag)
   450         freqmsg = ""
   456         freqmsg = " "
   451         if len(freqtags) > 0:
   457         if len(freqtags) > 0:
   452             freqmsg = ",".join(freqtags)
   458             freqmsg = ",".join(freqtags)
   453             freqmsg = "<div class='freq'>{:s}</div>".format(freqmsg)
       
   454         buf = []
   459         buf = []
   455         v1, v2, v3, v2alt, v3alt = (None, None, None, None, None)
   460         v1, v2, v3, v2alt, v3alt = (None, None, None, None, None)
   456         singular, plural = (None, None)
   461         singular, plural = (None, None)
   457         for hw in headwords:
   462         for hw in headwords:
   458             buf.append("<div clsas='headword'>")
   463             buf.append("<div clsas='headword'>")
   493         direct_to = "".join(buf)
   498         direct_to = "".join(buf)
   494         buf = []
   499         buf = []
   495         for sense in translations:
   500         for sense in translations:
   496             write_sense(buf, sense, with_examples = False)
   501             write_sense(buf, sense, with_examples = False)
   497         reverse_from = "".join(buf)         # without examples!!
   502         reverse_from = "".join(buf)         # without examples!!
   498         builder.add_note(identity, direct_from, direct_to, reverse_from)
   503         builder.add_note(identity, direct_from, direct_to, reverse_from, freqmsg, freqtags)
   499         if v1 and v2 and v3 and RICH_MODE:
   504         if v1 and v2 and v3 and RICH_MODE:
   500             riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
   505             riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
   501             riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
   506             riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
   502             riddle3 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3[0], v3[1])
   507             riddle3 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3[0], v3[1])
   503             if v2alt:
   508             if v2alt:
   506                 riddle2alt = ""
   511                 riddle2alt = ""
   507             if v3alt:
   512             if v3alt:
   508                 riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v3alt[0], v3alt[1])
   513                 riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v3alt[0], v3alt[1])
   509             else:
   514             else:
   510                 riddle3alt = ""
   515                 riddle3alt = ""
   511             builder.add_note_irr(identity, riddle1 + freqmsg, riddle2 + freqmsg, riddle2alt, riddle3 + freqmsg, riddle3alt, direct_from, direct_to)
   516             builder.add_note_irr(identity, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
   512         if singular and plural and RICH_MODE:
   517         if singular and plural and RICH_MODE:
   513             riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
   518             riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
   514             riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
   519             riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
   515             builder.add_note_pl(identity, riddle_s + freqmsg, riddle_pl + freqmsg, direct_from, direct_to)
   520             builder.add_note_pl(identity, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
   516 
   521 
   517     builder.export(FONAME)
   522     builder.export(FONAME)
   518 finally:
   523 finally:
   519     builder.close()
   524     builder.close()
   520     shutil.rmtree(TMPDIR, ignore_errors=True)
   525     shutil.rmtree(TMPDIR, ignore_errors=True)