py/gadict_c5.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sun, 27 Mar 2016 16:49:11 +0300
changeset 394 4d45194c71b6
parent 393 py/gadict.py@2756a6deca7e
child 399 a6a7036f3c6f
permissions -rw-r--r--
Split generator from parser.


import gadict
import io
import sys


fgadict = None
fnout = None
if len(sys.argv) >= 2:
    fgadict = sys.argv[1]
if len(sys.argv) >= 3:
    fnout = sys.argv[2]

fin = io.open(fgadict, mode='r', buffering=1, encoding="utf-8")
if fnout is None:
    fout = sys.stdout
else:
    fout = open(fnout, "w")


parser = gadict.Parser()
dom = parser.parse(fin)
fin.close()

for idx in range(1, len(dom)):
    article = dom[idx]
    fout.write("_____\n\n")
    title = "; ".join(article[0].keys())
    fout.write(title)
    fout.write("\n\n")
    for (word, (pron, attrs)) in article[0].items():
        if word == "approach":
            fout.write(str(article[0]))
        fout.write("  ")
        fout.write(word)
        fout.write("\n")
        if pron is not None:
            fout.write("    [")
            fout.write(pron)
            fout.write("]\n")
        if len(attrs) > 0:
            fout.write("    ")
            l = list(attrs)
            l.sort()
            fout.write(", ".join(l))
            fout.write("\n")
    fout.write("\n")
    for (pos, trs, exs) in article[1]:
        fout.write("  ")
        if pos is not None:
            fout.write("⟨")
            fout.write(pos)
            fout.write("⟩ ")
        for (lang, tr) in trs:
            if lang == "ru":
                fout.write(tr)
                break
        fout.write("\n")