clusterer.py 3.69 KB
#! /usr/bin/python
# -*- coding: utf-8 -*-

from local_db import get_db_data
from frame import LexicalUnit, SelectionalPreference
from group_frames import join_frames
from generate_xml import create_xml
from import_cluster import find_cluster

from semantics.models import SemanticFrame

import numpy as np

session, TT_dict = get_db_data()
LexicalUnit._session = session
SelectionalPreference._session = session

keys1 = np.matrix([[u'55841', u''], [u'64330', u''], [u'94702', u''],
                   [u'94130', u''], [u'94068', u''], [u'94709', u''],
                   [u'17561', u''], [u'94777', u''], [u'53406', u'']])

keys2 = np.matrix([[u'102918', u'Factor,0'],
                   [u'152460', u'Stimulus,1'],
                   [u'41616', u'Stimulus,1'],
                   [u'67156', u'Stimulus,0'],
                   [u'104086', u'Factor,0'],
                   [u'90008', u'Stimulus,0'],
                   [u'88730', u'Stimulus,0'],
                   [u'97820', u'Factor,0'],
                   [u'133150', u'Factor,0'],
                   [u'35105', u'Stimulus,1'],
                   [u'119586', u'Stimulus,1'],
                   [u'65187', u'Stimulus,0'],
                   [u'137252', u'Stimulus,1'],
                   [u'75815', u'Stimulus,0'],
                   [u'98473', u'Factor,0'],
                   [u'103211', u'Factor,0'],
                   [u'62127', u'Stimulus,0'],
                   [u'70961', u'Stimulus,1'],
                   [u'159284', u'Stimulus,0'],
                   [u'97205', u'Stimulus,0'],
                   [u'6070', u'Stimulus,1'],
                   [u'61625', u'Stimulus,0'],
                   [u'144187', u'Stimulus,0'],
                   [u'79166', u'Stimulus,0'],
                   [u'155967', u'Stimulus,0'],
                   [u'55362', u'Factor,0'],
                   [u'154294', u'Factor,0'],
                   [u'89746', u'Stimulus,0'],
                   [u'133167', u'Factor,0'],
                   [u'125130', u'Factor,0'],
                   [u'59470', u'Stimulus,0'],
                   [u'94929', u'Stimulus,1'],
                   [u'104146', u'Factor,0'],
                   [u'22868', u'Factor,0'],
                   [u'59479', u'Stimulus,0'],
                   [u'152666', u'Stimulus,0'],
                   [u'141019', u'Stimulus,1'],
                   [u'103772', u'Factor,0'],
                   [u'154205', u'Stimulus,1'],
                   [u'41822', u'Stimulus,0'],
                   [u'124895', u'Factor,0'],
                   [u'150266', u'Stimulus,0'],
                   [u'103777', u'Factor,0'],
                   [u'90082', u'Factor,0'],
                   [u'152675', u'Stimulus,0'],
                   [u'133354', u'Factor,0'],
                   [u'72815', u'Stimulus,0'],
                   [u'92018', u'Stimulus,0'],
                   [u'107561', u'Factor,0'],
                   [u'52088', u'Stimulus,1'],
                   [u'55289', u'Factor,0'],
                   [u'141090', u'Stimulus,1'],
                   [u'126335', u'Stimulus,0'],
                   [u'71167', u'Stimulus,0']])

    
def generate_xml(cluster_heart, xml_file):
    # prepare database
    session, TT_dict = get_db_data()
    LexicalUnit._session = session
    SelectionalPreference._session = session

    keys, transformation = find_cluster(cluster_heart, session, TT_dict)
    # keys, transformation = keys1, 'zero'
    # keys, transformation = keys2, 'fs'
    frames = []
    for i in list(keys2):
        frame_id = int(i[0,0])
        frame = SemanticFrame.objects.get(id=frame_id)
        transformation_id =  i[0,1]
        frames.append((frame, transformation_id))
    
    unified_frame, matching = join_frames(frames, transformation)

    create_xml(xml_file, unified_frame, matching)