translations.py 4.26 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from django.core.management.base import BaseCommand

from semantics.management.commands.frame_grouping_files.shellvalier_db import getLuToFrameId, getFrameArgumentToArgumentId

from wordnet.models import LexicalUnit
from semantics.models import SemanticFrame

class Command(BaseCommand):
    args = 'none'
    help = ''

    def handle(self, **options):
        c = 0
        lu_to_frame_id = getLuToFrameId()
        frame_argument_to_argument_id = getFrameArgumentToArgumentId()

        pos_translate = {'czasownik': 'verb', 'przymiotnik': 'adj', 'rzeczownik': 'noun'}
        
        frame_slowal_id_to_unifier_id = {}
        for frame in SemanticFrame.objects.filter(next=None, removed=False).all():
            lus = frame.lexical_units.all()
            slowal_id = frame.id
            for lu in lus:
                lu_data = (lu.base, pos_translate[lu.pos], lu.sense)
                if lu_data not in lu_to_frame_id:
                    # first type of error -- lexical unit does not exist in unifier
                    # print "type 1", lu.base, lu.pos, lu.sense
                    # c += 1
                    pass
                else:
                    unifier_id = lu_to_frame_id[lu_data]
                    if slowal_id in frame_slowal_id_to_unifier_id:
                        if frame_slowal_id_to_unifier_id[slowal_id] != unifier_id:
                            # second type of error -- lexical units from the same frame connected to different frames // frame split in unifier
                            # print "type 2", lu.base, lu.pos, lu.sense
                            # c += 1
                            pass
                    else:
                        frame_slowal_id_to_unifier_id[slowal_id] = unifier_id
                        

        # thirt type of error -- lexical units from different frames connected to the same frame // frames joined in unifier
        # l = [(s, f) for f, s in frame_slowal_id_to_unifier_id.items()]
        # l1 = [s for f, s in frame_slowal_id_to_unifier_id.items()]
        # print len(l1)
        # print len(set(l1))
        # prev_wal = -1
        # prev_uni = -1
        # for s, f in sorted(l):
        #     if s == prev_uni:
        #         f1 = SemanticFrame.objects.get(id=prev_wal)
        #         f2 = SemanticFrame.objects.get(id=f)
        #         print "type3", f1.lexical_units.all(), f2.lexical_units.all()
        #         c+= 1
        #         pass
        #     prev_wal = f
        #     prev_uni = s

        # print c

        print len(frame_slowal_id_to_unifier_id)

        argument_slowal_id_to_unifier_id = {}
        ATTRIBUTES = ['Source', 'Goal', 'Foreground', 'Background']
        for frame in SemanticFrame.objects.filter(next=None, removed=False).all():
            if frame.id in frame_slowal_id_to_unifier_id:
                corresponding_frame_id = frame_slowal_id_to_unifier_id[frame.id]
                for argument in frame.complements.all():
                    roles = argument.roles.all()
                    if len(roles) == 1:
                        role_tag = (roles[0].role, None)
                    elif len(roles) == 2:
                        if roles[0].role in ATTRIBUTES:
                            role_tag = (roles[1].role, roles[0].role)
                        elif roles[1].role in ATTRIBUTES:
                            role_tag = (roles[0].role, roles[1].role)
                        else:
                            raise MultipleRolesError()
                    else:
                        raise RolesNumberError()
                    if argument.id not in argument_slowal_id_to_unifier_id:
                        if role_tag in frame_argument_to_argument_id[corresponding_frame_id]:
                            argument_slowal_id_to_unifier_id[argument.id] = frame_argument_to_argument_id[corresponding_frame_id][role_tag]
                        else:
                            # 4th type of error -- changed or no longer existing role tag
                            print frame.lexical_units.all(), frame_argument_to_argument_id[corresponding_frame_id].keys(), role_tag
                            pass
                    else:
                        raise DoubleError()
                        
            else:
                pass # type 1 error