36 if FONAME is None: |
36 if FONAME is None: |
37 FOUT = sys.stdout |
37 FOUT = sys.stdout |
38 else: |
38 else: |
39 FOUT = codecs.open(FONAME, "w", "utf-8") |
39 FOUT = codecs.open(FONAME, "w", "utf-8") |
40 |
40 |
41 def attr_key(item): |
41 for (headwords, translations) in DOM[1:]: |
42 (word, (pron, attrs)) = item |
|
43 if not attrs: |
|
44 return "zzz" |
|
45 best_vattr = None |
|
46 for attr in attrs: |
|
47 if attr in ["v1", "v2", "v3"]: |
|
48 if not best_vattr or (best_vattr and best_vattr > attr): |
|
49 best_vattr = attr |
|
50 if best_vattr: |
|
51 return best_vattr |
|
52 for attr in attrs: # single/plural |
|
53 if attr in ["s"]: |
|
54 return attr |
|
55 for attr in attrs: # comparative/superlative |
|
56 if attr in ["comp"]: |
|
57 return attr |
|
58 for attr in attrs: # Am/Br/Au |
|
59 if attr in ["Am"]: |
|
60 return attr |
|
61 return "zzz" |
|
62 |
|
63 for idx in range(1, len(DOM)): |
|
64 article = DOM[idx] |
|
65 defs = article[0].items() |
|
66 defs = sorted(defs, key = attr_key) |
|
67 lines = [] |
42 lines = [] |
68 for (word, (pron, attrs)) in defs: |
43 for hw in headwords: |
69 line = "<b>"+word+"</b>" |
44 line = "<b>"+hw.headword+"</b>" |
70 if pron: |
45 if hw.pron: |
71 line += " ["+pron+"]" |
46 line += " ["+hw.pron+"]" |
72 if len(attrs) > 0: |
47 if len(hw.attrs) > 0: |
73 attrs = [" «"+x+"»" for x in attrs] |
48 attrs = [" «"+x+"»" for x in hw.attrs] |
74 attrs.sort() |
49 attrs.sort() |
75 line += ",".join(attrs) |
50 line += ",".join(attrs) |
76 lines.append(line) |
51 lines.append(line) |
77 question = "<br>".join(lines) |
52 question = "<br>".join(lines) |
78 FOUT.write(question) |
53 FOUT.write(question) |
79 FOUT.write("\t") |
54 FOUT.write("\t") |
80 for sense in article[1]: |
55 for sense in translations: |
81 if not sense: |
56 if not sense: |
82 raise Exception("""Empty sense for article: """ + article[0].__iter__().__next__()) |
57 raise Exception("""Empty sense for article: """ + article[0].__iter__().__next__()) |
83 if sense.pos: |
58 if sense.pos: |
84 FOUT.write('<i style="color: green;">') |
59 FOUT.write('<i style="color: green;">') |
85 FOUT.write(sense.pos) |
60 FOUT.write(sense.pos) |