clusterer.py
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/python
# -*- coding: utf-8 -*-
from local_db import get_db_data, FreeFrames
from frame import LexicalUnit, SelectionalPreference, Frame
from group_frames import join_frames
from generate_xml import create_xml
from import_cluster import find_cluster
from sqlalchemy import delete
from semantics.models import SemanticFrame
import numpy as np
import time
def generate_xml(cluster_heart, xml_file, time_log=[], timer_start=time.time()):
# prepare database
session, TT_dict = get_db_data()
LexicalUnit._session = session
SelectionalPreference._session = session
time_log.append(str(time.time() - timer_start))
keys, transformation = find_cluster(cluster_heart, session, TT_dict, time_log=time_log, timer_start=timer_start)
time_log.append(str(time.time() - timer_start))
frames = []
for i in list(keys):
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)
time_log.append(str(time.time() - timer_start))
create_xml(xml_file, unified_frame, matching)
change_frames_statuses([], keys)
def generate_empty_xml(xml_file):
create_xml(xml_file, Frame(), {})
def change_frames_statuses(freed, taken):
return 'ok'
if len(set(freed) & set(taken)) > 0:
return 'conflicting data'
else:
session, _ = get_db_data()
stmt = delete(FreeFrames).where(FreeFrames.frame_id.in_(taken))
session.execute(stmt)
new = [FreeFrames(frame_id=fid) for fid in freed]
session.bulk_save_objects(new)
session.commit()
return 'ok'