author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Wed, 21 Sep 2016 22:09:37 +0300 | |
changeset 565 | ac68f2680ea0 |
parent 554 | 59714b9033bc |
child 566 | 0bba61492c37 |
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 |
|
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
48 |
def __init__(self, pos, tr_list = None, ex_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 |
565
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
61 |
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
|
62 |
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
|
63 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
68 |
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
|
69 |
|
ac68f2680ea0
Add syntax to add related words. Add separators between ant/syn/rel in
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
554
diff
changeset
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
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
|
79 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
80 |
self.syn_list = [syn] |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
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_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
|
83 |
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
|
84 |
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
|
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.rel_list = [rel] |
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_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
|
89 |
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
|
90 |
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
|
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.topic_list = [topic] |
385 | 93 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
(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
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
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
|
101 |
|
385 | 102 |
class Parser: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
103 |
"""gadict dictionary format parser.""" |
385 | 104 |
|
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
105 |
COMMENT_RE = regex.compile(r"^# ") |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
106 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
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
|
111 |
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
|
112 |
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
|
113 |
TRANSL_EX_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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
REL_RE = regex.compile(u"^rel: (\\p{L}.*)$") |
385 | 118 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
119 |
CONT_RE = regex.compile(u"^ +(.*)") |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
120 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
121 |
TRAILING_SPACES_RE = regex.compile(u"\\p{Z}+$") |
385 | 122 |
|
552
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
123 |
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
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
PRELUDE_ABOUT_RE = regex.compile(u"^about: ?(.*)") |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
128 |
|
385 | 129 |
def __init__(self): |
130 |
pass |
|
131 |
||
132 |
def readline(self): |
|
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
133 |
while True: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
134 |
self.line = self.stream.readline() |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
135 |
self.eof = len(self.line) == 0 |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
136 |
if not self.eof: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
137 |
self.lineno += 1 |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
138 |
if self.TRAILING_SPACES_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
139 |
raise ParseException("Traling spaces detected...\n") |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
140 |
if self.COMMENT_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
141 |
continue |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
142 |
break |
385 | 143 |
|
144 |
def parse(self, stream): |
|
145 |
self.lineno = 0 |
|
146 |
self.stream = stream |
|
147 |
self.dom = [] |
|
148 |
try: |
|
149 |
self.parse_prelude() |
|
150 |
while not self.eof: |
|
151 |
self.parse_article() |
|
152 |
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
|
153 |
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
|
154 |
import traceback |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
155 |
traceback.print_exc() |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
156 |
raise ParseException(ex.msg, self.lineno, self.line) |
385 | 157 |
return self.dom |
158 |
||
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
159 |
def parse_prelude_continuation(self): |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
160 |
string = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
161 |
while True: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
162 |
self.readline() |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
163 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
164 |
return string |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
165 |
m = self.CONT_RE.match(self.line) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
166 |
if m is not None: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
167 |
string += "\n" + m.group(1) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
168 |
elif len(self.line) == 1: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
169 |
string += "\n" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
170 |
else: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
171 |
return string |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
172 |
|
385 | 173 |
def parse_prelude(self): |
174 |
"""Read dictionary prelude until first "__" delimiter.""" |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
175 |
pre = Prelude() |
385 | 176 |
while True: |
177 |
self.readline() |
|
178 |
if self.eof: |
|
179 |
raise ParseException("There are no articles...") |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
180 |
m = self.PRELUDE_ABOUT_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
181 |
if m: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
182 |
pre.about += m.group(1) + self.parse_prelude_continuation() |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
183 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
184 |
raise ParseException("There are no articles...") |
385 | 185 |
if self.SEPARATOR_RE.match(self.line): |
186 |
break |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
187 |
m = self.PRELUDE_NAME_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
188 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
189 |
pre.name = m.group(1) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
190 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
191 |
m = self.PRELUDE_URL_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
192 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
193 |
pre.urls.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
194 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
195 |
m = self.PRELUDE_AUTHOR_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.authors.append(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_LICENSE_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.licences.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 |
self.dom.append(pre) |
385 | 204 |
|
205 |
def parse_article(self): |
|
206 |
"""Try to match article until next "__" delimiter. Assume that `self.line` point to "__" delimiter.""" |
|
207 |
self.words = None |
|
208 |
self.tran = None |
|
209 |
self.parse_empty_line() |
|
210 |
self.parse_headlines() |
|
211 |
self.parse_translation() |
|
212 |
self.dom.append((self.words, self.tran)) |
|
213 |
||
214 |
def parse_empty_line(self): |
|
215 |
self.readline() |
|
216 |
if self.eof or len(self.line) != 1: |
|
217 |
raise ParseException(""""__" delimiter should followed by empty line...""") |
|
218 |
||
219 |
def parse_headlines(self): |
|
220 |
"""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
|
221 |
self.words = [] |
385 | 222 |
self.readline() |
223 |
if self.eof: |
|
224 |
raise ParseException("""There are no definition after "__" delimiter...""") |
|
225 |
m = self.HEADWORD_RE.match(self.line) |
|
226 |
if m is None: |
|
227 |
raise ParseException("""There are no headword after "__" delimiter...""") |
|
228 |
word = m.group(1) |
|
229 |
pron = None |
|
230 |
attrs = set() |
|
231 |
while True: |
|
232 |
self.readline() |
|
233 |
if self.eof or len(self.line) == 1: |
|
234 |
break |
|
235 |
m = self.HEADWORD_RE.match(self.line) |
|
236 |
if m is not None: |
|
237 |
if word is None: |
|
238 |
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
|
239 |
self.words.append(Headword(word, pron, attrs)) |
385 | 240 |
word = m.group(1) |
241 |
pron = None |
|
242 |
attrs = set() |
|
243 |
continue |
|
244 |
m = self.HEADWORD_PRON_RE.match(self.line) |
|
245 |
if m is not None: |
|
246 |
if pron is not None: |
|
247 |
raise ParseException("""Pronunciation is redefined...""") |
|
248 |
pron = m.group(1) |
|
249 |
continue |
|
250 |
m = self.HEADWORD_VAR_RE.match(self.line) |
|
251 |
if m is not None: |
|
252 |
attrs.add(m.group(1)) |
|
253 |
continue |
|
400
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
254 |
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
|
255 |
self.words.append(Headword(word, pron, attrs)) |
385 | 256 |
|
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
257 |
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
|
258 |
string = "" |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
259 |
while True: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
260 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
261 |
if self.eof: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
262 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
263 |
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
|
264 |
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
|
265 |
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
|
266 |
else: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
267 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
268 |
|
385 | 269 |
def parse_translation(self): |
270 |
senses = [] |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
271 |
sense = None |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
272 |
read = True |
385 | 273 |
while True: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
274 |
if read: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
275 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
276 |
read = True |
385 | 277 |
if self.eof: |
278 |
break |
|
279 |
m = self.SEPARATOR_RE.match(self.line) |
|
280 |
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
|
281 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
282 |
senses.append(sense) |
385 | 283 |
break |
284 |
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
|
285 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
286 |
senses.append(sense) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
287 |
sense = None |
385 | 288 |
continue |
289 |
m = self.TRANSL_POS_RE.match(self.line) |
|
290 |
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
|
291 |
if sense is not None: |
385 | 292 |
raise ParseException("""Each translation should have only one part of speech marker...""") |
293 |
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
|
294 |
sense = Sense(pos) |
385 | 295 |
continue |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
296 |
if not sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
297 |
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
|
298 |
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
|
299 |
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
|
300 |
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
|
301 |
for topic in topics: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
302 |
topic = topic.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
303 |
if len(topic) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
304 |
raise ParseException("""Empty topic...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
305 |
sense.add_topic(topic) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
306 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
for syn in syns: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
311 |
syn = syn.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
312 |
if len(syn) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
313 |
raise ParseException("""Empty synonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
314 |
sense.add_syn(syn) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
315 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
316 |
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
|
317 |
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
|
318 |
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
|
319 |
for ant in ants: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
320 |
ant = ant.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
321 |
if len(ant) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
322 |
raise ParseException("""Empty antonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
323 |
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
|
324 |
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
|
325 |
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
|
326 |
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
|
327 |
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
|
328 |
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
|
329 |
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
|
330 |
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
|
331 |
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
|
332 |
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
|
333 |
continue |
385 | 334 |
m = self.TRANSL_RE.match(self.line) |
335 |
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
|
336 |
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
|
337 |
read = False |
385 | 338 |
continue |
339 |
m = self.TRANSL_EX_RE.match(self.line) |
|
340 |
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
|
341 |
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
|
342 |
read = False |
385 | 343 |
continue |
344 |
raise ParseException("""Uknown syntax...""") |
|
345 |
self.tran = senses |