py/gadict_srs_anki.py
changeset 646 2d488cfc4c0c
parent 644 e38cd6112193
child 647 6ae5399c8087
equal deleted inserted replaced
645:6d4a074cea27 646:2d488cfc4c0c
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
       
     2 """Anki card writer"""
     2 
     3 
     3 import os
     4 import os
     4 import io
     5 import io
     5 import sys
     6 import sys
     6 import codecs
     7 import codecs
     8 import shutil
     9 import shutil
     9 import signal
    10 import signal
    10 import regex
    11 import regex
    11 
    12 
    12 import gadict
    13 import gadict
       
    14 import gadict_freq
    13 
    15 
    14 
    16 
    15 FINAME = None
    17 FINAME = None
    16 FONAME = None
    18 FONAME = None
    17 LANGS = None
    19 LANGS = None
       
    20 FREQ_SOURCES = []
    18 
    21 
    19 # -lang:ru,uk
    22 # -lang:ru,uk
    20 ARG_LANG_RE = regex.compile("-lang:(.+)")
    23 ARG_LANG_RE = regex.compile("-lang:(.+)")
    21 # -freq:var:TAG=FILE or -freq:freq:TAG=FILE
    24 # -freq:var:TAG=FILE or -freq:freq:TAG=FILE
    22 ARG_FREQ_RE = regex.compile("-freq:(freq|var):([^=]+)=(.+)")
    25 ARG_FREQ_RE = regex.compile("-freq:(freq|var):([^=]+)=(.+)")
    35                 if len(lang) != 2:
    38                 if len(lang) != 2:
    36                     raise Exception("Incorrect language specification: '{:s}'".format(arg))
    39                     raise Exception("Incorrect language specification: '{:s}'".format(arg))
    37             continue
    40             continue
    38         m = ARG_FREQ_RE.match(arg)
    41         m = ARG_FREQ_RE.match(arg)
    39         if m:
    42         if m:
    40             LANGS = set(arg.split(","))
    43             mode = m.group(1)
    41             for lang in LANGS:
    44             tag = m.group(2)
    42                 if len(lang) != 2:
    45             fname = m.group(3)
    43                     raise Exception("Incorrect language specification: '{:s}'".format(arg))
    46             with io.open(fname, mode='r', buffering=1, encoding="utf-8") as stream:
       
    47                 if mode == "var":
       
    48                     parser = gadict_freq.HeadVarParser(stream)
       
    49                 elif mode == "freq":
       
    50                     parser = gadict_freq.FreqlistParser(stream)
       
    51                 else:
       
    52                     raise Exception("Unsupported mode: '{:s}'".format(mode))
       
    53                 wlist = parser.parse()
       
    54             FREQ_SOURCES.append((tag, set(wlist)))
    44             continue
    55             continue
    45         if arg.startswith("-"):
    56         if arg.startswith("-"):
    46             raise Exception("Unsupported option format: '{:s}'".format(arg))
    57             raise Exception("Unsupported option format: '{:s}'".format(arg))
    47     if not FINAME:
    58     if not FINAME:
    48         FINAME = arg
    59         FINAME = arg
   163 div.glos .lang {
   174 div.glos .lang {
   164   color: brown;
   175   color: brown;
   165 }
   176 }
   166 span.glos {
   177 span.glos {
   167   font-size: .95em;
   178   font-size: .95em;
       
   179 }
       
   180 .freq {
       
   181   color: red;
       
   182   font-weight: bold;
   168 }
   183 }
   169 .del {
   184 .del {
   170   color: red;
   185   color: red;
   171   font-weight: bold;
   186   font-weight: bold;
   172 }
   187 }
   292         builder.add_note("singular", identity, warnmsg, warnmsg+" singular", "del")
   307         builder.add_note("singular", identity, warnmsg, warnmsg+" singular", "del")
   293         builder.add_note("plural", identity, warnmsg, warnmsg+" plural", "del")
   308         builder.add_note("plural", identity, warnmsg, warnmsg+" plural", "del")
   294 
   309 
   295     for (headwords, translations) in DOM[1:]:
   310     for (headwords, translations) in DOM[1:]:
   296         identity = headwords[0].headword
   311         identity = headwords[0].headword
       
   312         freqtags = []
       
   313         for (freqtag, freqset) in FREQ_SOURCES:
       
   314             if identity in freqset:
       
   315                 freqtags.append(freqtag)
       
   316         freqmsg = None
       
   317         if len(freqtags) > 0:
       
   318             freqmsg = ",".join(freqtags)
       
   319             freqmsg = "<div class='freq'>{:s}</div>".format(freqmsg)
   297         buf = []
   320         buf = []
   298         v1, v2, v3 = (None, None, None)
   321         v1, v2, v3 = (None, None, None)
   299         singular, plural = (None, None)
   322         singular, plural = (None, None)
   300         for hw in headwords:
   323         for hw in headwords:
   301             buf.append("<div clsas='headword'>")
   324             buf.append("<div clsas='headword'>")
   321             if 's' in hw.attrs:
   344             if 's' in hw.attrs:
   322                 singular = (hw.headword, hw.pron)
   345                 singular = (hw.headword, hw.pron)
   323             if 'pl' in hw.attrs:
   346             if 'pl' in hw.attrs:
   324                 plural = (hw.headword, hw.pron)
   347                 plural = (hw.headword, hw.pron)
   325             buf.append("</div>")
   348             buf.append("</div>")
       
   349         if freqmsg:
       
   350             buf.append(freqmsg)
   326         direct_from = "".join(buf)
   351         direct_from = "".join(buf)
   327         buf = []
   352         buf = []
   328         for sense in translations:
   353         for sense in translations:
   329             write_sense(buf, sense, with_examples = True)
   354             write_sense(buf, sense, with_examples = True)
   330         direct_to = "".join(buf)
   355         direct_to = "".join(buf)