-
Notifications
You must be signed in to change notification settings - Fork 0
/
svo.py
76 lines (59 loc) · 1.69 KB
/
svo.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
import parser
import squad
def generateGraph(txt):
statements=txt.split(".")
statements=list(map(parser.svo,statements))
return statements
def equal(s1,s2):
return s1[0].lower()==s2[0].lower() and s1[1].lower()==s2[1].lower() and s1[2].lower()==s2[2].lower()
def match(s1,s2):
return (s1[0].lower()==s2[0].lower() or s1[2].lower()==s2[2].lower()) or s1[2].lower()==s2[2].lower()
def inGraph(svo,graph):
for inst in graph:
if equal(inst,svo):
return True
return False
def findMatch(svo,graph):
for inst in graph:
if match(inst,svo):
return inst
return None
def inGraphs(g1,g2):
for inst in g1:
if inGraph(inst,g2):
return True
return False
def findMatchGraphs(g1,g2):
for inst in g1:
if findMatch(inst,g2):
return True
return False
def graphToString(g):
def fgg(ggg):
return ggg+" "
def fg(gg):
return list(map(fgg,gg))
return gg+" "
return "".join(sum(list(map(fg,g)),[]))+ " "
def graphToAnswerString(g):
return " ".join(list(map(lambda x: x[2]+" ",g)))
def countSameDifference(t1,t2):
txt1=t1.split(" ")
txt2=t2.split(" ")
tp=0
for tt1 in txt1:
#bool=False
for tt2 in txt2:
if(tt1==tt2): #and bool=False:#Item in ls 1 is in ls 2 and hasnt been marked
#bool=True
tp+=1
fp=len(txt1)-tp
fn=len(txt2)-tp
return [tp,fp,fn]
def stats(g1,g2):
[tp,fp,fn]=countSameDifference(graphToString(g1),graphToString(g2))
precision=0.75*tp/(tp+fp)
recall=tp/(tp+fn)
f1=(precision*recall)/(precision+recall)
# print(precision,recall)
return f1