Handling for sequence of article with same headword (as workaround for
authorOleksandr Gavenko <gavenkoa@gmail.com>
Thu, 12 Jan 2017 19:04:32 +0200
changeset 739 4c70daa48492
parent 738 206fd730aec4
child 740 77668cb05069
Handling for sequence of article with same headword (as workaround for different pronunciation depending on meaning).
py/gadict_srs_anki.py
--- a/py/gadict_srs_anki.py	Thu Jan 12 19:03:14 2017 +0200
+++ b/py/gadict_srs_anki.py	Thu Jan 12 19:04:32 2017 +0200
@@ -328,24 +328,28 @@
         collection.models.save(model)
         self.model_pl = model
 
-    def guid(self, nodetype, headword):
+    def guid(self, nodetype, headword, unambiguous):
+        """
+        :nodetype     used to generate different notes from same headword
+        :unambiguous  used if several subsequent article with same headword (and different pronunciation)
         """
-        :nodetype  used to generate different notes from same headword
-        """
-        h = hashlib.md5(":".join((self.name, nodetype, headword)))
+        if unambiguous > 0:
+            h = hashlib.md5(":".join((self.name, nodetype, headword, str(unambiguous))))
+        else:
+            h = hashlib.md5(":".join((self.name, nodetype, headword)))
         return h.hexdigest()
 
-    def add_note(self, headword, front, back, safeback, freq, tags=None):
+    def add_note(self, headword, unambiguous, front, back, safeback, freq="", tags=None):
         note = anki.notes.Note(self.collection, self.model)
         note['Front'] = front
         note['Back'] = back
         note['SafeBack'] = safeback
         note['Freq'] = freq
         note_add_tags(note, tags)
-        note.guid = self.guid("front/back", headword)
+        note.guid = self.guid("front/back", headword, unambiguous)
         self.collection.addNote(note)
 
-    def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, freq, tags=None):
+    def add_note_irr(self, headword, unambiguous, v1, v2, v2alt, v3, v3alt, front, back, freq, tags=None):
         note = anki.notes.Note(self.collection, self.model_irr)
         note['V1'] = v1
         note['V2'] = v2
@@ -356,10 +360,10 @@
         note['Back'] = back
         note['Freq'] = freq
         note_add_tags(note, tags)
-        note.guid = self.guid("irregular verb", headword)
+        note.guid = self.guid("irregular verb", headword, unambiguous)
         self.collection.addNote(note)
 
-    def add_note_pl(self, headword, singular, plural, front, back, freq, tags=None):
+    def add_note_pl(self, headword, unambiguous, singular, plural, front, back, freq, tags=None):
         note = anki.notes.Note(self.collection, self.model_pl)
         note['Singular'] = singular
         note['Plural'] = plural
@@ -367,7 +371,7 @@
         note['Back'] = back
         note['Freq'] = freq
         note_add_tags(note, tags)
-        note.guid = self.guid("singular/plural noun", headword)
+        note.guid = self.guid("singular/plural noun", headword, unambiguous)
         self.collection.addNote(note)
 
     def export(self, fname):
@@ -451,10 +455,12 @@
     for identity in FDEL or []:
         identity = identity.strip()
         warnmsg = "<div class='del'>Please delete this note ({})</div>".format(identity)
-        BUILDER.add_note(identity, warnmsg, warnmsg, warnmsg, "del")
-        BUILDER.add_note_irr(identity, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, "del")
-        BUILDER.add_note_pl(identity, warnmsg, warnmsg, warnmsg, warnmsg, "del")
+        BUILDER.add_note(identity, 0, warnmsg, warnmsg, warnmsg, tags="del")
+        BUILDER.add_note_irr(identity, 0, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, warnmsg, tags="del")
+        BUILDER.add_note_pl(identity, 0, warnmsg, warnmsg, warnmsg, warnmsg, tags="del")
 
+    prev_identity = None
+    unambiguous = 0
     for (headwords, translations) in DOM[1:]:
         identity = headwords[0].headword
         if 'rare' in headwords[0].attrs:
@@ -463,6 +469,11 @@
         for (freqtag, freqset) in FREQ_SOURCES:
             if identity in freqset:
                 freqtags.append(freqtag)
+        if prev_identity == identity:
+            unambiguous += 1
+        else:
+            prev_identity = identity
+            unambiguous = 0
         freqmsg = " "
         if len(freqtags) > 0:
             freqmsg = ",".join(freqtags)
@@ -510,7 +521,7 @@
         for sense in translations:
             write_sense(buf, sense, with_examples=False)
         reverse_from = "".join(buf)         # without examples!!
-        BUILDER.add_note(identity, direct_from, direct_to, reverse_from, freqmsg, freqtags)
+        BUILDER.add_note(identity, unambiguous, direct_from, direct_to, reverse_from, freqmsg)
         if v1 and v2 and v3 and RICH_MODE:
             riddle1 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v1</span>".format(v1[0], v1[1])
             riddle2 = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v2[0], v2[1])
@@ -523,11 +534,11 @@
                 riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v3</span>".format(v3alt[0], v3alt[1])
             else:
                 riddle3alt = u""
-            BUILDER.add_note_irr(identity, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
+            BUILDER.add_note_irr(identity, unambiguous, riddle1, riddle2, riddle2alt, riddle3, riddle3alt, direct_from, direct_to, freqmsg)
         if singular and plural and RICH_MODE:
             riddle_s = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>s</span>".format(singular[0], singular[1])
             riddle_pl = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>pl</span>".format(plural[0], plural[1])
-            BUILDER.add_note_pl(identity, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
+            BUILDER.add_note_pl(identity, unambiguous, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
 
     BUILDER.export(FONAME)
 finally: