author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Mon, 27 Feb 2023 00:55:27 +0200 | |
changeset 1342 | d6413e1d20b0 |
parent 1313 | db3f92fe3295 |
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 |
|
1148
263e9e066981
Fixed: NameError: name 'ParseException' is not defined.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1011
diff
changeset
|
9 |
from gadict_util import ParseException |
385 | 10 |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
11 |
class Prelude: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
12 |
"""Dictionary metainfo structure.""" |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
13 |
name = None |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
14 |
about = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
15 |
urls = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
16 |
authors = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
17 |
licences = [] |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
18 |
|
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
19 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
20 |
class Headword: |
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
21 |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
self.attrs = attrs |
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
26 |
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
|
27 |
|
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
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
|
31 |
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
|
32 |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
33 |
class Sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
34 |
|
1006
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
35 |
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, col_list = None, countable = None): |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
36 |
if not pos: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
37 |
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
|
38 |
self.pos = pos |
553
45a3138c9b4d
Simplify expression.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
552
diff
changeset
|
39 |
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
|
40 |
self.ex_list = ex_list |
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
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
|
44 |
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
|
45 |
self.topic_list = topic_list |
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
46 |
self.hyper_list = hyper_list |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
47 |
self.hypo_list = hypo_list |
984
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
48 |
self.col_list = col_list |
1006
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
49 |
self.countable = countable |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
50 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
51 |
def add_tr(self, tr): |
569
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
52 |
if self.tr_list: |
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
53 |
self.tr_list.append(tr) |
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
54 |
else: |
5709d5bd349b
Fix: reverse card missed glossary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
568
diff
changeset
|
55 |
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
|
56 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
61 |
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
|
62 |
|
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
63 |
def add_glos(self, glos): |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
64 |
if self.glos_list: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
65 |
self.glos_list.append(glos) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
66 |
else: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
67 |
self.glos_list = [glos] |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
68 |
|
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 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
79 |
self.syn_list = [syn] |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
80 |
|
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 |
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
|
82 |
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
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
|
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
else: |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
91 |
self.topic_list = [topic] |
385 | 92 |
|
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
93 |
def add_hyper(self, hyper): |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
94 |
if self.hyper_list: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
95 |
self.hyper_list.append(hyper) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
96 |
else: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
97 |
self.hyper_list = [hyper] |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
98 |
|
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
99 |
def add_hypo(self, hypo): |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
100 |
if self.hypo_list: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
101 |
self.hypo_list.append(hypo) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
102 |
else: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
103 |
self.hypo_list = [hypo] |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
104 |
|
984
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
105 |
def add_col(self, col): |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
106 |
if self.col_list: |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
107 |
self.col_list.append(col) |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
108 |
else: |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
109 |
self.col_list = [col] |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
110 |
|
1006
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
111 |
def set_countable(self, countable): |
1313
db3f92fe3295
Fixed compatibility with Python 2.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1297
diff
changeset
|
112 |
if isinstance(countable, str) or isinstance(countable, unicode): |
1006
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
113 |
if countable == 'yes': |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
114 |
self.countable = True |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
115 |
elif countable == 'no': |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
116 |
self.countable = False |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
117 |
else: |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
118 |
raise ParseException("Countable can only be yes/no.") |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
119 |
elif isinstance(countable, bool): |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
120 |
self.countable = countable |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
121 |
else: |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
122 |
raise ParseException("Countable can only be yes/no or bool.") |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
123 |
|
554
59714b9033bc
Store headword structure as class. Store headwords in list to preserve order
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
553
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
(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
|
127 |
return "{}: {}".format(lang, text) |
568 | 128 |
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
|
129 |
def __repr__(self): |
568 | 130 |
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
|
131 |
|
385 | 132 |
class Parser: |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
133 |
"""gadict dictionary format parser.""" |
385 | 134 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
135 |
COMMENT_RE = re.compile("^# ") |
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
136 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
137 |
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
|
138 |
HEADWORD_RE = re.compile( u"^(\\w.*)$" ) |
1182
0ef4b4264a1a
Added category "baby" as opposite to male/female.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1148
diff
changeset
|
139 |
HEADWORD_VAR_RE = re.compile(u"^ +(rare|s|pl|v[123]|male|female|baby|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
|
140 |
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
|
141 |
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
|
142 |
TRANSL_POS_RE = re.compile(u"^(?:n|det|pron|adj|v|adv|prep|conj|num|int|phr|phr\\.v|contr|abbr|prefix)$", re.UNICODE) |
1297
2a7472a87c39
Defined string regex for Cyrillic script and general translation syntax. Russian Ё is not allowed. Fixed slipped mistakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
1182
diff
changeset
|
143 |
TRANSL_RE = re.compile(u"""^(ru|uk|la|en): ([- .,;/'"?!()0-9²A-Za-zА-Яа-яЄєІіЇїҐґ]+)$""", re.UNICODE) |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
144 |
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
|
145 |
TRANSL_GLOS_RE = re.compile(u"^(ru|uk|la|en)= ([-\\w\\d].*)$", re.UNICODE) |
1006
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
146 |
CNT_RE = re.compile(u"^cnt: (yes|no)$", re.UNICODE) |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
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
|
151 |
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
|
152 |
HYPO_RE = re.compile(u"^hypo: (\\w.*)$", re.UNICODE) |
984
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
153 |
COL_RE = re.compile(u"^col: (\\w.*)$", re.UNICODE) |
385 | 154 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
155 |
CONT_RE = re.compile(u"^ +(.*)", re.UNICODE) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
156 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
157 |
TRAILING_SPACES_RE = re.compile(u"\\s+$", re.UNICODE) |
385 | 158 |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
159 |
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
|
160 |
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
|
161 |
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
|
162 |
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
|
163 |
PRELUDE_ABOUT_RE = re.compile(u"^about: ?(.*)", re.UNICODE) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
164 |
|
385 | 165 |
def __init__(self): |
166 |
pass |
|
167 |
||
168 |
def readline(self): |
|
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
169 |
while True: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
170 |
self.line = self.stream.readline() |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
171 |
self.eof = len(self.line) == 0 |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
172 |
if not self.eof: |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
173 |
self.lineno += 1 |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
174 |
self.line = self.line.rstrip('\n') |
432
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
175 |
if self.TRAILING_SPACES_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
176 |
raise ParseException("Traling spaces detected...\n") |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
177 |
if self.COMMENT_RE.search(self.line): |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
178 |
continue |
b3a78fc20b31
Add parsing comment syntax.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
422
diff
changeset
|
179 |
break |
385 | 180 |
|
181 |
def parse(self, stream): |
|
182 |
self.lineno = 0 |
|
183 |
self.stream = stream |
|
184 |
self.dom = [] |
|
185 |
try: |
|
186 |
self.parse_prelude() |
|
187 |
while not self.eof: |
|
188 |
self.parse_article() |
|
189 |
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
|
190 |
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
|
191 |
import traceback |
7398bc1829d6
Port to python2 because Anki library written for this version.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
542
diff
changeset
|
192 |
traceback.print_exc() |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
193 |
raise ParseException(ex.msg, self.lineno, self.line) |
385 | 194 |
return self.dom |
195 |
||
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
196 |
def parse_prelude_continuation(self): |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
197 |
string = "" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
198 |
while True: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
199 |
self.readline() |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
200 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
201 |
return string |
406
f0ac87e10d9a
Fix warnings from pylint/pyflakes.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
402
diff
changeset
|
202 |
m = self.CONT_RE.match(self.line) |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
203 |
if m is not None: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
204 |
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
|
205 |
elif len(self.line) == 0: |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
206 |
string += "\n" |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
207 |
else: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
208 |
return string |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
209 |
|
385 | 210 |
def parse_prelude(self): |
211 |
"""Read dictionary prelude until first "__" delimiter.""" |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
212 |
pre = Prelude() |
385 | 213 |
while True: |
214 |
self.readline() |
|
215 |
if self.eof: |
|
216 |
raise ParseException("There are no articles...") |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
217 |
m = self.PRELUDE_ABOUT_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
218 |
if m: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
219 |
pre.about += m.group(1) + self.parse_prelude_continuation() |
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
220 |
if self.eof: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
221 |
raise ParseException("There are no articles...") |
385 | 222 |
if self.SEPARATOR_RE.match(self.line): |
223 |
break |
|
402
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
224 |
m = self.PRELUDE_NAME_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.name = 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_URL_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.urls.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 |
m = self.PRELUDE_AUTHOR_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
233 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
234 |
pre.authors.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
235 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
236 |
m = self.PRELUDE_LICENSE_RE.match(self.line) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
237 |
if m: |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
238 |
pre.licences.append(m.group(1)) |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
239 |
continue |
b47698d5ccab
Parse dictionary metainfo.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
400
diff
changeset
|
240 |
self.dom.append(pre) |
385 | 241 |
|
242 |
def parse_article(self): |
|
243 |
"""Try to match article until next "__" delimiter. Assume that `self.line` point to "__" delimiter.""" |
|
244 |
self.words = None |
|
245 |
self.tran = None |
|
246 |
self.parse_empty_line() |
|
247 |
self.parse_headlines() |
|
248 |
self.parse_translation() |
|
249 |
self.dom.append((self.words, self.tran)) |
|
250 |
||
251 |
def parse_empty_line(self): |
|
252 |
self.readline() |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
253 |
if self.eof or len(self.line) != 0: |
385 | 254 |
raise ParseException(""""__" delimiter should followed by empty line...""") |
255 |
||
256 |
def parse_headlines(self): |
|
257 |
"""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
|
258 |
self.words = [] |
385 | 259 |
self.readline() |
260 |
if self.eof: |
|
261 |
raise ParseException("""There are no definition after "__" delimiter...""") |
|
262 |
m = self.HEADWORD_RE.match(self.line) |
|
263 |
if m is None: |
|
264 |
raise ParseException("""There are no headword after "__" delimiter...""") |
|
265 |
word = m.group(1) |
|
266 |
pron = None |
|
267 |
attrs = set() |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
268 |
homo = None |
385 | 269 |
while True: |
270 |
self.readline() |
|
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
271 |
if self.eof or len(self.line) == 0: |
385 | 272 |
break |
273 |
m = self.HEADWORD_RE.match(self.line) |
|
274 |
if m is not None: |
|
275 |
if word is None: |
|
276 |
raise ParseException("""Didn't match previous headword...""") |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
277 |
self.words.append(Headword(word, pron, attrs, homo = homo)) |
385 | 278 |
word = m.group(1) |
279 |
pron = None |
|
280 |
attrs = set() |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
281 |
homo = None |
385 | 282 |
continue |
283 |
m = self.HEADWORD_PRON_RE.match(self.line) |
|
284 |
if m is not None: |
|
285 |
if pron is not None: |
|
286 |
raise ParseException("""Pronunciation is redefined...""") |
|
287 |
pron = m.group(1) |
|
288 |
continue |
|
289 |
m = self.HEADWORD_VAR_RE.match(self.line) |
|
290 |
if m is not None: |
|
291 |
attrs.add(m.group(1)) |
|
292 |
continue |
|
931
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
293 |
m = self.HEADWORD_HOMO_RE.match(self.line) |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
294 |
if m is not None: |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
295 |
if homo is not None: |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
296 |
raise ParseException("""Homophones are redefined...""") |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
297 |
homo = [s.strip() for s in m.group(1).split(";")] |
9a5f97027ee7
Added support for homophones.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
892
diff
changeset
|
298 |
continue |
400
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
299 |
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
|
300 |
self.words.append(Headword(word, pron, attrs, homo)) |
385 | 301 |
|
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
302 |
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
|
303 |
string = "" |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
304 |
while True: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
305 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
306 |
if self.eof: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
307 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
308 |
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
|
309 |
if m is not None: |
593
32de94c1b039
Disable word wrapping for dictd dictionary.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
582
diff
changeset
|
310 |
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
|
311 |
else: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
312 |
return string |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
313 |
|
385 | 314 |
def parse_translation(self): |
315 |
senses = [] |
|
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
316 |
sense = None |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
317 |
read = True |
385 | 318 |
while True: |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
319 |
if read: |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
320 |
self.readline() |
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
321 |
read = True |
385 | 322 |
if self.eof: |
659
3d4ea0a5928f
Fix: last sense in last article is skipped.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
647
diff
changeset
|
323 |
if sense: |
3d4ea0a5928f
Fix: last sense in last article is skipped.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
647
diff
changeset
|
324 |
senses.append(sense) |
385 | 325 |
break |
326 |
m = self.SEPARATOR_RE.match(self.line) |
|
327 |
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
|
328 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
329 |
senses.append(sense) |
385 | 330 |
break |
757
5417f2102dc5
Switch to built-in `re` Python module over `regex`.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
740
diff
changeset
|
331 |
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
|
332 |
if sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
333 |
senses.append(sense) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
334 |
sense = None |
385 | 335 |
continue |
336 |
m = self.TRANSL_POS_RE.match(self.line) |
|
337 |
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
|
338 |
if sense is not None: |
385 | 339 |
raise ParseException("""Each translation should have only one part of speech marker...""") |
340 |
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
|
341 |
sense = Sense(pos) |
385 | 342 |
continue |
530
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
343 |
if not sense: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
344 |
raise ParseException("""Missing part of speech marker...""") |
1006
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
345 |
m = self.CNT_RE.match(self.line) |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
346 |
if m is not None: |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
347 |
sense.set_countable(m.group(1)) |
b1f11eff7c70
Added support for countable.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
984
diff
changeset
|
348 |
continue |
412
ece60575a96a
Adopt to parse VOA dictionary: add topics support and translation continuation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
406
diff
changeset
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
for topic in topics: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
353 |
topic = topic.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
354 |
if len(topic) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
355 |
raise ParseException("""Empty topic...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
356 |
sense.add_topic(topic) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
357 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
358 |
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
|
359 |
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
|
360 |
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
|
361 |
for syn in syns: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
362 |
syn = syn.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
363 |
if len(syn) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
364 |
raise ParseException("""Empty synonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
365 |
sense.add_syn(syn) |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
366 |
continue |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
for ant in ants: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
371 |
ant = ant.strip() |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
372 |
if len(ant) == 0: |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
373 |
raise ParseException("""Empty antonym...""") |
91771594bc8b
Make storage for topics, antonyms and synonyms. Require pos marker.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
527
diff
changeset
|
374 |
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
|
375 |
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
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
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
|
381 |
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
|
382 |
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
|
383 |
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
|
384 |
continue |
618
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
385 |
m = self.HYPER_RE.match(self.line) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
386 |
if m is not None: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
387 |
hypers = m.group(1).split(";") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
388 |
for hyper in hypers: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
389 |
hyper = hyper.strip() |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
390 |
if len(hyper) == 0: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
391 |
raise ParseException("""Empty hypernym...""") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
392 |
sense.add_hyper(hyper) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
393 |
continue |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
394 |
m = self.HYPO_RE.match(self.line) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
395 |
if m is not None: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
396 |
hypos = m.group(1).split(";") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
397 |
for hypo in hypos: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
398 |
hypo = hypo.strip() |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
399 |
if len(hypo) == 0: |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
400 |
raise ParseException("""Empty hyponym...""") |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
401 |
sense.add_hypo(hypo) |
6ad7203ac9dc
Add support for hypernyms and hyponyms.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
594
diff
changeset
|
402 |
continue |
984
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
403 |
m = self.COL_RE.match(self.line) |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
404 |
if m is not None: |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
405 |
cols = m.group(1).split(";") |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
406 |
for col in cols: |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
407 |
col = col.strip() |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
408 |
if len(col) == 0: |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
409 |
raise ParseException("""Empty collocations...""") |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
410 |
sense.add_col(col) |
73d6e2631338
Added support for collocations' reference.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
937
diff
changeset
|
411 |
continue |
385 | 412 |
m = self.TRANSL_RE.match(self.line) |
413 |
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
|
414 |
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
|
415 |
read = False |
385 | 416 |
continue |
417 |
m = self.TRANSL_EX_RE.match(self.line) |
|
418 |
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
|
419 |
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
|
420 |
read = False |
385 | 421 |
continue |
566
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
422 |
m = self.TRANSL_GLOS_RE.match(self.line) |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
423 |
if m is not None: |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
424 |
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
|
425 |
read = False |
0bba61492c37
Add syntax for glossary/explanation.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
565
diff
changeset
|
426 |
continue |
385 | 427 |
raise ParseException("""Uknown syntax...""") |
428 |
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
|
429 |
|
910efcf51ac0
Fix: regex doesn't match whole line. Add Parser representation to simplify debugging.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
593
diff
changeset
|
430 |
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
|
431 |
return u"<{:s}; line={:s}>".format(type(self).__name__, repr(self.line)) |