py/gadict_c5.py
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sun, 27 Mar 2016 22:43:45 +0300
changeset 399 a6a7036f3c6f
parent 394 4d45194c71b6
child 402 b47698d5ccab
permissions -rw-r--r--
File name is not available in parser. Move error printing to writer.


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()
try:
    dom = parser.parse(fin)
except gadict.ParseException as ex:
    sys.stdout.write("{:s}{:s}\n".format(fgadict, repr(ex)))
    if __debug__:
        import traceback
        traceback.print_exc()
    exit(1)
finally:
    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")