phraseology_generator.py 6.44 KB
# -*- coding: utf-8 -*-

from dictionary.models import sort_arguments, sort_positions, sortatributes
from settings import MORFEUSZ2

def lexicalisation(argument, subj, base, negativity):
    b = argument.type
    if b == 'fixed':
        return (get_words(sortatributes(argument)[-1]), [])
    attributes = sortatributes(argument)
    lexicalisation_type = attributes[0].values.all()[0].argument.type
    lexicalisation_parameters = sortatributes(attributes[0].values.all()[0].argument)
    if lexicalisation_type == 'xp': # xp(...)[np/prepnp], ...
       lexicalisation_type = lexicalisation_parameters[0].values.all()[0].argument.type
       lexicalisation_parameters = sortatributes(lexicalisation_parameters[0].values.all()[0].argument)
    if lexicalisation_type == 'np': # np(case), number, nouns, atr
        nps = get_nps(get_case(lexicalisation_parameters[0], subj, negativity), get_number(attributes[1], subj), get_words(attributes[2]), attributes[3])
        return (nps, get_verb(base, get_number(attributes[1], subj), subj))
    elif lexicalisation_type == 'prepnp': #prepnp(prep, case), number, nouns, atr
        prepnps = get_prepnps(get_preposition(lexicalisation_parameters[0]), get_case(lexicalisation_parameters[1], subj, negativity), get_number(attributes[1], subj), get_words(attributes[2]), attributes[3])
        return (prepnps, [])
    elif lexicalisation_type == 'adjp': # adjp(case), number, gender, degree, adjectives, atr
        adjps = get_adjps(get_case(lexicalisation_parameters[0], subj, negativity), get_number(attributes[1], subj), get_gender(attributes[2]), get_degree(attributes[3]), get_words(attributes[4]), attributes[5])
        return (adjps, get_verb(base, get_number(attributes[1], subj), subj))
    elif lexicalisation_type == 'prepadjp': #prepadjp(prep, case), number, gender, degree, adjectives, atr
        prepadjps = get_prepadjps(get_preposition(lexicalisation_parameters[0]), get_case(lexicalisation_parameters[1], subj, False), get_number(attributes[1], subj), get_gender(attributes[2]), get_degree(attributes[3]), get_words(attributes[4]), attributes[5])
        return (prepadjps, [])
    else:
        return ([], [])
    return ([], [])

def is_subj(categories):
    for cat in categories:
        if cat.category == u'subj':
            return True
    return False

def get_preposition(attribute):
    return attribute.values.all()[0].parameter.type.name

def get_numerals(attribute):
    return get_words(attribute)

def get_words(attribute):
    words = [word.text[1:-1] for word in attribute.values.all()]
    return words

def get_case(attribute, is_subj, negativity):
    case = attribute.values.all()[0].parameter.type.name
    if case == u'str':
        if is_subj:
            case = u'nom'
        elif negativity:
            case = u'gen'
        else:
            case = u'acc'
    return case

def get_number(attribute, is_subj):
    number = attribute.values.all()[0].parameter.type.name
    if number == u'_':
        if is_subj:
            number = u'sg'
    return number

def get_gender(attribute):
    gender = attribute.values.all()[0].parameter.type.name
    if gender == u'_':
        gender = u'n'
    if gender == u'n':
        gender = u'n1'
    if gender == u'm':
        gender = u'm1'
    return gender

def get_degree(attribute):
    degree = attribute.values.all()[0].parameter.type.name
    if degree == u'_':
        degree = u'pos'
    return degree

def get_nps(case, number, nouns, _atr):
    result = []
    for noun in nouns:
        options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(noun.encode('utf8'))]
        if case != u'_':
            filtered = []
            for option in options:
                (orth, tag) = option
                if u':' + case + u':' in tag:
                    filtered.append(option)
            options = filtered
        if number != u'_':
            filtered = []
            for option in options:
                (orth, tag) = option
                if u':' + number + u':' in tag:
                    filtered.append(option)
            options = filtered
        result += options
    return [orth for orth, _ in result]

def get_prepnps(prep, case, number, nouns, _atr):
    # ala["ma"] = kot
    nps = get_nps(case, number, nouns, _atr)
    return [prep + ' ' + np for np in nps]

def get_adjps(case, number, gender, degree, adjectives, _atr):
    result = []
    for adjective in adjectives:
        options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(adjective.encode('utf8'))]
        filtered = []
        for option in options:
            (orth, tag) = option
            if u'adj:' in tag:
                filtered.append(option)
        options = filtered
        if case != u'_':
            filtered = []
            for option in options:
                (orth, tag) = option
                if u':' + case + u':' in tag:
                    filtered.append(option)
            options = filtered
        if number != u'_':
            filtered = []
            for option in options:
                (orth, tag) = option
                if u':' + number + u':' in tag:
                    filtered.append(option)
            options = filtered
        if gender != u'_':
            filtered = []
            for option in options:
                (orth, tag) = option
                if u':' + gender + u':' in tag or  u'.' + gender + u':' in tag or  u':' + gender + u'.' in tag or  u'.' + gender + u'.' in tag:
                    filtered.append(option)
            options = filtered
        if degree != u'_':
            filtered = []
            for option in options:
                (orth, tag) = option
                if u':' + degree in tag:
                    filtered.append(option)
            options = filtered
        result += options
    return [orth for orth, _ in result]

def get_prepadjps(prep, case, number, gender, degree, adjectives, _atr):
     adjps = get_adjps(case, number, gender, degree, adjectives, _atr)
     return [prep + ' ' + adjp for adjp in adjps]

def get_verb(inf, number, is_subj):
    if not is_subj:
        return None
    else:
        options = [(interp.orth, interp.getTag(MORFEUSZ2)) for interp in MORFEUSZ2.generate(inf.encode('utf8'))]
        filtered = []
        for option in options:
            (orth, tag) = option
            if u'fin:' in tag and u':' + number + ':' in tag and u':ter:' in tag:
                filtered.append(option)
        options = filtered
        return [orth for orth, _ in options]