-
Notifications
You must be signed in to change notification settings - Fork 2
/
eMG_run.py
100 lines (87 loc) · 2.82 KB
/
eMG_run.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
import getopt
import sys
from eMG_generate import *
agree_cats = []
late_expand = []
late_expand_default = False
sinking = False
def main(argv):
global agree_cats
global late_expand
global late_expand_default
global sinking
lexicon_file = 'lexicon/eMG_dict_RC.json'
#lexicon_file = 'lexicon/eMG_UD_ENG_extracted.json'
parameters_file = 'parameters/eMG_param_default.json'
#parameters_file = 'parameters/eMG_param_eng.json'
input_sentence = "I saw the cow that the giraffe kicked"
#input_sentence = "i see a tree."
silent = False
#silent = True
try:
opts, args = getopt.getopt(argv, "i:l:p:s:", ["input_sentence=", "lexicon_file=", "parameters_file=", "silent="])
except getopt.GetoptError as e:
sys.stderr.write("%s %s\n" % (argv[0], e.msg))
sys.exit(1)
for opt, arg in opts:
if opt == '-h':
print('eMG_run.py -i <input sentence you want to process> -l <lexicon_file.json> -p <parameters_file.json>')
sys.exit()
elif opt in ("-i", "--input_sentence"):
input_sentence = arg
elif opt in ("-l", "--lexicon_file"):
lexicon_file = arg
elif opt in ("-p", "--parameters_file"):
parameters_file = arg
elif opt in ("-s", "--silent"):
silent = True
print('Input: "' + input_sentence + '"')
print('Lexicon file: ', lexicon_file)
print('Parameter file: ', parameters_file)
print('Silent: ', silent)
g = MG_generate(lexicon_file)
root = g.mg.select("ROOT")
if root.ambiguous:
prompt = "Digit your ROOT choice:\n"
for i, r in enumerate(root.ambiguous):
prompt = prompt + "[" + str(i) + "] for " + r + "\n"
choice = input(prompt)
while not check_choice(choice, len(root.ambiguous)):
choice = input("Wrong choice. " + prompt)
root = g.mg.select(root.ambiguous[int(choice)])
g.mg.set_root(root)
get_param(parameters_file)
g.mg.set_param_agree(agree_cats)
g.mg.set_late_expansion(late_expand)
g.mg.late_expansion_default = late_expand_default
g.mg.sinking = sinking
g.mg.tracking = not silent
g.sentence = input_sentence
g.generate(input_sentence.split())
def get_param(param_file) -> {}:
global agree_cats
global late_expand
global late_expand_default
global sinking
with open(param_file) as json_file:
params = json.load(json_file)
n = 0
for x in params['Agreement']['expected'][0]:
agree_cats.insert(n, params['Agreement']['expected'][0][x])
n += 1
n = 0
for x in params['Agreement']['expected'][0]:
agree_cats.insert(n, params['Agreement']['expected'][0][x])
n += 1
n = 0
for x in params['Agreement']['cued_agree'][0]:
agree_cats.insert(n, params['Agreement']['cued_agree'][0][x])
n += 1
n = 0
for x in params['Late_expansion']['expected'][0]:
late_expand.insert(n, params['Late_expansion']['expected'][0][x])
n += 1
late_expand_default = bool(params['Late_expansion']['default'])
sinking = bool(params['Sinking']['default'])
if __name__ == "__main__":
main(sys.argv[1:])