frame_compare_12.py 2.55 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

import sys, os, codecs
from collections import defaultdict
from copy import deepcopy

from django.core.management.base import BaseCommand

from semantics.models import SemanticFrame

from semantics.management.commands.frame_compare_modules.frame import Frame, LexicalUnit, SelectionalPreference
from semantics.management.commands.frame_compare_modules.match_frames import match_frames, match_frames_diagonal
from semantics.management.commands.frame_compare_modules.local_db import get_db_data

from settings import PROJECT_PATH

BUNDLE_SIZE = 50
PROCESSES_NO = 16
PROCESS_ID = 12

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

    def handle(self, **options):
        # SPLIT FRAMES IN BUNLES
        print "splitting frames in bundles..."
        frames = SemanticFrame.objects.filter(next=None, removed=False, entry__isnull=False).order_by('id')
        frame_bundles = []
        frame_bundle = []
        i = 0 # frame_bundle size
        for frame in frames:
            if frame.entry.actual_lemma().status.priority >= 90: # (S)ready / (S)validated
                lus = frame.lexical_units.all()
                if len(lus) > 0 and\
                   max([lu.luid for lu in lus]) >=0:
                    frame_bundle.append(frame)
                    i += 1
                    if i == BUNDLE_SIZE:
                        frame_bundles.append(frame_bundle)
                        frame_bundle = []
                        i = 0
        if i != 0:
            frame_bundles.append(frame_bundle)
        print "   ...done"
            
        # LOCAL DATABASE CONNECTION
        session, TT_dict = get_db_data()
        LexicalUnit._session = session
        SelectionalPreference._session = session

        c = 0
        for i in range(len(frame_bundles)):
            for j in range(i+1):
                if c % PROCESSES_NO == PROCESS_ID:
                    print i, j
                    frames1 = []
                    for frame in frame_bundles[i]:
                        f = Frame.from_slowal(frame)
                        frames1.append(f)
                    if i == j:
                        match_frames_diagonal(frames1, session, TT_dict)# , verbose=True, fake=True)
                    else:
                        frames2 = []
                        for frame in frame_bundles[j]:
                            f = Frame.from_slowal(frame)
                            frames2.append(f)
                        match_frames(frames1, frames2, session, TT_dict)# , verbose=True, fake=True)
                c += 1