utils.py
5.56 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
def get_frames_differences(initial_frames, frames):
differences = {'matching_frames': [],
'part_matching_frames': [],
'missing_frames': []}
matching_frames = []
for ini_frame in initial_frames:
matching_frame = get_matching_frame(frames, ini_frame)
if matching_frame:
differences['matching_frames'].append(ini_frame)
matching_frames.append(matching_frame)
else:
differences['missing_frames'].append(ini_frame)
not_matched_frames = list(set(frames) - set(matching_frames))
for miss_frame in differences['missing_frames']:
part_matching_frames = get_partially_matching_frames(not_matched_frames, miss_frame)
if part_matching_frames:
not_matched_frames.remove(part_matching_frames[0])
differences['part_matching_frames'].append(miss_frame)
differences['missing_frames'] = list(set(differences['missing_frames']) - set(differences['part_matching_frames']))
return differences
def get_matching_frame(frames, frame_to_find):
for frame in frames.all():
if lexical_units_match(frame, frame_to_find) and frames_match(frame, frame_to_find):
return frame
return None
def lexical_units_match(frame1, frame2):
if set(frame1.lexical_units.all()) == set(frame2.lexical_units.all()):
return True
return False
def frames_match(frame1, frame2):
complements1 = frame1.complements
complements2 = frame2.complements
if complements1.count() == complements2.count():
for compl in complements1.all():
if not matching_complement_exists(complements2.all(), compl):
return False
else:
return False
return True
def matching_complement_exists(complements, compl_to_find):
for compl in complements:
if complements_match(compl, compl_to_find):
return True
return False
def complements_match(compl1, compl2):
if (roles_match(compl1.roles, compl2.roles) and
preference_match(compl1.selective_preference, compl2.selective_preference)):
return True
return False
def roles_match(roles1, roles2):
if set(roles1.all()) == set(roles2.all()):
return True
return False
def preference_match(sel_preference1, sel_preference2):
if sel_preference1 is None and sel_preference2 is None:
return True
elif((sel_preference1 is None and sel_preference2 is not None) or
(sel_preference1 is not None and sel_preference2 is None)):
return False
elif(general_sel_prefs_match(sel_preference1, sel_preference2) and
synset_sel_prefs_match(sel_preference1, sel_preference2) and
relation_sel_prefs_match(sel_preference1, sel_preference2) and
synset_rel_sel_prefs_match(sel_preference1, sel_preference2)):
return True
return False
def general_sel_prefs_match(sel_preference1, sel_preference2):
if set(sel_preference1.generals.all()) == set(sel_preference2.generals.all()):
return True
return False
def synset_sel_prefs_match(sel_preference1, sel_preference2):
if set(sel_preference1.synsets.all()) == set(sel_preference2.synsets.all()):
return True
return False
def relation_sel_prefs_match(sel_preference1, sel_preference2):
relations1 = sel_preference1.relations
relations2 = sel_preference2.relations
if relations1.count() == relations2.count():
for rel in relations1.all():
if not matching_relation_exists(rel, relations2):
return False
else:
return False
return True
def matching_relation_exists(rel_to_find, relations):
for rel in relations.filter(relation=rel_to_find.relation):
if complements_match(rel_to_find.to, rel.to):
return True
return False
def synset_rel_sel_prefs_match(sel_preference1, sel_preference2):
relations1 = sel_preference1.synset_relations
relations2 = sel_preference2.synset_relations
if relations1.count() == relations2.count():
for rel in relations1.all():
if not matching_synset_relation_exists(rel, relations2):
return False
else:
return False
return True
def matching_synset_relation_exists(rel_to_find, relations):
for rel in relations.filter(relation=rel_to_find.relation):
if rel_to_find.to == rel.to:
return True
return False
def get_partially_matching_frames(frames, frame_to_find):
matching_frames = []
for frame in frames:
if lexical_units_match(frame, frame_to_find) and frames_partially_match(frame, frame_to_find):
matching_frames.append(frame)
return matching_frames
def frames_partially_match(frame1, frame2):
complements1 = frame1.complements
complements2 = frame2.complements
if complements1.count() == complements2.count():
for compl in complements1.all():
if not partially_matching_complement_exists(complements2.all(), compl):
return False
else:
return False
return True
def partially_matching_complement_exists(complements, compl_to_find):
for compl in complements:
if complements_partially_match(compl, compl_to_find):
return True
return False
def complements_partially_match(compl1, compl2):
if roles_match(compl1.roles, compl2.roles):
return True
return False
def get_structural_matching_frame(frames, frame_to_find):
for frame in frames.all():
if frames_match(frame, frame_to_find):
return frame
return None