author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Wed, 21 Sep 2016 22:40:23 +0300 | |
changeset 566 | 0bba61492c37 |
parent 565 | ac68f2680ea0 |
child 568 | 4b610eaaf4be |
permissions | -rw-r--r-- |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
1 |
""" |
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
2 |
gadict dictionary format parser. |
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
3 |
""" |
385 | 4 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
5 |
import sys |
385 | 6 |
import regex |
7 |
||
8 |
||
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
9 |
class Prelude: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
10 |
"""Dictionary metainfo structure.""" |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
11 |
name = None |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
12 |
about = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
13 |
urls = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
14 |
authors = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
15 |
licences = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
16 |
|
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
17 |
|
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
18 |
class ParseException(BaseException): |
385 | 19 |
|
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
20 |
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
|
21 |
BaseException.__init__(self) |
385 | 22 |
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
|
23 |
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
|
24 |
self.line = line |
385 | 25 |
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
elif self.line is None: |
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
30 |
return ":{:d}:{:s}".format(self.lineno, self.msg.encode('utf-8')) |
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
31 |
else: |
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
32 |
return ":{:d}: {:s}\nLINE: {:s}".format(self.lineno, self.msg.encode('utf-8'), self.line.encode('utf-8')) |
385 | 33 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
34 |
class Headword: |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
35 |
|
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
36 |
def __init__(self, headword, pron = None, attrs = None): |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
self.attrs = attrs |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
40 |
|
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
46 |
class Sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
47 |
|
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
48 |
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): |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
49 |
if not pos: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
50 |
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
|
51 |
self.pos = pos |
553
45a3138c9b4d
Simplify expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
552
diff
changeset
|
52 |
self.tr_list = tr_list |
45a3138c9b4d
Simplify expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
552
diff
changeset
|
53 |
if not tr_list: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
54 |
self.tr_list = [] |
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 |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
61 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
62 |
def add_tr(self, tr): |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
63 |
self.tr_list.append(tr) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
64 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
69 |
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
|
70 |
|
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
71 |
def add_glos(self, glos): |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
72 |
if self.glos_list: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
73 |
self.glos_list.append(glos) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
74 |
else: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
75 |
self.glos_list = [glos] |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
76 |
|
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
77 |
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
|
78 |
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
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
87 |
self.syn_list = [syn] |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
88 |
|
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 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
95 |
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
|
96 |
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
|
97 |
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
|
98 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
99 |
self.topic_list = [topic] |
385 | 100 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
(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
|
104 |
return "{}: {}".format(lang, text) |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
105 |
return "<empy sence>" |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
106 |
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
|
107 |
return "<Sence {}>".format(str(self)) |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
108 |
|
385 | 109 |
class Parser: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
110 |
"""gadict dictionary format parser.""" |
385 | 111 |
|
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
112 |
COMMENT_RE = regex.compile(r"^# ") |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
113 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
114 |
SEPARATOR_RE = regex.compile(u"^__$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
115 |
HEADWORD_RE = regex.compile( u"^(\\p{L}.*)$" ) |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
116 |
HEADWORD_VAR_RE = regex.compile(u"^ +(s|pl|v[123]|male|female|comp|super|abbr|Am|Br|Au)$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
117 |
HEADWORD_PRON_RE = regex.compile(u"^ +\\[([\p{L}' ]+)\\]$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
118 |
TRANSL_POS_RE = regex.compile(u"^n|det|pron|adj|v|adv|prep|conj|num|int|phr|phr\\.v|contr|abbr|prefix$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
119 |
TRANSL_RE = regex.compile(u"^(ru|uk|la|en): ([\\p{L}(].*)$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
120 |
TRANSL_EX_RE = regex.compile(u"^(ru|uk|la|en)> ([-\\p{L}].*)$") |
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
121 |
TRANSL_GLOS_RE = regex.compile(u"^(ru|uk|la|en)= ([-\\p{L}].*)$") |
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
122 |
TOPIC_RE = regex.compile(u"^topic: (\\p{L}.*)$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
123 |
SYN_RE = regex.compile(u"^syn: (\\p{L}.*)$") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
124 |
ANT_RE = regex.compile(u"^ant: (\\p{L}.*)$") |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
125 |
REL_RE = regex.compile(u"^rel: (\\p{L}.*)$") |
385 | 126 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
127 |
CONT_RE = regex.compile(u"^ +(.*)") |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
128 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
129 |
TRAILING_SPACES_RE = regex.compile(u"\\p{Z}+$") |
385 | 130 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
131 |
PRELUDE_NAME_RE = regex.compile(u"^name: (.*)") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
132 |
PRELUDE_URL_RE = regex.compile(u"^url: (.*)") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
133 |
PRELUDE_AUTHOR_RE = regex.compile(u"^by: (.*)") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
134 |
PRELUDE_LICENSE_RE = regex.compile(u"^term: (.*)") |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
135 |
PRELUDE_ABOUT_RE = regex.compile(u"^about: ?(.*)") |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
136 |
|
385 | 137 |
def __init__(self): |
138 |
pass |
|
139 |
||
140 |
def readline(self): |
|
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
141 |
while True: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
142 |
self.line = self.stream.readline() |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
143 |
self.eof = len(self.line) == 0 |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
144 |
if not self.eof: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
145 |
self.lineno += 1 |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
146 |
if self.TRAILING_SPACES_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
147 |
raise ParseException("Traling spaces detected...\n") |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
148 |
if self.COMMENT_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
149 |
continue |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
150 |
break |
385 | 151 |
|
152 |
def parse(self, stream): |
|
153 |
self.lineno = 0 |
|
154 |
self.stream = stream |
|
155 |
self.dom = [] |
|
156 |
try: |
|
157 |
self.parse_prelude() |
|
158 |
while not self.eof: |
|
159 |
self.parse_article() |
|
160 |
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
|
161 |
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
|
162 |
import traceback |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
163 |
traceback.print_exc() |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
164 |
raise ParseException(ex.msg, self.lineno, self.line) |
385 | 165 |
return self.dom |
166 |
||
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
167 |
def parse_prelude_continuation(self): |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
168 |
string = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
169 |
while True: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
170 |
self.readline() |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
171 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
172 |
return string |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
173 |
m = self.CONT_RE.match(self.line) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
174 |
if m is not None: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
175 |
string += "\n" + m.group(1) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
176 |
elif len(self.line) == 1: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
177 |
string += "\n" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
178 |
else: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
179 |
return string |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
180 |
|
385 | 181 |
def parse_prelude(self): |
182 |
"""Read dictionary prelude until first "__" delimiter.""" |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
183 |
pre = Prelude() |
385 | 184 |
while True: |
185 |
self.readline() |
|
186 |
if self.eof: |
|
187 |
raise ParseException("There are no articles...") |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
188 |
m = self.PRELUDE_ABOUT_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
189 |
if m: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
190 |
pre.about += m.group(1) + self.parse_prelude_continuation() |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
191 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
192 |
raise ParseException("There are no articles...") |
385 | 193 |
if self.SEPARATOR_RE.match(self.line): |
194 |
break |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
195 |
m = self.PRELUDE_NAME_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
196 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
197 |
pre.name = m.group(1) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
198 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
199 |
m = self.PRELUDE_URL_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
200 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
201 |
pre.urls.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
202 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
203 |
m = self.PRELUDE_AUTHOR_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
204 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
205 |
pre.authors.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
206 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
207 |
m = self.PRELUDE_LICENSE_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
208 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
209 |
pre.licences.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
210 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
211 |
self.dom.append(pre) |
385 | 212 |
|
213 |
def parse_article(self): |
|
214 |
"""Try to match article until next "__" delimiter. Assume that `self.line` point to "__" delimiter.""" |
|
215 |
self.words = None |
|
216 |
self.tran = None |
|
217 |
self.parse_empty_line() |
|
218 |
self.parse_headlines() |
|
219 |
self.parse_translation() |
|
220 |
self.dom.append((self.words, self.tran)) |
|
221 |
||
222 |
def parse_empty_line(self): |
|
223 |
self.readline() |
|
224 |
if self.eof or len(self.line) != 1: |
|
225 |
raise ParseException(""""__" delimiter should followed by empty line...""") |
|
226 |
||
227 |
def parse_headlines(self): |
|
228 |
"""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
|
229 |
self.words = [] |
385 | 230 |
self.readline() |
231 |
if self.eof: |
|
232 |
raise ParseException("""There are no definition after "__" delimiter...""") |
|
233 |
m = self.HEADWORD_RE.match(self.line) |
|
234 |
if m is None: |
|
235 |
raise ParseException("""There are no headword after "__" delimiter...""") |
|
236 |
word = m.group(1) |
|
237 |
pron = None |
|
238 |
attrs = set() |
|
239 |
while True: |
|
240 |
self.readline() |
|
241 |
if self.eof or len(self.line) == 1: |
|
242 |
break |
|
243 |
m = self.HEADWORD_RE.match(self.line) |
|
244 |
if m is not None: |
|
245 |
if word is None: |
|
246 |
raise ParseException("""Didn't match previous headword...""") |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
247 |
self.words.append(Headword(word, pron, attrs)) |
385 | 248 |
word = m.group(1) |
249 |
pron = None |
|
250 |
attrs = set() |
|
251 |
continue |
|
252 |
m = self.HEADWORD_PRON_RE.match(self.line) |
|
253 |
if m is not None: |
|
254 |
if pron is not None: |
|
255 |
raise ParseException("""Pronunciation is redefined...""") |
|
256 |
pron = m.group(1) |
|
257 |
continue |
|
258 |
m = self.HEADWORD_VAR_RE.match(self.line) |
|
259 |
if m is not None: |
|
260 |
attrs.add(m.group(1)) |
|
261 |
continue |
|
400
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
262 |
raise ParseException("""Line is not a headword or translation or headword attribute...""") |
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
263 |
self.words.append(Headword(word, pron, attrs)) |
385 | 264 |
|
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
265 |
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
|
266 |
string = "" |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
267 |
while True: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
268 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
269 |
if self.eof: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
270 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
271 |
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
|
272 |
if m is not None: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
273 |
string += "\n" + m.group(1) |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
274 |
else: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
275 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
276 |
|
385 | 277 |
def parse_translation(self): |
278 |
senses = [] |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
279 |
sense = None |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
280 |
read = True |
385 | 281 |
while True: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
282 |
if read: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
283 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
284 |
read = True |
385 | 285 |
if self.eof: |
286 |
break |
|
287 |
m = self.SEPARATOR_RE.match(self.line) |
|
288 |
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
|
289 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
290 |
senses.append(sense) |
385 | 291 |
break |
292 |
if len(self.line) == 1: |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
293 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
294 |
senses.append(sense) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
295 |
sense = None |
385 | 296 |
continue |
297 |
m = self.TRANSL_POS_RE.match(self.line) |
|
298 |
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
|
299 |
if sense is not None: |
385 | 300 |
raise ParseException("""Each translation should have only one part of speech marker...""") |
301 |
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
|
302 |
sense = Sense(pos) |
385 | 303 |
continue |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
304 |
if not sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
305 |
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
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
for topic in topics: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
310 |
topic = topic.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
311 |
if len(topic) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
312 |
raise ParseException("""Empty topic...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
313 |
sense.add_topic(topic) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
314 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
315 |
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
|
316 |
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
|
317 |
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
|
318 |
for syn in syns: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
319 |
syn = syn.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
320 |
if len(syn) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
321 |
raise ParseException("""Empty synonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
322 |
sense.add_syn(syn) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
323 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
324 |
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
|
325 |
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
|
326 |
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
|
327 |
for ant in ants: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
328 |
ant = ant.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
329 |
if len(ant) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
330 |
raise ParseException("""Empty antonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
331 |
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
|
332 |
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
|
333 |
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
|
334 |
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
|
335 |
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
|
336 |
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
|
337 |
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
|
338 |
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
|
339 |
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
|
340 |
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
|
341 |
continue |
385 | 342 |
m = self.TRANSL_RE.match(self.line) |
343 |
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
|
344 |
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
|
345 |
read = False |
385 | 346 |
continue |
347 |
m = self.TRANSL_EX_RE.match(self.line) |
|
348 |
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
|
349 |
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
|
350 |
read = False |
385 | 351 |
continue |
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
352 |
m = self.TRANSL_GLOS_RE.match(self.line) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
353 |
if m is not None: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
354 |
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
|
355 |
read = False |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
356 |
continue |
385 | 357 |
raise ParseException("""Uknown syntax...""") |
358 |
self.tran = senses |