Skip to content
Snippets Groups Projects
model.py 11.3 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
Victor Zimmermann's avatar
Victor Zimmermann committed
import os
from typing import List

def check_format(json_file, check_for=dict):

    if isinstance(json_file, check_for):
Victor Zimmermann's avatar
Victor Zimmermann committed
        return json_file
    elif isinstance(json_file, str):
        if os.path.exists(json_file):
            with open(json_file, 'r') as jf:
                return json.load(jf)
Victor Zimmermann's avatar
Victor Zimmermann committed
    else:
        raise TypeError('Input not convertible.')
def from_json(json_file):

    if hasattr(json_file, 'read'):

        verses = json.loads(json_file.read())

    elif isinstance(json_file, str) and os.path.exists(json_file):

        verses = json.loads(open(json_file).read())

    else:

        TypeError('Input not convertible.')

    return [Verse.from_json(verse) for verse in verses]


def minimal(full_dict:dict):
    #print(full_dict)
    result_dict = dict()
    for key,value in full_dict.items():
        if value == {}:
            pass
        elif isinstance(value, dict):
            result_dict.update({key:minimal(value)})
        elif value != None:
            result_dict.update({key:value})
        else:
            pass
    #print(result_dict)
    return result_dict

class Syllable:

    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):
Victor Zimmermann's avatar
Victor Zimmermann committed
        raw = check_format(json_file)
        span = raw['span']
        text = raw['syllable']
        syllable_length = raw['syllable_length']
        vowel_length = raw['vowel_length']
        syllable = cls(text, span, idx, syllable_length, vowel_length)
            syllable.phenomena = dict()
            for phenomenon in raw['phenomena'].items():
                syllable.phenomena[phenomenon[0]] = Phenomenon.from_json(phenomenon[1])
Victor Zimmermann's avatar
Victor Zimmermann committed
        return syllable
        features.update({'id':self.id})
        features.update({'span':self.span})
        features.update({'syllable':self.text})
        features.update({'syllable_length':self.syllable_length})
        features.update({'vowel_length':self.vowel_length})
        features.update({'phenomena': minimal({key:value.to_dict() for key,value in self.phenomena.items()}) })

        return minimal(features)

        return json.dumps(self.to_dict())
class Phenomenon:
    def __init__(self, caused_by=None, overruled_by=None,
Victor Zimmermann's avatar
Victor Zimmermann committed
                 chars=None, typus=None, omitted=None):
        self.caused_by = caused_by
        self.overruled_by = overruled_by
Victor Zimmermann's avatar
Victor Zimmermann committed
        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):
Victor Zimmermann's avatar
Victor Zimmermann committed
        raw = check_format(json_file)
        phenomenon = cls()
        if 'caused_by' in raw:
            phenomenon.caused_by = raw['caused_by']
        if 'overruled_by' in raw:
            phenomenon.overruled_by = raw['overruled_by']
        if 'chars' in raw:
            phenomenon.chars = raw['chars']
        if 'typus' in raw:
            phenomenon.typus = raw['typus']
        if 'omitted' in raw:
            phenomenon.omitted = raw['omitted']
        if self.caused_by != None:
            features.update({'caused_by':self.caused_by})
        if self.overruled_by != None:
            features.update({'overruled_by':self.overruled_by})
        if self.chars != None:
            features.update({'chars':self.chars})
        if self.typus != None:
            features.update({'typus':self.typus})
        if self.omitted != None:
            features.update({'omitted':self.omitted})
        return json.dumps(self.to_dict())

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):
Victor Zimmermann's avatar
Victor Zimmermann committed
        raw = check_format(json_file)
        beginning = raw['beginning']
        end = raw['end']
        phenomenon = cls(beginning, end)
        if 'caused_by' in raw:
            phenomenon.caused_by = raw['caused_by']
        if 'overruled_by' in raw:
            phenomenon.overruled_by = raw['overruled_by']
        if 'chars' in raw:
            phenomenon.chars = raw['chars']
        if 'typus' in raw:
            phenomenon.typus = raw['typus']
        if 'omitted' in raw:
            phenomenon.omitted = raw['omitted']
        features.update({'beginning':self.beginning})
        features.update({'caused_by':self.end})
        if self.caused_by != None:
            features.update({'caused_by':self.caused_by})
        if self.overruled_by != None:
            features.update({'overruled_by':self.overruled_by})
        if self.chars != None:
            features.update({'chars':self.chars})
        if self.typus != None:
            features.update({'typus':self.typus})
        if self.omitted != None:
            features.update({'omitted':self.omitted})
        return json.dumps(self.to_dict())

