-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaveragejacard.py
131 lines (91 loc) · 2.97 KB
/
averagejacard.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
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
import numpy as np
import sys
import re
import math
total = []
lines = []
def isIt(s, p):
if len(re.findall(p, s)) > 0:
return True
return False
for line in sys.stdin:
lines.append(line.replace("\n", ""))
def lessThanGreaterThanK(l, k):
try:
if (int(l.split(",")[4]) <= k and int(l.split(",")[4]) > floor[k]):
return True
return False
except ValueError:
pass
syn = [l for l in lines if isIt(l, "^syn")]
hypo = [l for l in lines if isIt(l, "^hypo")]
hyper = [l for l in lines if isIt(l, "^hyper")]
holo = [l for l in lines if isIt(l, "^holo")]
mero = [l for l in lines if isIt(l, "^mero")]
n_groups = 5
ks = [200, 400, 600, 800, 1000]
floor = {}
floor[200] = 0
floor[400] = 200
floor[600] = 400
floor[800] = 600
floor[1000] = 800
count_syn = []
count_hyper = []
count_hypo = []
count_holo = []
count_mero = []
base = 10
for k in ks:
arr = np.array([float(s.split(",")[5]) for s in syn if lessThanGreaterThanK(s, k)])
lessthan = np.mean(arr)
count_syn.append(lessthan)
for k in ks:
lessthan = np.mean(np.array([float(s.split(",")[5]) for s in hyper if lessThanGreaterThanK(s, k)]))
count_hyper.append(lessthan)
for k in ks:
lessthan = np.mean(np.array([float(s.split(",")[5]) for s in hypo if lessThanGreaterThanK(s, k)]))
count_hypo.append(lessthan)
for k in ks:
lessthan = np.mean(np.array([float(s.split(",")[5]) for s in holo if lessThanGreaterThanK(s, k)]))
count_holo.append(lessthan)
for k in ks:
lessthan = np.mean(np.array([float(s.split(",")[5]) for s in mero if lessThanGreaterThanK(s, k)]))
count_mero.append(lessthan)
count_syn = tuple(count_syn)
count_hyper = tuple(count_hyper)
count_hypo = tuple(count_hypo)
count_holo = tuple(count_holo)
count_mero = tuple(count_mero)
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.1
opacity = 0.4
rects1 = plt.bar(index + .1, count_syn, bar_width,
alpha=opacity,
color='blue',
label='synonyms')
rects2 = plt.bar(index + .25, count_hyper, bar_width,
alpha=opacity,
color='red',
label='hyernyms')
rects3 = plt.bar(index + .4, count_hypo, bar_width,
alpha=opacity,
color='purple',
label='hypernyms')
rects4 = plt.bar(index + .55, count_holo, bar_width,
alpha=opacity,
color='green',
label='holonyms')
rects5 = plt.bar(index + .7, count_mero, bar_width,
alpha=opacity,
color='orange',
label='meronyms')
plt.xlabel('K')
plt.ylabel('Average Jacard Distance')
plt.title('Average Jaccard Distance by Relation Type and K')
plt.xticks(index + bar_width * 5, ('<200', '200-400', '400-600', '600-800', '>800'))
plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.tight_layout()
plt.savefig('jacard.png', bbox_inches='tight', pad_inches=.4)