-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexperiment_2.py
135 lines (107 loc) · 4.36 KB
/
experiment_2.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
132
133
134
135
"""
Run transfer experiments.
Running this file will replicate experiment 2 from the paper.
"""
import json
from conch.evaluation.intrinsic import evaluate_transfer
from reach import Reach
from conch.conch import compose, reciprocal
from conch.evaluation.utils import evaluate_k
from conch.preprocessing.baseline import baseline
from itertools import chain
def experiment(parsed_train,
gold_chunks_train,
parsed_test,
gold_chunks_test,
embeddings,
context_function,
window,
k,
use_focus=True):
"""Run an experiment with transfer evaluation."""
_, np_chunks_train = zip(*parsed_train)
_, np_chunks_test = zip(*parsed_test)
phrase_embeddings_train = compose(parsed_train,
window=window,
embeddings=embeddings,
context_function=context_function,
use_focus=use_focus)
phrase_embeddings_test = compose(parsed_test,
window=window,
embeddings=embeddings,
context_function=context_function,
use_focus=use_focus)
result = evaluate_transfer(gold_chunks_train,
np_chunks_train,
gold_chunks_test,
np_chunks_test,
phrase_embeddings_train,
phrase_embeddings_test,
k=k)
return result
if __name__ == "__main__":
scores = {}
parsed_train = json.load(open("data/partners_uima.json"))
parsed_train = list(zip(*sorted(parsed_train.items())))[1]
gold_train = json.load(open("data/partners_gold.json"))
gold_train = list(zip(*sorted(gold_train.items())))[1]
parsed_test = json.load(open("data/beth_uima.json"))
parsed_test = list(zip(*sorted(parsed_test.items())))[1]
gold_test = json.load(open("data/beth_gold.json"))
gold_test = list(zip(*sorted(gold_test.items())))[1]
txt, gold_chunks_train = zip(*gold_train)
_, gold_chunks_test = zip(*gold_test)
embeddings = Reach.load("")
for a, b in zip(parsed_train, gold_train):
assert len(a[0]) == len(b[0])
for a, b in zip(parsed_test, gold_test):
assert len(a[0]) == len(b[0])
knn_focus = experiment(parsed_train,
gold_chunks_train,
parsed_test,
gold_chunks_test,
embeddings,
reciprocal,
0,
k=1,
use_focus=True)
knn_full = experiment(parsed_train,
gold_chunks_train,
parsed_test,
gold_chunks_test,
embeddings,
reciprocal,
10,
k=1,
use_focus=True)
knn_context = experiment(parsed_train,
gold_chunks_train,
parsed_test,
gold_chunks_test,
embeddings,
reciprocal,
10,
k=1,
use_focus=False)
# Baseline space with 10000 words.
txt = list(chain.from_iterable(txt))
embeddings = baseline(txt, 10000)
baseline = experiment(parsed_train,
gold_chunks_train,
parsed_test,
gold_chunks_test,
embeddings,
reciprocal,
0,
k=1,
use_focus=True)
scores_knn = {'focus': knn_focus,
'full': knn_full,
'context': knn_context,
'baseline': baseline}
scores = {}
for k, v in scores_knn.items():
t, p = zip(*v)
scores[k] = evaluate_k(t, p, None)
json.dump(scores, open("scores_transfer.json", 'w'))
json.dump(scores_knn, open("knn_transfer.json", 'w'))