PhraseAttributes.py 10.9 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from syntax.models_phrase import *
from syntax.models import Position

def get_case(name, **kwargs):
    return Case.objects.get(name=name)

def get_prep(name, **kwargs):
    prep, _ = Preposition.objects.get_or_create(name=name)
    return prep

def get_comprep(name, **kwargs):
    prep, _ = ComplexPreposition.objects.get_or_create(name=name)
    return prep

def get_cptype(name, **kwargs):
    cptype, _ = CPType.objects.get_or_create(name=name)
    return cptype

def get_cpreals(names, **kwargs):
    cpreals = []
    if names:
        for name in names:
            cpreal, _ = CPRealisation.objects.get_or_create(name=name)
            cpreals.append(cpreal)
    return cpreals

def get_aspect(name, **kwargs):
    return PhraseAspect.objects.get(name=name)

def get_neg(name, **kwargs):
    return PhraseNegativity.objects.get(name=name)

def get_inhsie(name, **kwargs):
    return PhraseInherentSie.objects.get(name=name)

def get_advcat(name, **kwargs):
    return AdverbialCategory.objects.get(name=name)

def get_comparcat(name, **kwargs):
    comparcat, _ = ComparCategory.objects.get_or_create(name=name)
    return comparcat

def get_num(name, **kwargs):
    return Number.objects.get(name=name)

def get_gend(name, **kwargs):
    return Gender.objects.get(name=name)

def get_deg(name, **kwargs):
    return Degree.objects.get(name=name)

#def store_phrase(phrase):
#    dummy_postition = Position.objects.get(id=1)
#    return phrase.store(dummy_postition, dict())

# for compar
def get_lexes(phrases, stored_positions=None):
    #return [store_phrase(lex) for lex in lexes]
    return [lex.store(None, stored_positions) for lex in phrases]

# for lex(xp), lex(advp)
def get_lex(phrase, stored_positions=None):
    return phrase.store(None, stored_positions)

# for fixed phrases
def get_phrase(phrase, stored_positions=None):
    return phrase.store(None, stored_positions)

def get_text(text, **kwargs):
    return text.strip('\'')
    
#-------------------------------------------------------------------------------

'''
transformations = {
    'case'      : get_case,
    'prep'      : get_prep,
    'comprep'   : get_comprep,
    'cptype'    : get_cptype,
    'aspect'    : get_aspect,
    'neg'       : get_neg,
    'advcat'    : get_advcat,
    'comparcat' : get_comparcat,
    # lex
    'num'       : get_num,
}
'''

class AttributesHelper(object):
    
    # many-to-many fields can’t be initialised on model object creation
    def __init__(self, attrs_cls=None, lex_attrs_cls=None, attr_getters=None, m2m_attr_getters={}, lex_attr_getters=None, lex_m2m_attr_getters={}):
        self.attrs_cls = attrs_cls
        self.lex_attrs_cls = lex_attrs_cls
        self.attr_getters = attr_getters
        self.m2m_attr_getters = m2m_attr_getters
        self.lex_attr_getters = lex_attr_getters
        self.lex_m2m_attr_getters = lex_m2m_attr_getters

prepp_attr_getters = {
    'prep' : lambda x: x._prep._value,
    'case' : lambda x: x._prep._case._value,
}

