author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sat, 21 Oct 2017 21:50:03 +0300 | |
changeset 952 | 8f01b6342416 |
parent 937 | 981839c72b64 |
child 984 | 73d6e2631338 |
permissions | -rw-r--r-- |
631
f6d706812a0f
Fix: SyntaxError: Non-ASCII character '\xcb' in file, but no encoding
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
629
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
2 |
""" |
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
3 |
gadict dictionary format parser. |
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
4 |
""" |
385 | 5 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
6 |
import sys |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
7 |
import re |
385 | 8 |
|
9 |
||
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
10 |
class Prelude: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
11 |
"""Dictionary metainfo structure.""" |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
12 |
name = None |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
13 |
about = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
14 |
urls = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
15 |
authors = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
16 |
licences = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
17 |
|
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
18 |
|
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
19 |
class ParseException(BaseException): |
385 | 20 |
|
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
21 |
def __init__(self, msg, lineno=None, line=None): |
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
22 |
BaseException.__init__(self) |
385 | 23 |
self.msg = msg |
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
24 |
self.lineno = lineno |
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
25 |
self.line = line |
385 | 26 |
|
27 |
def __repr__(self): |
|
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
28 |
if self.lineno is None: |
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
29 |
return self.msg |
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
30 |
elif self.line is None: |
582
92a189ed49b6
Fix: TypeError: non-empty format string passed to object.__format__.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
569
diff
changeset
|
31 |
return u":{:d}:{:s}".format(self.lineno, self.msg) |
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
32 |
else: |
582
92a189ed49b6
Fix: TypeError: non-empty format string passed to object.__format__.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
569
diff
changeset
|
33 |
return u":{:d}: {:s}\nLINE: {:s}".format(self.lineno, self.msg, self.line) |
385 | 34 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
35 |
class Headword: |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
36 |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
37 |
def __init__(self, headword, pron = None, attrs = None, homo = None): |
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
38 |
self.headword = headword |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
39 |
self.pron = pron |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
40 |
self.attrs = attrs |
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
41 |
self.homo = homo |
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
42 |
|
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
43 |
def __str__(self): |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
44 |
return self.headword |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
45 |
def __repr__(self): |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
46 |
return "<Headword {}>".format(self.headword) |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
47 |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
48 |
class Sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
49 |
|
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
50 |
def __init__(self, pos, tr_list = None, ex_list = None, glos_list = None, ant_list = None, syn_list = None, rel_list = None, topic_list = None, hyper_list = None, hypo_list = None): |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
51 |
if not pos: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
52 |
raise ParseException("Part of speech expected...\n") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
53 |
self.pos = pos |
553
45a3138c9b4d
Simplify expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
552
diff
changeset
|
54 |
self.tr_list = tr_list |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
55 |
self.ex_list = ex_list |
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
56 |
self.glos_list = glos_list |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
57 |
self.ant_list = ant_list |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
58 |
self.syn_list = syn_list |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
59 |
self.rel_list = rel_list |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
60 |
self.topic_list = topic_list |
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
61 |
self.hyper_list = hyper_list |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
62 |
self.hypo_list = hypo_list |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
63 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
64 |
def add_tr(self, tr): |
569
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
65 |
if self.tr_list: |
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
66 |
self.tr_list.append(tr) |
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
67 |
else: |
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
68 |
self.tr_list = [tr] |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
69 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
70 |
def add_ex(self, ex): |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
71 |
if self.ex_list: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
72 |
self.ex_list.append(ex) |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
73 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
74 |
self.ex_list = [ex] |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
75 |
|
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
76 |
def add_glos(self, glos): |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
77 |
if self.glos_list: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
78 |
self.glos_list.append(glos) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
79 |
else: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
80 |
self.glos_list = [glos] |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
81 |
|
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
82 |
def add_ant(self, ant): |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
83 |
if self.ant_list: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
84 |
self.ant_list.append(ant) |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
85 |
else: |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
86 |
self.ant_list = [ant] |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
87 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
88 |
def add_syn(self, syn): |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
89 |
if self.syn_list: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
90 |
self.syn_list.append(syn) |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
91 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
92 |
self.syn_list = [syn] |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
93 |
|
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
94 |
def add_rel(self, rel): |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
95 |
if self.rel_list: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
96 |
self.rel_list.append(rel) |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
97 |
else: |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
98 |
self.rel_list = [rel] |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
99 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
100 |
def add_topic(self, topic): |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
101 |
if self.topic_list: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
102 |
self.topic_list.append(topic) |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
103 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
104 |
self.topic_list = [topic] |
385 | 105 |
|
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
106 |
def add_hyper(self, hyper): |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
107 |
if self.hyper_list: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
108 |
self.hyper_list.append(hyper) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
109 |
else: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
110 |
self.hyper_list = [hyper] |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
111 |
|
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
112 |
def add_hypo(self, hypo): |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
113 |
if self.hypo_list: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
114 |
self.hypo_list.append(hypo) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
115 |
else: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
116 |
self.hypo_list = [hypo] |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
117 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
118 |
def __str__(self): |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
119 |
if tr_list: |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
120 |
(lang, text) = self.tr_list[0] |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
121 |
return "{}: {}".format(lang, text) |
568 | 122 |
return "<empy sense>" |
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
123 |
def __repr__(self): |
568 | 124 |
return "<Sense {}>".format(str(self)) |
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
125 |
|
385 | 126 |
class Parser: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
127 |
"""gadict dictionary format parser.""" |
385 | 128 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
129 |
COMMENT_RE = re.compile("^# ") |
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
130 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
131 |
SEPARATOR_RE = re.compile(u"^__$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
132 |
HEADWORD_RE = re.compile( u"^(\\w.*)$" ) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
133 |
HEADWORD_VAR_RE = re.compile(u"^ +(rare|s|pl|v[123]|male|female|comp|super|abbr|Am|Br|Au)$", re.UNICODE) |
892
0c298fe6739e
Marked glottal stop as valid IPA sign.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
871
diff
changeset
|
134 |
HEADWORD_PRON_RE = re.compile(u"^ +\\[([a-zˌˈːəæɛɒʊɪɔɜɑʌɚɐɹʃʧθðɡʒŋɾʔ ]+)\\]$", re.UNICODE) |
937
981839c72b64
Made parsing rule more strict for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
931
diff
changeset
|
135 |
HEADWORD_HOMO_RE = re.compile(u"^ +homo: (\\w|\\w[-'\\w ;]*\\w)$", re.UNICODE) |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
136 |
TRANSL_POS_RE = re.compile(u"^(?:n|det|pron|adj|v|adv|prep|conj|num|int|phr|phr\\.v|contr|abbr|prefix)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
137 |
TRANSL_RE = re.compile(u"^(ru|uk|la|en): ([\\w(].*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
138 |
TRANSL_EX_RE = re.compile(u"""^(ru|uk|la|en)> ([-'"\\w].*)$""", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
139 |
TRANSL_GLOS_RE = re.compile(u"^(ru|uk|la|en)= ([-\\w\\d].*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
140 |
TOPIC_RE = re.compile(u"^topic: (\\w.*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
141 |
SYN_RE = re.compile(u"^syn: (\\w.*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
142 |
ANT_RE = re.compile(u"^ant: (\\w.*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
143 |
REL_RE = re.compile(u"^rel: (\\w.*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
144 |
HYPER_RE = re.compile(u"^hyper: (\\w.*)$", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
145 |
HYPO_RE = re.compile(u"^hypo: (\\w.*)$", re.UNICODE) |
385 | 146 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
147 |
CONT_RE = re.compile(u"^ +(.*)", re.UNICODE) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
148 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
149 |
TRAILING_SPACES_RE = re.compile(u"\\s+$", re.UNICODE) |
385 | 150 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
151 |
PRELUDE_NAME_RE = re.compile(u"^name: (.*)", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
152 |
PRELUDE_URL_RE = re.compile(u"^url: (.*)", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
153 |
PRELUDE_AUTHOR_RE = re.compile(u"^by: (.*)", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
154 |
PRELUDE_LICENSE_RE = re.compile(u"^term: (.*)", re.UNICODE) |
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
155 |
PRELUDE_ABOUT_RE = re.compile(u"^about: ?(.*)", re.UNICODE) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
156 |
|
385 | 157 |
def __init__(self): |
158 |
pass |
|
159 |
||
160 |
def readline(self): |
|
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
161 |
while True: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
162 |
self.line = self.stream.readline() |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
163 |
self.eof = len(self.line) == 0 |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
164 |
if not self.eof: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
165 |
self.lineno += 1 |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
166 |
self.line = self.line.rstrip('\n') |
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
167 |
if self.TRAILING_SPACES_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
168 |
raise ParseException("Traling spaces detected...\n") |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
169 |
if self.COMMENT_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
170 |
continue |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
171 |
break |
385 | 172 |
|
173 |
def parse(self, stream): |
|
174 |
self.lineno = 0 |
|
175 |
self.stream = stream |
|
176 |
self.dom = [] |
|
177 |
try: |
|
178 |
self.parse_prelude() |
|
179 |
while not self.eof: |
|
180 |
self.parse_article() |
|
181 |
except ParseException as ex: |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
182 |
if sys.version_info.major == 2: |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
183 |
import traceback |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
184 |
traceback.print_exc() |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
185 |
raise ParseException(ex.msg, self.lineno, self.line) |
385 | 186 |
return self.dom |
187 |
||
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
188 |
def parse_prelude_continuation(self): |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
189 |
string = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
190 |
while True: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
191 |
self.readline() |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
192 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
193 |
return string |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
194 |
m = self.CONT_RE.match(self.line) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
195 |
if m is not None: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
196 |
string += "\n" + m.group(1) |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
197 |
elif len(self.line) == 0: |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
198 |
string += "\n" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
199 |
else: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
200 |
return string |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
201 |
|
385 | 202 |
def parse_prelude(self): |
203 |
"""Read dictionary prelude until first "__" delimiter.""" |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
204 |
pre = Prelude() |
385 | 205 |
while True: |
206 |
self.readline() |
|
207 |
if self.eof: |
|
208 |
raise ParseException("There are no articles...") |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
209 |
m = self.PRELUDE_ABOUT_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
210 |
if m: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
211 |
pre.about += m.group(1) + self.parse_prelude_continuation() |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
212 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
213 |
raise ParseException("There are no articles...") |
385 | 214 |
if self.SEPARATOR_RE.match(self.line): |
215 |
break |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
216 |
m = self.PRELUDE_NAME_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
217 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
218 |
pre.name = m.group(1) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
219 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
220 |
m = self.PRELUDE_URL_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
221 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
222 |
pre.urls.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
223 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
224 |
m = self.PRELUDE_AUTHOR_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
225 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
226 |
pre.authors.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
227 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
228 |
m = self.PRELUDE_LICENSE_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
229 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
230 |
pre.licences.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
231 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
232 |
self.dom.append(pre) |
385 | 233 |
|
234 |
def parse_article(self): |
|
235 |
"""Try to match article until next "__" delimiter. Assume that `self.line` point to "__" delimiter.""" |
|
236 |
self.words = None |
|
237 |
self.tran = None |
|
238 |
self.parse_empty_line() |
|
239 |
self.parse_headlines() |
|
240 |
self.parse_translation() |
|
241 |
self.dom.append((self.words, self.tran)) |
|
242 |
||
243 |
def parse_empty_line(self): |
|
244 |
self.readline() |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
245 |
if self.eof or len(self.line) != 0: |
385 | 246 |
raise ParseException(""""__" delimiter should followed by empty line...""") |
247 |
||
248 |
def parse_headlines(self): |
|
249 |
"""Try to match word variations with attributed. Assume that `self.line` on preceding empty line.""" |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
250 |
self.words = [] |
385 | 251 |
self.readline() |
252 |
if self.eof: |
|
253 |
raise ParseException("""There are no definition after "__" delimiter...""") |
|
254 |
m = self.HEADWORD_RE.match(self.line) |
|
255 |
if m is None: |
|
256 |
raise ParseException("""There are no headword after "__" delimiter...""") |
|
257 |
word = m.group(1) |
|
258 |
pron = None |
|
259 |
attrs = set() |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
260 |
homo = None |
385 | 261 |
while True: |
262 |
self.readline() |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
263 |
if self.eof or len(self.line) == 0: |
385 | 264 |
break |
265 |
m = self.HEADWORD_RE.match(self.line) |
|
266 |
if m is not None: |
|
267 |
if word is None: |
|
268 |
raise ParseException("""Didn't match previous headword...""") |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
269 |
self.words.append(Headword(word, pron, attrs, homo = homo)) |
385 | 270 |
word = m.group(1) |
271 |
pron = None |
|
272 |
attrs = set() |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
273 |
homo = None |
385 | 274 |
continue |
275 |
m = self.HEADWORD_PRON_RE.match(self.line) |
|
276 |
if m is not None: |
|
277 |
if pron is not None: |
|
278 |
raise ParseException("""Pronunciation is redefined...""") |
|
279 |
pron = m.group(1) |
|
280 |
continue |
|
281 |
m = self.HEADWORD_VAR_RE.match(self.line) |
|
282 |
if m is not None: |
|
283 |
attrs.add(m.group(1)) |
|
284 |
continue |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
285 |
m = self.HEADWORD_HOMO_RE.match(self.line) |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
286 |
if m is not None: |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
287 |
if homo is not None: |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
288 |
raise ParseException("""Homophones are redefined...""") |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
289 |
homo = [s.strip() for s in m.group(1).split(";")] |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
290 |
continue |
400
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
291 |
raise ParseException("""Line is not a headword or translation or headword attribute...""") |
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
292 |
self.words.append(Headword(word, pron, attrs, homo)) |
385 | 293 |
|
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
294 |
def parse_translation_continuation(self): |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
295 |
string = "" |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
296 |
while True: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
297 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
298 |
if self.eof: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
299 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
300 |
m = self.CONT_RE.match(self.line) |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
301 |
if m is not None: |
593
32de94c1b039
Disable word wrapping for dictd dictionary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
582
diff
changeset
|
302 |
string += " " + m.group(1) |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
303 |
else: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
304 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
305 |
|
385 | 306 |
def parse_translation(self): |
307 |
senses = [] |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
308 |
sense = None |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
309 |
read = True |
385 | 310 |
while True: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
311 |
if read: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
312 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
313 |
read = True |
385 | 314 |
if self.eof: |
659
3d4ea0a5928f
Fix: last sense in last article is skipped.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
647
diff
changeset
|
315 |
if sense: |
3d4ea0a5928f
Fix: last sense in last article is skipped.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
647
diff
changeset
|
316 |
senses.append(sense) |
385 | 317 |
break |
318 |
m = self.SEPARATOR_RE.match(self.line) |
|
319 |
if m is not None: |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
320 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
321 |
senses.append(sense) |
385 | 322 |
break |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
323 |
if len(self.line) == 0: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
324 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
325 |
senses.append(sense) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
326 |
sense = None |
385 | 327 |
continue |
328 |
m = self.TRANSL_POS_RE.match(self.line) |
|
329 |
if m is not None: |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
330 |
if sense is not None: |
385 | 331 |
raise ParseException("""Each translation should have only one part of speech marker...""") |
332 |
pos = m.group(0) |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
333 |
sense = Sense(pos) |
385 | 334 |
continue |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
335 |
if not sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
336 |
raise ParseException("""Missing part of speech marker...""") |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
337 |
m = self.TOPIC_RE.match(self.line) |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
338 |
if m is not None: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
339 |
topics = m.group(1).split(";") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
340 |
for topic in topics: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
341 |
topic = topic.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
342 |
if len(topic) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
343 |
raise ParseException("""Empty topic...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
344 |
sense.add_topic(topic) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
345 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
346 |
m = self.SYN_RE.match(self.line) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
347 |
if m is not None: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
348 |
syns = m.group(1).split(";") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
349 |
for syn in syns: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
350 |
syn = syn.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
351 |
if len(syn) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
352 |
raise ParseException("""Empty synonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
353 |
sense.add_syn(syn) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
354 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
355 |
m = self.ANT_RE.match(self.line) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
356 |
if m is not None: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
357 |
ants = m.group(1).split(";") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
358 |
for ant in ants: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
359 |
ant = ant.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
360 |
if len(ant) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
361 |
raise ParseException("""Empty antonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
362 |
sense.add_ant(ant) |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
363 |
continue |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
364 |
m = self.REL_RE.match(self.line) |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
365 |
if m is not None: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
366 |
rels = m.group(1).split(";") |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
367 |
for rel in rels: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
368 |
rel = rel.strip() |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
369 |
if len(rel) == 0: |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
370 |
raise ParseException("""Empty relation...""") |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
371 |
sense.add_rel(rel) |
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
372 |
continue |
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
373 |
m = self.HYPER_RE.match(self.line) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
374 |
if m is not None: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
375 |
hypers = m.group(1).split(";") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
376 |
for hyper in hypers: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
377 |
hyper = hyper.strip() |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
378 |
if len(hyper) == 0: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
379 |
raise ParseException("""Empty hypernym...""") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
380 |
sense.add_hyper(hyper) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
381 |
continue |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
382 |
m = self.HYPO_RE.match(self.line) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
383 |
if m is not None: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
384 |
hypos = m.group(1).split(";") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
385 |
for hypo in hypos: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
386 |
hypo = hypo.strip() |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
387 |
if len(hypo) == 0: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
388 |
raise ParseException("""Empty hyponym...""") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
389 |
sense.add_hypo(hypo) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
390 |
continue |
385 | 391 |
m = self.TRANSL_RE.match(self.line) |
392 |
if m is not None: |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
393 |
sense.add_tr((m.group(1), m.group(2) + self.parse_translation_continuation())) |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
394 |
read = False |
385 | 395 |
continue |
396 |
m = self.TRANSL_EX_RE.match(self.line) |
|
397 |
if m is not None: |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
398 |
sense.add_ex((m.group(1), m.group(2) + self.parse_translation_continuation())) |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
399 |
read = False |
385 | 400 |
continue |
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
401 |
m = self.TRANSL_GLOS_RE.match(self.line) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
402 |
if m is not None: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
403 |
sense.add_glos((m.group(1), m.group(2) + self.parse_translation_continuation())) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
404 |
read = False |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
405 |
continue |
385 | 406 |
raise ParseException("""Uknown syntax...""") |
407 |
self.tran = senses |
|
594
910efcf51ac0
Fix: regex doesn't match whole line. Add Parser representation to simplify debugging.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
593
diff
changeset
|
408 |
|
910efcf51ac0
Fix: regex doesn't match whole line. Add Parser representation to simplify debugging.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
593
diff
changeset
|
409 |
def __repr__(self): |
910efcf51ac0
Fix: regex doesn't match whole line. Add Parser representation to simplify debugging.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
593
diff
changeset
|
410 |
return u"<{:s}; line={:s}>".format(type(self).__name__, repr(self.line)) |