utils.py
3.39 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
# -*- coding: utf-8 -*-
def get_frames_differences(initial_frames, frames):
differences = {'matching_frames': [],
'missing_frames': []}
for ini_frame in initial_frames:
matching_frame = get_matching_frame(frames, ini_frame)
if matching_frame:
differences['matching_frames'].append(ini_frame)
else:
differences['missing_frames'].append(ini_frame)
return differences
def get_matching_frame(frames, frame_to_find):
for frame in frames.all():
if frames_match(frame, frame_to_find):
return frame_to_find
return None
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(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