attrs_helpers = {

    'np' : AttributesHelper(
        attrs_cls=NPAttributes,
        lex_attrs_cls=LexNPAttributes,
        attr_getters={
            'case' : lambda x: x._case._value,
        },
        lex_attr_getters={
            'num' : lambda x: x._number,
        }),
    'nump' : AttributesHelper(
        attrs_cls=NumPAttributes,
        attr_getters={
            'case' : lambda x: x._case._value,
        },
        #lex_attr_getters={
        #    'num' : lambda x: x._number,
        #}
        ),
    'adjp' : AttributesHelper(
        attrs_cls=AdjPAttributes,
        lex_attrs_cls=LexAdjPAttributes,
        attr_getters={
            'case' : lambda x: x._case._value,
        },
        lex_attr_getters={
            'num'  : lambda x: x._number,
            'gend' : lambda x: x._gender,
            'deg'  : lambda x: x._degree,
        }),
     'ppasp' : AttributesHelper(
        attrs_cls=PPasPAttributes,
        lex_attrs_cls=LexPPasPAttributes,
        attr_getters={
            'case' : lambda x: x._case._value,
        },
        lex_attr_getters={
            'num'  : lambda x: x._number,
            'gend' : lambda x: x._gender,
            'neg'  : lambda x: x._negativity,
        }),
     'pactp' : AttributesHelper(
        attrs_cls=PActPAttributes,
        lex_attrs_cls=LexPActPAttributes,
        attr_getters={
            'case' : lambda x: x._case._value,
        },
        lex_attr_getters={
            'num'  : lambda x: x._number,
            'gend' : lambda x: x._gender,
            'neg'  : lambda x: x._negativity,
            'inhsie' : lambda x: x._inherent_sie,
        }),
    'prepnp' : AttributesHelper(
        attrs_cls=PrepNPAttributes,
        lex_attrs_cls=LexPrepNPAttributes,
        attr_getters=prepp_attr_getters,
        lex_attr_getters={
            'num' : lambda x: x._number,
        }),
    'prepnump' : AttributesHelper(
        attrs_cls=PrepNumPAttributes,
        #lex_attrs_cls=LexPrepNumPAttributes,
        attr_getters=prepp_attr_getters,
        #lex_attr_getters={
        #    'num' : lambda x: x._number,
        #}
        ),
    'prepadjp' : AttributesHelper(
        attrs_cls=PrepAdjPAttributes,
        lex_attrs_cls=LexPrepAdjPAttributes,
        attr_getters=prepp_attr_getters,
        lex_attr_getters={
            'num'  : lambda x: x._number,
            'gend' : lambda x: x._gender,
            'deg'  : lambda x: x._degree,
        }),
    'prepppasp' : AttributesHelper(
        attrs_cls=PrepPPasPAttributes,
        lex_attrs_cls=LexPrepPPasPAttributes,
        attr_getters=prepp_attr_getters,
        lex_attr_getters={
            'num'  : lambda x: x._number,
            'gend' : lambda x: x._gender,
            'neg'  : lambda x: x._negativity,
        }),
    'prepgerp' : AttributesHelper(
        attrs_cls=PrepGerPAttributes,
        lex_attrs_cls=LexPrepGerPAttributes,
        attr_getters=prepp_attr_getters,
        lex_attr_getters={
            'num' : lambda x: x._number,
            'neg' : lambda x: x._negativity,
            'inhsie' : lambda x: x._inherent_sie,
        }),
    'comprepnp' : AttributesHelper(
        attrs_cls=ComPrepNPAttributes,
        attr_getters={
            'comprep' : lambda x: x._prep._value,
        }),
    'cp' : AttributesHelper(
        attrs_cls=CPAttributes,
        lex_attrs_cls=LexCPAttributes,
        attr_getters={
            'cptype' : lambda x: x._type._value,
        },
        lex_attr_getters={
            'neg' : lambda x: x._negativity,
            'inhsie' : lambda x: x._inherent_sie,
        },
        m2m_attr_getters={
            'cpreals' : lambda x: x._type._realisations,
        }),
    'ncp' : AttributesHelper(
        attrs_cls=NCPAttributes,
        lex_attrs_cls=LexNCPAttributes,
        attr_getters={
            'case' : lambda x: x._case._value,
            'cptype' : lambda x: x._type._value,
        },
        lex_attr_getters={
            'neg' : lambda x: x._negativity,
            'inhsie' : lambda x: x._inherent_sie,
        },
        m2m_attr_getters={
            'cpreals' : lambda x: x._type._realisations,
        }),
    'prepncp' : AttributesHelper(
        attrs_cls=PrepNCPAttributes,
        attr_getters={
            'prep' : lambda x: x._prep._value,
            'case' : lambda x: x._prep._case._value,
            'cptype' : lambda x: x._type._value,
        },
        m2m_attr_getters={
            'cpreals' : lambda x: x._type._realisations,
        }),
    'infp' : AttributesHelper(
        attrs_cls=InfPAttributes,
        lex_attrs_cls=LexInfPAttributes,
        attr_getters={
            'aspect' : lambda x: x._aspect._value,
        },
        lex_attr_getters={
            'neg' : lambda x: x._negativity,
            'inhsie' : lambda x: x._inherent_sie,
        }),
    'xp' : AttributesHelper(
        attrs_cls=XPAttributes,
        lex_attrs_cls=LexXPAttributes,
        attr_getters={
            'advcat' : lambda x: x._category._value,
        },
        lex_attr_getters={
            'lex' : lambda x: x._lex,
        }),
    'advp' : AttributesHelper(
        attrs_cls=AdvPAttributes,
        lex_attrs_cls=LexAdvPAttributes,
        attr_getters={
            'advcat' : lambda x: x._category._value,
        },
        lex_attr_getters={
            'deg'  : lambda x: x._degree,
        }),
    'compar' : AttributesHelper(
        attrs_cls=ComparAttributes,
        lex_attrs_cls=LexComparAttributes,
        attr_getters={
            'comparcat' : lambda x: x._category._value,
        },
        lex_attr_getters={},
        lex_m2m_attr_getters={
            'lexes' : lambda x: x._lexes,
        }),
     'fixed' : AttributesHelper(
        attrs_cls=FixedAttributes,
        attr_getters={
            'phrase' : lambda x: x._phrase,
            'text'   : lambda x: x._text,
        }),
}

