-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_experiments_prejacent.py
79 lines (73 loc) · 3.44 KB
/
run_experiments_prejacent.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
import os
import subprocess
import codecs
from collections import defaultdict
import jsonlines
import spacy
import csv
nlp = spacy.load("en_core_web_sm")
def convert_pred_to_bottom_up(tag):
converted = []
if len(tag.strip()) > 1:
tag = tag.split("-")
prefix = tag[0]
suffix = tag[1]
if suffix in ["deontic", "buletic", "teleological", "intentional", "buletic_teleological"]:
converted.append(f"{prefix}-priority")
elif suffix in ["epistemic", "ability", "circumstantial", "opportunity", "ability_circumstantial",
"epistemic_circumstantial"]:
converted.append(f"{prefix}-plausibility")
else:
converted.append(f"{prefix}-{suffix}")
else:
converted.append(tag)
return converted[0]
def convert_tags_to_mnm(tag):
converted = []
if len(tag.strip()) > 1:
tag = tag.split("-")
prefix = tag[0]
suffix = tag[1]
converted.append(f"{prefix}-modal")
else:
converted.append(tag)
return converted[0]
def convert_to_eval_format(filename, outfilename, bottom_up=False):
outfile = codecs.open(outfilename, 'w')
with jsonlines.open(filename) as reader:
for obj in reader:
pred = obj["tags"]
gold = obj["gold_labels"]
words = obj["words"]
for p, g, w in zip(pred, gold, words):
if bottom_up:
#g = convert_pred_to_bottom_up(g)
#p = convert_pred_to_bottom_up(p)
g = convert_tags_to_mnm(g)
p = convert_tags_to_mnm(p)
outfile.write(w + '\t' + g + '\t' + p + '\n')
else:
if w not in ['T_S', 'T_E', 'R_S', 'R_E', 'L_S', 'L_E', 'Y_S', 'Y_E', 'X_S', 'X_E', 'V_S', 'V_E']:
outfile.write(w+'\t'+g+'\t'+p+'\n')
outfile.write('\n')
outfile.close()
for exp in ['prejacent']:
for split in [0, 1, 2, 3, 4]:
try:
command = "allennlp train experiments/"+exp+str(split)+".jsonnet --include-package my_library -s prejacent_exp/"+exp+str(split)
subprocess.run(command, check=True, shell=True)
except subprocess.CalledProcessError:
command = "allennlp train experiments/"+exp+str(split)+".jsonnet --include-package my_library -s prejacent_exp/"+exp+str(split)
subprocess.run(command, check=True, shell=True)
try:
command = "allennlp predict prejacent_exp/"+exp+str(split)+"/model.tar.gz /home/nlp/pyatkiv/workspace/Modality-Corpus/Data/with_event_span/Fine-Grained/"+str(split)+"/dev_space.txt --include-package my_library --use-dataset-reader --cuda-device 0 --output-file prejacent_predictions/"+exp+str(split)
subprocess.run(command, check=True, shell=True)
except subprocess.CalledProcessError:
command = "allennlp predict prejacent_exp/"+exp+str(split)+"/model.tar.gz /home/nlp/pyatkiv/workspace/Modality-Corpus/Data/with_event_span/Fine-Grained/"+str(split)+"/dev_space.txt --include-package my_library --use-dataset-reader --cuda-device 0 --output-file prejacent_predictions/"+exp+str(split)
subprocess.run(command, check=True, shell=True)
pred_file = "prejacent_predictions/"+exp+str(split)
outfile = "prejacent_predictions/readable"+exp+str(split)
convert_to_eval_format(
pred_file,
outfile,
bottom_up=False)