Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def check_format(json_file):
if os.path.exists(json_file):
with open(json_file, 'r') as jf:
return json.load(jf)
elif isinstance(json_file, str):
return json_file
else:
raise TypeError('Input not convertible.')
def __init__(self, syllable: str, span: List[int], idx: int,
syllable_length: int, vowel_length: int,
phenomena: dict = dict()):
if len(syllable) != span[1] - span[0]:
raise ValueError('Syllable length does not match syllable span.')
else:
self.text = syllable
self.span = span
self.id = idx
self.syllable_length = syllable_length
self.vowel_length = vowel_length
self.phenomena = phenomena
@classmethod
def from_json(cls, json_file):
id = raw['id']
span = raw['span']
text = raw['syllable']
syllable_length = raw['syllable_length']
vowel_length = raw['vowel_length']
syllable = cls(text, span, id, syllable_length, vowel_length)
if raw['phenomena']:
for phenomenon in raw['phenomena']:
syllable.phenomena.add(Phenomenon.from_json(phenomenon))
return syllable
def __init__(self, name: str, caused_by=None, overruled_by=None,
self.name = name
self.caused_by = caused_by
self.overruled_by = overruled_by
self.chars = chars
self.typus = typus
self.omitted = omitted
def positional_lengthening(self, chars: str, caused_by=None,
overruled_by=None):
self.__init__(self, 'positional lengthening', caused_by, overruled_by)
def iambic_shortening(self, typus: str, caused_by=None, overruled_by=None):
self.__init__(self, 'iambic shortening', caused_by, overruled_by)
def s_elision(self, caused_by=None, overruled_by=None):
self.__init__(self, 's-elision', caused_by, overruled_by)
self.omitted = 's'
def verse_end(self, caused_by=None, overruled_by=None):
self.__init__(self, 'verse end', caused_by, overruled_by)
@classmethod
def from_json(cls, json_file):
raw = check_format(json_file)
pass
def to_json(self):
pass
class MultisyllablePhenomenon(Phenomenon):
def __init__(self, name: str, beginning: int, end: int,
caused_by=None, overruled_by=None):
Phenomenon.__init__(self, name, caused_by=None, overruled_by=None)
self.beginning = beginning
self.end = end
def apheresis(self, beginning, end, caused_by=None, overruled_by=None):
MultisyllablePhenomenon.__init__(self, 'apheresis', beginning, end,
caused_by, overruled_by)
def synizesis(self, beginning, end, caused_by=None, overruled_by=None):
MultisyllablePhenomenon.__init__(self, 'synizesis', beginning, end,
caused_by, overruled_by)
@classmethod
def from_json(cls, json_file):
raw = check_format(json_file)
pass
def to_json(self):
pass
class Token:
def __init__(self, token: str, span: List[int],
syllables: List[Syllable] = list):
if len(token) != span[1]-span[0]:
raise ValueError('Token length does not match token span.')
else:
self.text = token
self.span = span
self.syllables = syllables
text = raw['token']
span = raw['span']
syllables = list()
for syllable in raw['syllables']:
def to_json():
pass
class Reading:
def __init__(self, tokens: List[Token]):
self.tokens = tokens
@classmethod
def from_json(cls, json_file):
raw = check_format(json_file)
for token in raw["tokens"]:
# self is undefined
tokens.append(Token.from_json(token))
return cls(tokens)
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
def to_json():
pass
class Verse:
def __init__(self, verse: str, author: str = None,
work: str = None, place: str = None,
readings: List[Reading] = None):
self.verse = verse
self.author = author
self.work = work
self.place = place
self.readings = readings
@classmethod
def from_plain_verse(cls, plain_verse):
verse = cls(plain_verse)
# TODO: Generate readings.
pass
return verse
@classmethod
def from_json(cls, json_file):
raw = check_format(json_file)
text = raw['verse']
author = raw['source']['author']
work = raw['source']['work']
place = raw['source']['place']
verse = cls(text, author, work, place)
for reading in raw['readings']:
verse.readings.append(Reading.from_json(reading))
return verse
def to_json():
pass