class Token:

    def __init__(self, token: str, span: List[int],
                 syllables: List[Syllable] = list(),
                 analysis: str = None, clitic: str = None):
        if len(token) != span[1]-span[0]:
            raise ValueError('Length of token {} does not match span {}.'
                             .format(token, span))
        else:
            self.text = token
            self.span = span
            self.syllables = syllables
            self.analysis = analysis
            self.clitic = clitic
Victor Zimmermann's avatar
Victor Zimmermann committed
    @classmethod
    def from_json(cls, json_file):
        raw = check_format(json_file)
        # self is undefined
Victor Zimmermann's avatar
Victor Zimmermann committed
        text = raw['token']
        span = raw['span']
        if 'clitic' in raw:
            token.clitic = raw['clitic']
            
            token.syllables = list()
            for syllable in raw['syllables']:
                token.syllables.append(Syllable.from_json(syllable))
        features.update({'token':self.text})
        features.update({'span':self.span})
        features.update({'clitic':self.span})
        
        if self.syllables:
            features.update({'syllables': [syllable.to_dict() for syllable in self.syllables]})
    def to_json(self):
        return json.dumps(self.to_dict())
    def is_punct(self):
        return bool(re.match('^[\W_]+$', self.text))

Simon Will's avatar
Simon Will committed
    def __str__(self):
        return self.text

    def __repr__(self):
        return ('Token(token={}, span={}, syllables={})'
Simon Will's avatar
Simon Will committed
                .format(self.text, self.span, self.syllables))


    def __init__(self, tokens: List[Token], phenomena: dict = dict()):
        self.tokens = tokens
        self.phenomena = phenomena

    @classmethod
    def from_json(cls, json_file):
        raw = check_format(json_file)
Victor Zimmermann's avatar
Victor Zimmermann committed
        tokens = list()
        for token in raw["tokens"]:
            # self is undefined
Victor Zimmermann's avatar
Victor Zimmermann committed
            tokens.append(Token.from_json(token))
        reading = cls(tokens)
        if 'phenomena' in raw:
            reading.phenomena = dict()
            for phenomenon in raw['phenomena'].items():
                key, value = phenomenon
                for v in value:
                    if key in reading.phenomena:
                        reading.phenomena[key].append(MultisyllablePhenomenon.from_json(v))
                    else:
                        reading.phenomena[key] = [MultisyllablePhenomenon.from_json(v)]

        return reading
        features.update({'tokens': [token.to_dict() for token in self.tokens]})

        phenomena = {key:[minimal(v.to_dict()) for v in value] for key,value in self.phenomena.items()}
        features.update({'phenomena': phenomena})

        return minimal(features)

        return json.dumps(self.to_dict())
    def __init__(self, verse: str, readings: List[Reading] = list(),
                 source: dict = None):
        self.source = source
        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']
        source = dict()
        source['author'] = raw['source']['author']
        source['work'] = raw['source']['work']
        source['place'] = raw['source']['place']
        verse = cls(text, source=source)
        verse.readings = list()
        for reading in raw['readings']:
            verse.readings.append(Reading.from_json(reading))
        return verse
        features.update({'verse':self.text})
        features.update({'source':self.source})
        features.update({'readings': [reading.to_dict() for reading in self.readings]})
        return json.dumps(self.to_dict())