This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_similarities.py
98 lines (88 loc) · 2.86 KB
/
run_similarities.py
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
import yaml
from crawltogsm.LegacyPipeline import LegacyPipeline
from gsmtosimilarity.graph_similarity import Singleton, Grouping, SetOfSingletons, Graph, Relationship
def singlet(x):
return Singleton(
named_entity=x,
properties=frozenset(),
min=-1,
max=-1,
type='None',
confidence=1.0
)
def _not(x):
grouped_nodes = [x]
SetOfSingletons(
type=Grouping.NOT,
entities=tuple(grouped_nodes),
min=min(grouped_nodes, key=lambda x: x.min).min,
max=max(grouped_nodes, key=lambda x: x.max).max,
confidence=1
)
def _and(x,y):
grouped_nodes = [x,y]
return SetOfSingletons(
type=Grouping.AND,
entities=tuple(grouped_nodes),
min=min(grouped_nodes, key=lambda x: x.min).min,
max=max(grouped_nodes, key=lambda x: x.max).max,
confidence=1
)
def _or(x,y):
grouped_nodes = [x,y]
return SetOfSingletons(
type=Grouping.OR,
entities=tuple(grouped_nodes),
min=min(grouped_nodes, key=lambda x: x.min).min,
max=max(grouped_nodes, key=lambda x: x.max).max,
confidence=1
)
def _neither(x,y):
grouped_nodes = [_not(x),_not(y)]
return SetOfSingletons(
type=Grouping.OR,
entities=tuple(grouped_nodes),
min=min(grouped_nodes, key=lambda x: x.min).min,
max=max(grouped_nodes, key=lambda x: x.max).max,
confidence=1
)
def edge(g, verb, h, isNegated):
return Relationship(
source=g,
target=h,
edgeLabel=Singleton(
named_entity=verb,
properties=frozenset(dict().items()),
min=-1,
max=-1,
type="verb",
confidence=-1
),
isNegated=isNegated
)
def graph(edges):
e = []
for g,verb,h,isneg in edges:
e.append(edge(g, verb, h, isneg))
return Graph(edges=e)
if __name__ == '__main__':
conf = "/home/giacomo/projects/similarity-pipeline/submodules/news-crawler/config_proposed.yaml"
try:
with open(conf) as f:
cfg = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError:
raise Exception("Error: missing configuration file")
l = LegacyPipeline(cfg)
Alice = singlet("Alice")
Bob = singlet("Bob")
soccer = singlet("soccer")
a = graph([(Alice,"plays",soccer,False)])
na = graph([(Alice,"plays",soccer,True)])
b = graph([(Bob, "plays", soccer, False)])
ab = graph([(_or(Alice,Bob), "plays", soccer, False)])
# print(l.graph_similarity(a,b))
# print(l.graph_similarity(a,na))
# print(l.graph_similarity(a,ab))
# print(l.graph_similarity(b,ab))
# print(l.graph_similarity(ab,a))
# print(l.graph_similarity(ab,b))