Add frequency tags as separate field and as native Anki tags.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Sat, 12 Nov 2016 14:11:24 +0200
changeset 678 28e04408c0c0
parent 677 e330e7f195b7
child 679 2d82757deae4
Add frequency tags as separate field and as native Anki tags.
py/gadict_srs_anki.py
--- a/py/gadict_srs_anki.py	Sat Nov 12 13:55:08 2016 +0200
+++ b/py/gadict_srs_anki.py	Sat Nov 12 14:11:24 2016 +0200
@@ -223,15 +223,16 @@
         collection.models.addField(model, collection.models.newField('Front'))
         collection.models.addField(model, collection.models.newField('Back'))
         collection.models.addField(model, collection.models.newField('SafeBack'))
+        collection.models.addField(model, collection.models.newField('Freq'))
 
         tmpl = collection.models.newTemplate('Front -> Back')
-        tmpl['qfmt'] = '<div class="front">{{Front}}</div>'
+        tmpl['qfmt'] = '<div class="front">{{Front}}</div><div class="freq">{{Freq}}</div>'
         tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         tmpl = collection.models.newTemplate('SafeBack -> Front')
         tmpl['qfmt'] = '<div class="safe-back">{{SafeBack}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="front">{{Front}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="front">{{Front}}</div><div class="freq">{{Freq}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         # Equivalent of ``collection.models.add(model)`` without setting
@@ -259,32 +260,33 @@
         collection.models.addField(model, collection.models.newField('V3alt'))
         collection.models.addField(model, collection.models.newField('Front'))
         collection.models.addField(model, collection.models.newField('Back'))
+        collection.models.addField(model, collection.models.newField('Freq'))
 
         question = u"<div class='ask'>Find irregular verb:</div>"
 
         tmpl = collection.models.newTemplate('V1 -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{V1}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         tmpl = collection.models.newTemplate('V2 -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{V2}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         tmpl = collection.models.newTemplate('V3 -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{V3}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         tmpl = collection.models.newTemplate('V2alt -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{V2alt}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         tmpl = collection.models.newTemplate('V3alt -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{V3alt}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         model['id'] = MODEL_ID + 2          # Keep model['id'] unique.
@@ -302,17 +304,18 @@
         collection.models.addField(model, collection.models.newField('Plural'))
         collection.models.addField(model, collection.models.newField('Front'))
         collection.models.addField(model, collection.models.newField('Back'))
+        collection.models.addField(model, collection.models.newField('Freq'))
 
         question = u"<div class='ask'>Find singular/plural form:</div>"
 
         tmpl = collection.models.newTemplate('Singular -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{Singular}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         tmpl = collection.models.newTemplate('Plural -> Back')
         tmpl['qfmt'] = question + '<div class="front">{{Plural}}</div>'
-        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="back">{{Back}}</div>'
+        tmpl['afmt'] = '{{FrontSide}}<hr id=answer><div class="back">{{Front}}</div><div class="freq">{{Freq}}</div><div class="back">{{Back}}</div>'
         collection.models.addTemplate(model, tmpl)
 
         model['id'] = MODEL_ID + 3          # Keep model['id'] unique.
@@ -328,16 +331,17 @@
         h = hashlib.md5(":".join((self.name, nodetype, headword)))
         return h.hexdigest()
 
-    def add_note(self, headword, front, back, safeback, tags = None):
+    def add_note(self, headword, 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)
         self.collection.addNote(note)
 
-    def add_note_irr(self, headword, v1, v2, v2alt, v3, v3alt, front, back, tags = None):
+    def add_note_irr(self, headword, 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
@@ -346,16 +350,18 @@
         note['V3alt'] = v3alt
         note['Front'] = front
         note['Back'] = back
+        note['Freq'] = freq
         note_add_tags(note, tags)
         note.guid = self.guid("irregular verb", headword)
         self.collection.addNote(note)
 
-    def add_note_pl(self, headword, singular, plural, front, back, tags = None):
+    def add_note_pl(self, headword, singular, plural, front, back, freq, tags = None):
         note = anki.notes.Note(self.collection, self.model_pl)
         note['Singular'] = singular
         note['Plural'] = plural
         note['Front'] = front
         note['Back'] = back
+        note['Freq'] = freq
         note_add_tags(note, tags)
         note.guid = self.guid("singular/plural noun", headword)
         self.collection.addNote(note)
@@ -447,10 +453,9 @@
         for (freqtag, freqset) in FREQ_SOURCES:
             if identity in freqset:
                 freqtags.append(freqtag)
-        freqmsg = ""
+        freqmsg = " "
         if len(freqtags) > 0:
             freqmsg = ",".join(freqtags)
-            freqmsg = "<div class='freq'>{:s}</div>".format(freqmsg)
         buf = []
         v1, v2, v3, v2alt, v3alt = (None, None, None, None, None)
         singular, plural = (None, None)
@@ -495,7 +500,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)
+        builder.add_note(identity, direct_from, direct_to, reverse_from, freqmsg, freqtags)
         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])
@@ -508,11 +513,11 @@
                 riddle3alt = u"<span class='headword'>{}</span> <span class='pron'>[{}]</span> <span class='attrs'>v2</span>".format(v3alt[0], v3alt[1])
             else:
                 riddle3alt = ""
-            builder.add_note_irr(identity, riddle1 + freqmsg, riddle2 + freqmsg, riddle2alt, riddle3 + freqmsg, riddle3alt, direct_from, direct_to)
+            builder.add_note_irr(identity, 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 + freqmsg, riddle_pl + freqmsg, direct_from, direct_to)
+            builder.add_note_pl(identity, riddle_s, riddle_pl, direct_from, direct_to, freqmsg)
 
     builder.export(FONAME)
 finally: