py/gadict_srs_anki.py
changeset 739 4c70daa48492
parent 736 e7f0c03887e1
child 750 b68dfe0c2804
equal deleted inserted replaced
738:206fd730aec4 739:4c70daa48492
   326         model['id'] = model_id + 3          # Keep model['id'] unique.
   326         model['id'] = model_id + 3          # Keep model['id'] unique.
   327         collection.models.update(model)
   327         collection.models.update(model)
   328         collection.models.save(model)
   328         collection.models.save(model)
   329         self.model_pl = model
   329         self.model_pl = model
   330 
   330 
   331     def guid(self, nodetype, headword):
   331     def guid(self, nodetype, headword, unambiguous):
   332         """
   332         """
   333         :nodetype  used to generate different notes from same headword
   333         :nodetype     used to generate different notes from same headword
       
   334         :unambiguous  used if several subsequent article with same headword (and different pronunciation)
   334         """
   335         """
   335         h = hashlib.md5(":".join((self.name, nodetype, headword)))
   336         if unambiguous > 0:
       
   337             h = hashlib.md5(":".join((self.name, nodetype, headword, str(unambiguous))))
       
   338         else:
       
   339             h = hashlib.md5(":".join((self.name, nodetype, headword)))
   336         return h.hexdigest()
   340         return h.hexdigest()
   337 
   341 
   338     def add_note(self, headword, front, back, safeback, freq, tags=None):
   342     def add_note(self, headword, unambiguous, front, back, safeback, freq="", tags=None):
   339         note = anki.notes.Note(self.collection, self.model)
   343         note = anki.notes.Note(self.collection, self.model)
   340         note['Front'] = front
   344         note['Front'] = front
   341         note['Back'] = back
   345         note['Back'] = back
   342         note['SafeBack'] = safeback
   346         note['SafeBack'] = safeback
   343         note['Freq'] = freq
   347         note['Freq'] = freq
   344         note_add_tags(note, tags)
   348         note_add_tags(note, tags)
   345         note.guid = self.guid("front/back", headword)
   349         note.guid = self.guid("front/back", headword, unambiguous)
   346         self.collection.addNote(note)
   350         self.collection.addNote(note)
   347 
   351 
   348     def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, freq, tags=None):
   352     def add_note_irr(self, headword, unambiguous, v1, v2, v2alt, v3, v3alt, front, back, freq, tags=None):
   349         note = anki.notes.Note(self.collection, self.model_irr)
   353         note = anki.notes.Note(self.collection, self.model_irr)
   350         note['V1'] = v1
   354         note['V1'] = v1
   351         note['V2'] = v2
   355         note['V2'] = v2
   352         note['V3'] = v3
   356         note['V3'] = v3
   353         note['V2alt'] = v2alt
   357         note['V2alt'] = v2alt
   354         note['V3alt'] = v3alt
   358         note['V3alt'] = v3alt
   355         note['Front'] = front
   359         note['Front'] = front
   356         note['Back'] = back
   360         note['Back'] = back
   357         note['Freq'] = freq
   361         note['Freq'] = freq
   358         note_add_tags(note, tags)
   362         note_add_tags(note, tags)
   359         note.guid = self.guid("irregular verb", headword)
   363         note.guid = self.guid("irregular verb", headword, unambiguous)
   360         self.collection.addNote(note)
   364         self.collection.addNote(note)
   361 
   365 
   362     def add_note_pl(self, headword, singular, plural, front, back, freq, tags=None):
   366     def add_note_pl(self, headword, unambiguous, singular, plural, front, back, freq, tags=None):
   363         note = anki.notes.Note(self.collection, self.model_pl)
   367         note = anki.notes.Note(self.collection, self.model_pl)
   364         note['Singular'] = singular
   368         note['Singular'] = singular
   365         note['Plural'] = plural
   369         note['Plural'] = plural
   366         note['Front'] = front
   370         note['Front'] = front
   367         note['Back'] = back
   371         note['Back'] = back
   368         note['Freq'] = freq
   372         note['Freq'] = freq
   369         note_add_tags(note, tags)
   373         note_add_tags(note, tags)
   370         note.guid = self.guid("singular/plural noun", headword)
   374         note.guid = self.guid("singular/plural noun", headword, unambiguous)
   371         self.collection.addNote(note)
   375         self.collection.addNote(note)
   372 
   376 
   373     def export(self, fname):
   377     def export(self, fname):
   374         export = AnkiPackageExporter(self.collection)
   378         export = AnkiPackageExporter(self.collection)
   375         export.exportInto(fname)
   379         export.exportInto(fname)
   449     BUILDER = AnkiDbBuilder(TMPDIR, NAME)
   453     BUILDER = AnkiDbBuilder(TMPDIR, NAME)
   450 
   454 
   451     for identity in FDEL or []:
   455     for identity in FDEL or []:
   452         identity = identity.strip()
   456         identity = identity.strip()
   453         warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
   457         warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
   454         BUILDER.add_note(identity, warnmsg, warnmsg, warnmsg, "del")
   458         BUILDER.add_note(identity, 0, warnmsg, warnmsg, warnmsg, tags="del")
   455         BUILDER.add_note_irr(identity, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, "del")
   459         BUILDER.add_note_irr(identity, 0, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, tags="del")
   456         BUILDER.add_note_pl(identity, warnmsg, warnmsg, warnmsg, warnmsg, "del")
   460         BUILDER.add_note_pl(identity, 0, warnmsg, warnmsg, warnmsg, warnmsg, tags="del")
   457 
   461 
       
   462     prev_identity = None
       
   463     unambiguous = 0
   458     for (headwords, translations) in DOM[1:]:
   464     for (headwords, translations) in DOM[1:]:
   459         identity = headwords[0].headword
   465         identity = headwords[0].headword
   460         if 'rare' in headwords[0].attrs:
   466         if 'rare' in headwords[0].attrs:
   461             continue
   467             continue
   462         freqtags = []
   468         freqtags = []
   463         for (freqtag, freqset) in FREQ_SOURCES:
   469         for (freqtag, freqset) in FREQ_SOURCES:
   464             if identity in freqset:
   470             if identity in freqset:
   465                 freqtags.append(freqtag)
   471                 freqtags.append(freqtag)
       
   472         if prev_identity == identity:
       
   473             unambiguous += 1
       
   474         else:
       
   475             prev_identity = identity
       
   476             unambiguous = 0
   466         freqmsg = " "
   477         freqmsg = " "
   467         if len(freqtags) > 0:
   478         if len(freqtags) > 0:
   468             freqmsg = ",".join(freqtags)
   479             freqmsg = ",".join(freqtags)
   469         buf = []
   480         buf = []
   470         v1, v2, v3, v2alt, v3alt = (None, None, None, None, None)
   481         v1, v2, v3, v2alt, v3alt = (None, None, None, None, None)
   508         direct_to = "".join(buf)
   519         direct_to = "".join(buf)
   509         buf = []
   520         buf = []
   510         for sense in translations:
   521         for sense in translations:
   511             write_sense(buf, sense, with_examples=False)
   522             write_sense(buf, sense, with_examples=False)
   512         reverse_from = "".join(buf)         # without examples!!
   523         reverse_from = "".join(buf)         # without examples!!
   513         BUILDER.add_note(identity, direct_from, direct_to, reverse_from, freqmsg, freqtags)
   524         BUILDER.add_note(identity, unambiguous, direct_from, direct_to, reverse_from, freqmsg)
   514         if v1 and v2 and v3 and RICH_MODE:
   525         if v1 and v2 and v3 and RICH_MODE:
   515             riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
   526             riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
   516             riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
   527             riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
   517             riddle3 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3[0], v3[1])
   528             riddle3 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3[0], v3[1])
   518             if v2alt:
   529             if v2alt:
   521                 riddle2alt = u""
   532                 riddle2alt = u""
   522             if v3alt:
   533             if v3alt:
   523                 riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3alt[0], v3alt[1])
   534                 riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3alt[0], v3alt[1])
   524             else:
   535             else:
   525                 riddle3alt = u""
   536                 riddle3alt = u""
   526             BUILDER.add_note_irr(identity, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
   537             BUILDER.add_note_irr(identity, unambiguous, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
   527         if singular and plural and RICH_MODE:
   538         if singular and plural and RICH_MODE:
   528             riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
   539             riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
   529             riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
   540             riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
   530             BUILDER.add_note_pl(identity, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
   541             BUILDER.add_note_pl(identity, unambiguous, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
   531 
   542 
   532     BUILDER.export(FONAME)
   543     BUILDER.export(FONAME)
   533 finally:
   544 finally:
   534     BUILDER.close()
   545     BUILDER.close()
   535     shutil.rmtree(TMPDIR, ignore_errors=True)
   546     shutil.rmtree(TMPDIR, ignore_errors=True)