Few new words.
"""
gadict dictionary format parser.
"""
import regex
class Prelude:
"""Dictionary metainfo structure."""
name = None
about = ""
urls = []
authors = []
licences = []
class ParseException(BaseException):
def __init__(self, msg, lineno=None, line=None):
super().__init__()
self.msg = msg
self.lineno = lineno
self.line = line
def __repr__(self):
if self.lineno is None:
return self.msg
elif self.line is None:
return ":{:d}:{:s}".format(self.lineno, self.msg)
else:
return ":{:d}: {:s}\nLINE: {:s}".format(self.lineno, self.msg, self.line)
class Parser:
"""gadict dictionary format parser."""
COMMENT_RE = regex.compile(r"^# ")
SEPARATOR_RE = regex.compile(r"^__$")
HEADWORD_RE = regex.compile(r"^(\p{L}.*)$")
HEADWORD_VAR_RE = regex.compile(r"^ +(s|pl|v[123]|male|female|comp|super|abbr)$")
HEADWORD_PRON_RE = regex.compile(r"^ +\[([\p{L}' ]+)\]$")
TRANSL_POS_RE = regex.compile(r"^n|pron|adj|v|adv|prep|conj|num|int|phr|phr\.v|abbr|prefix$")
TRANSL_RE = regex.compile(r"^(ru|uk|la|en): ([\p{L}(].*)$")
TRANSL_EX_RE = regex.compile(r"^(ru|uk|la|en)> ([-\p{L}].*)$")
TOPIC_RE = regex.compile(r"^(topic|ant|syn): (\p{L}.*)$")
CONT_RE = regex.compile(r"^ +(.*)")
TRAILING_SPACES_RE = regex.compile(r"\p{Z}+$")
PRELUDE_NAME_RE = regex.compile(r"^name: (.*)")
PRELUDE_URL_RE = regex.compile(r"^url: (.*)")
PRELUDE_AUTHOR_RE = regex.compile(r"^by: (.*)")
PRELUDE_LICENSE_RE = regex.compile(r"^term: (.*)")
PRELUDE_ABOUT_RE = regex.compile(r"^about: ?(.*)")
def __init__(self):
pass
def readline(self):
while True:
self.line = self.stream.readline()
self.eof = len(self.line) == 0
if not self.eof:
self.lineno += 1
if self.TRAILING_SPACES_RE.search(self.line):
raise ParseException("Traling spaces detected...\n")
if self.COMMENT_RE.search(self.line):
continue
break
def parse(self, stream):
self.lineno = 0
self.stream = stream
self.dom = []
try:
self.parse_prelude()
while not self.eof:
self.parse_article()
except ParseException as ex:
raise ParseException(ex.msg, self.lineno, self.line)
return self.dom
def parse_prelude_continuation(self):
string = ""
while True:
self.readline()
if self.eof:
return string
m = self.CONT_RE.match(self.line)
if m is not None:
string += "\n" + m.group(1)
elif len(self.line) == 1:
string += "\n"
else:
return string
def parse_prelude(self):
"""Read dictionary prelude until first "__" delimiter."""
pre = Prelude()
while True:
self.readline()
if self.eof:
raise ParseException("There are no articles...")
m = self.PRELUDE_ABOUT_RE.match(self.line)
if m:
pre.about += m.group(1) + self.parse_prelude_continuation()
if self.eof:
raise ParseException("There are no articles...")
if self.SEPARATOR_RE.match(self.line):
break
m = self.PRELUDE_NAME_RE.match(self.line)
if m:
pre.name = m.group(1)
continue
m = self.PRELUDE_URL_RE.match(self.line)
if m:
pre.urls.append(m.group(1))
continue
m = self.PRELUDE_AUTHOR_RE.match(self.line)
if m:
pre.authors.append(m.group(1))
continue
m = self.PRELUDE_LICENSE_RE.match(self.line)
if m:
pre.licences.append(m.group(1))
continue
self.dom.append(pre)
def parse_article(self):
"""Try to match article until next "__" delimiter. Assume that `self.line` point to "__" delimiter."""
self.words = None
self.tran = None
self.parse_empty_line()
self.parse_headlines()
self.parse_translation()
self.dom.append((self.words, self.tran))
def parse_empty_line(self):
self.readline()
if self.eof or len(self.line) != 1:
raise ParseException(""""__" delimiter should followed by empty line...""")
def parse_headlines(self):
"""Try to match word variations with attributed. Assume that `self.line` on preceding empty line."""
self.words = {}
self.readline()
if self.eof:
raise ParseException("""There are no definition after "__" delimiter...""")
m = self.HEADWORD_RE.match(self.line)
if m is None:
raise ParseException("""There are no headword after "__" delimiter...""")
word = m.group(1)
pron = None
attrs = set()
while True:
self.readline()
if self.eof or len(self.line) == 1:
break
m = self.HEADWORD_RE.match(self.line)
if m is not None:
if word is None:
raise ParseException("""Didn't match previous headword...""")
self.words[word] = (pron, attrs)
word = m.group(1)
pron = None
attrs = set()
continue
m = self.HEADWORD_PRON_RE.match(self.line)
if m is not None:
if pron is not None:
raise ParseException("""Pronunciation is redefined...""")
pron = m.group(1)
continue
m = self.HEADWORD_VAR_RE.match(self.line)
if m is not None:
attrs.add(m.group(1))
continue
raise ParseException("""Line is not a headword or translation or headword attribute...""")
self.words[word] = (pron, attrs)
def parse_translation_continuation(self):
string = ""
while True:
self.readline()
if self.eof:
return string
m = self.CONT_RE.match(self.line)
if m is not None:
string += "\n" + m.group(1)
else:
return string
def parse_translation(self):
senses = []
pos = None
tr = []
ex = []
read = True
while True:
if read:
self.readline()
read = True
if self.eof:
break
m = self.SEPARATOR_RE.match(self.line)
if m is not None:
break
if len(self.line) == 1:
senses.append((pos, tr, ex))
pos = None
tr = []
ex = []
continue
m = self.TRANSL_POS_RE.match(self.line)
if m is not None:
if pos is not None:
raise ParseException("""Each translation should have only one part of speech marker...""")
pos = m.group(0)
continue
m = self.TOPIC_RE.match(self.line)
if m is not None:
# TODO
continue
m = self.TRANSL_RE.match(self.line)
if m is not None:
tr.append((m.group(1), m.group(2) + self.parse_translation_continuation()))
read = False
continue
m = self.TRANSL_EX_RE.match(self.line)
if m is not None:
ex.append((m.group(1), m.group(2) + self.parse_translation_continuation()))
read = False
continue
raise ParseException("""Uknown syntax...""")
if len(tr) > 0:
senses.append((pos, tr, ex))
self.tran = senses