author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Sun, 27 Mar 2016 22:55:59 +0300 | |
changeset 400 | aa03182d2e26 |
parent 399 | a6a7036f3c6f |
child 402 | b47698d5ccab |
permissions | -rw-r--r-- |
385 | 1 |
|
2 |
import regex |
|
3 |
||
4 |
||
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
5 |
class ParseException(BaseException): |
385 | 6 |
|
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
7 |
def __init__(self, msg, lineno = None, line = None): |
385 | 8 |
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
|
9 |
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
|
10 |
self.line = line |
385 | 11 |
|
12 |
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
|
13 |
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
|
14 |
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
|
15 |
elif self.line is None: |
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
16 |
return ":{:d}:{:s}".format(self.lineno, self.msg) |
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
17 |
else: |
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
18 |
return ":{:d}: {:s}\nLINE: {:s}".format(self.lineno, self.msg, self.line) |
385 | 19 |
|
20 |
||
21 |
class Parser: |
|
22 |
||
23 |
SEPARATOR_RE = regex.compile(r"^__$") |
|
24 |
HEADWORD_RE = regex.compile(r"^(\p{L}.*)$") |
|
25 |
HEADWORD_VAR_RE = regex.compile(r"^ +(s|pl|v[123]|male|female|comp|super)$") |
|
26 |
HEADWORD_PRON_RE = regex.compile(r"^ +\[([\p{L}' ]+)\]$") |
|
27 |
TRANSL_POS_RE = regex.compile(r"^n|pron|adj|v|adv|prep|conj|num|int|phr\.v|abbr$") |
|
28 |
TRANSL_RE = regex.compile(r"^(ru|uk|la|en): ([\p{L}(][\p{L}\p{P}~ ]*)$") |
|
29 |
TRANSL_EX_RE = regex.compile(r"^(ru|uk|la|en)> (\p{L}.*)$") |
|
30 |
||
31 |
TRAILING_SPACES_RE = regex.compile(r"\p{Z}+$") |
|
32 |
||
33 |
def __init__(self): |
|
34 |
pass |
|
35 |
||
36 |
def readline(self): |
|
37 |
self.line = self.stream.readline() |
|
38 |
self.eof = len(self.line) == 0 |
|
39 |
if not self.eof: |
|
40 |
self.lineno += 1 |
|
400
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
41 |
if self.TRAILING_SPACES_RE.search(self.line): |
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
42 |
raise ParseException("Traling spaces detected...\n") |
385 | 43 |
|
44 |
def parse(self, stream): |
|
45 |
self.lineno = 0 |
|
46 |
self.stream = stream |
|
47 |
self.dom = [] |
|
48 |
try: |
|
49 |
self.parse_prelude() |
|
50 |
while not self.eof: |
|
51 |
self.parse_article() |
|
52 |
except ParseException as ex: |
|
399
a6a7036f3c6f
File name is not available in parser. Move error printing to writer.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
394
diff
changeset
|
53 |
raise ParseException(ex.msg, self.lineno, self.line) from ex |
385 | 54 |
return self.dom |
55 |
||
56 |
def parse_prelude(self): |
|
57 |
"""Read dictionary prelude until first "__" delimiter.""" |
|
58 |
while True: |
|
59 |
self.readline() |
|
60 |
if self.eof: |
|
61 |
raise ParseException("There are no articles...") |
|
62 |
if self.SEPARATOR_RE.match(self.line): |
|
63 |
break |
|
64 |
||
65 |
def parse_article(self): |
|
66 |
"""Try to match article until next "__" delimiter. Assume that `self.line` point to "__" delimiter.""" |
|
67 |
self.words = None |
|
68 |
self.tran = None |
|
69 |
self.parse_empty_line() |
|
70 |
self.parse_headlines() |
|
71 |
self.parse_translation() |
|
72 |
self.dom.append((self.words, self.tran)) |
|
73 |
||
74 |
def parse_empty_line(self): |
|
75 |
self.readline() |
|
76 |
if self.eof or len(self.line) != 1: |
|
77 |
raise ParseException(""""__" delimiter should followed by empty line...""") |
|
78 |
||
79 |
def parse_headlines(self): |
|
80 |
"""Try to match word variations with attributed. Assume that `self.line` on preceding empty line.""" |
|
81 |
self.words = {} |
|
82 |
self.readline() |
|
83 |
if self.eof: |
|
84 |
raise ParseException("""There are no definition after "__" delimiter...""") |
|
85 |
m = self.HEADWORD_RE.match(self.line) |
|
86 |
if m is None: |
|
87 |
raise ParseException("""There are no headword after "__" delimiter...""") |
|
88 |
word = m.group(1) |
|
89 |
pron = None |
|
90 |
attrs = set() |
|
91 |
while True: |
|
92 |
self.readline() |
|
93 |
if self.eof or len(self.line) == 1: |
|
94 |
break |
|
95 |
m = self.HEADWORD_RE.match(self.line) |
|
96 |
if m is not None: |
|
97 |
if word is None: |
|
98 |
raise ParseException("""Didn't match previous headword...""") |
|
99 |
self.words[word] = (pron, attrs) |
|
100 |
word = m.group(1) |
|
101 |
pron = None |
|
102 |
attrs = set() |
|
103 |
continue |
|
104 |
m = self.HEADWORD_PRON_RE.match(self.line) |
|
105 |
if m is not None: |
|
106 |
if pron is not None: |
|
107 |
raise ParseException("""Pronunciation is redefined...""") |
|
108 |
pron = m.group(1) |
|
109 |
continue |
|
110 |
m = self.HEADWORD_VAR_RE.match(self.line) |
|
111 |
if m is not None: |
|
112 |
attrs.add(m.group(1)) |
|
113 |
continue |
|
400
aa03182d2e26
Proper check for trailing spaces.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
399
diff
changeset
|
114 |
raise ParseException("""Line is not a headword or translation or headword attribute...""") |
385 | 115 |
self.words[word] = (pron, attrs) |
116 |
||
117 |
def parse_translation(self): |
|
118 |
senses = [] |
|
119 |
pos = None |
|
120 |
tr = [] |
|
121 |
ex = [] |
|
122 |
while True: |
|
123 |
self.readline() |
|
124 |
if self.eof: |
|
125 |
break |
|
126 |
m = self.SEPARATOR_RE.match(self.line) |
|
127 |
if m is not None: |
|
128 |
break |
|
129 |
if len(self.line) == 1: |
|
130 |
senses.append((pos, tr, ex)) |
|
131 |
pos = None |
|
132 |
tr = [] |
|
133 |
ex = [] |
|
134 |
continue |
|
135 |
m = self.TRANSL_POS_RE.match(self.line) |
|
136 |
if m is not None: |
|
137 |
if pos is not None: |
|
138 |
raise ParseException("""Each translation should have only one part of speech marker...""") |
|
139 |
pos = m.group(0) |
|
140 |
continue |
|
141 |
m = self.TRANSL_RE.match(self.line) |
|
142 |
if m is not None: |
|
143 |
tr.append((m.group(1), m.group(2))) |
|
144 |
continue |
|
145 |
m = self.TRANSL_EX_RE.match(self.line) |
|
146 |
if m is not None: |
|
147 |
ex.append((m.group(1), m.group(2))) |
|
148 |
continue |
|
149 |
raise ParseException("""Uknown syntax...""") |
|
150 |
if len(tr) > 0: |
|
151 |
senses.append((pos, tr, ex)) |
|
152 |
self.tran = senses |
|
153 |