Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
if isinstance(json_file, dict):
elif isinstance(json_file, str):
if os.path.exists(json_file):
with open(json_file, 'r') as jf:
return json.load(jf)
else:
return json.loads(json_file)
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 'phenomena' in raw:
for phenomenon in raw['phenomena'].items():
syllable.phenomena[phenomenon[0]] = Phenomenon.from_json(phenomenon[1])
def __init__(self, caused_by=None, overruled_by=None,
self.caused_by = caused_by
self.overruled_by = overruled_by
self.chars = chars
self.typus = typus
self.omitted = omitted
#@classmethod
#def positional_lengthening(cls, chars: str, caused_by=None,
#overruled_by=None):
#phenomenon = cls('positional lengthening', caused_by, overruled_by)
#phenomenon.chars = chars
#return phenomenon
#@classmethod
#def iambic_shortening(cls, typus: str, caused_by=None, overruled_by=None):
#phenomenon = cls('iambic shortening', caused_by, overruled_by)
#phenomenon.typus = typus
#return phenomenon
#@classmethod
#def s_elision(cls, caused_by=None, overruled_by=None):
#phenomenon = cls('s-elision', caused_by, overruled_by)
#phenomenon.omitted = 's'
#return phenomenon
#@classmethod
#def verse_end(cls, caused_by=None, overruled_by=None):
#phenomenon = cls('verse end', caused_by, overruled_by)
#return phenomenon
@classmethod
def from_json(cls, json_file):
phenomenon = cls()
if 'caused_by' in raw:
cls.caused_by = raw['caused_by']
if 'overruled_by' in raw:
cls.overruled_by = raw['overruled_by']
if 'chars' in raw:
cls.chars = raw['chars']
if 'typus' in raw:
cls.typus = raw['typus']
if 'omitted' in raw:
cls.omitted = raw['omitted']
return phenomenon
class MultisyllablePhenomenon(Phenomenon):
def __init__(self, beginning:int, end:int, caused_by=None,
overruled_by=None, chars=None, typus=None, omitted=None):
Phenomenon.__init__(self, caused_by, overruled_by,
chars, typus, omitted)
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):
beginning = raw['beginning']
end = raw['end']
phenomenon = cls(beginning, end)
if 'caused_by' in raw:
cls.caused_by = raw['caused_by']
if 'overruled_by' in raw:
cls.overruled_by = raw['overruled_by']
if 'chars' in raw:
cls.chars = raw['chars']
if 'typus' in raw:
cls.typus = raw['typus']
if 'omitted' in raw:
cls.omitted = raw['omitted']
return phenomenon
class Token:
def __init__(self, token: str, span: List[int],
syllables: List[Syllable] = list()):
raise ValueError('Token length does not match token span for token "{}".'.format(token))
else:
self.text = token
self.span = span
self.syllables = syllables
token = cls(text, span)
if 'syllables' in raw:
for syllable in raw['syllables']:
token.syllables.append(Syllable.from_json(syllable))
return token
else:
return token
def to_json():
pass
class Reading:
def __init__(self, tokens: List[Token], phenomena: List[Phenomenon] = list()):
self.phenomena = phenomena
@classmethod
def from_json(cls, json_file):
raw = check_format(json_file)
for token in raw["tokens"]:
# self is undefined
reading = cls(tokens)
if 'phenomena' in raw:
for phenomenon in raw['phenomena'].items():
reading.phenomena[phenomenon[0]] = Phenomenon.from_json(phenomenon[1])
def to_json():
pass
class Verse:
def __init__(self, verse: str, author: str = None,
work: str = None, place: str = None,
readings: List[Reading] = list()):
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