'''
attrs_cls = {
    'np'        : NPAttributes,
    'adjp'      : NPAttributes,
    'prepnp'    : PrepNPAttributes,
    'prepadjp'  : PrepNPAttributes,
    'comprepnp' : ComPrepNPAttributes,
    'cp'        : CPAttributes,
    'ncp'       : NCPAttributes,
    'prepncp'   : PrepNCPAttributes,
    'infp'      : InfPAttributes,
    'xp'        : XPAttributes,
    'advp'      : XPAttributes,
    'compar'    : ComparAttributes,
}
'''

#-------------------------------------------------------------------------------

def empty_attributes():
    attr, _ = EmptyAttributes.objects.get_or_create(empty='_')
    return attr

stored_attributes = dict()

def do_get_attributes(phrase, stored_positions, lex):
    helper = attrs_helpers[phrase._name]
    cls = helper.lex_attrs_cls if lex else helper.attrs_cls
    attr_getters = helper.lex_attr_getters if lex else helper.attr_getters
    m2m_attr_getters = helper.lex_m2m_attr_getters if lex else helper.m2m_attr_getters
    kwargs = { key : eval('get_{}'.format(key))(getter(phrase), stored_positions=stored_positions) for key, getter in attr_getters.items() }
    m2m_attrs = { key : eval('get_{}'.format(key))(getter(phrase), stored_positions=stored_positions) for key, getter in m2m_attr_getters.items() }
    if cls is not None:
        if not m2m_attrs:
            # atomic attributes only – uniqueness of attr objects assured by get_or_create
            attrs, _ = cls.objects.get_or_create(**kwargs)
        else:
            # multi-valued attributes (m2m fields) – can’t use get_or_create to assure uniqueness
            attrs_key = str(sorted(kwargs.items()) + sorted(m2m_attrs.items()))
            if attrs_key in stored_attributes:
                attrs = stored_attributes[attrs_key]
            else:
                attrs = cls.objects.create(**kwargs)
                for key, values in m2m_attrs.items():
                    if values:
                        getattr(attrs, key).set(values)
                stored_attributes[attrs_key] = attrs
        return attrs
    return None

def get_attributes(phrase, stored_positions):
    return do_get_attributes(phrase, stored_positions, False)

def get_lex_attributes(phrase, stored_positions):
    return do_get_attributes(phrase, stored_positions, True)