-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.py
executable file
·170 lines (136 loc) · 4.69 KB
/
super.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/local/bin/python3
# POS- and supertagger using (averaged) ELMo embeddings
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sys, getopt
import os, os.path
import pickle
import operator
from keras.models import Model, load_model
from keras.layers import Bidirectional, Dense, Input, Dropout, LSTM, Activation, TimeDistributed, BatchNormalization, concatenate, Concatenate
from keras.layers.embeddings import Embedding
from keras.constraints import max_norm
from keras import regularizers
from keras.utils import to_categorical
from keras import backend as K
from gensim.models import KeyedVectors
from elmoformanylangs import Embedder
from grail_data_utils import *
inputfile = 'input.txt'
outputfile = 'super.txt'
beta = 1.0
modelfile = 'best_elmo_superpos.h5'
try:
opts, args = getopt.getopt(sys.argv[1:],"hbiom",["beta=","input=","output=","model="])
print(opts)
except getopt.GetoptError as err:
print(str(err))
print("super.py -b <beta_value> -i <inputfile> -o <outputfile> -m <modelfile>")
for opt, arg in opts:
if opt == "-h":
print("super.py -b <beta_value> -i <inputfile> -o <outputfile> -m <modelfile>")
elif opt in ("-m", "--model"):
modelfile = arg
elif opt in ("-i", "--input"):
inputfile = arg
elif opt in ("-o", "--output"):
outputfile = arg
elif opt in ("-b", "--beta"):
beta = float(arg)
current_dir = os.getcwd()
os.chdir('/Users/moot/checkout/DeepGrail/best_elmo_superpos')
# load auxiliary mappings
def load_obj(name):
with open(name + '.pkl', 'rb') as f:
return pickle.load(f)
super_to_index = load_obj('super_to_index')
index_to_super = load_obj('index_to_super')
pos1_to_index = load_obj('pos1_to_index')
index_to_pos1 = load_obj('index_to_pos1')
pos2_to_index = load_obj('pos2_to_index')
index_to_pos2 = load_obj('index_to_pos2')
maxLen = 266
numSuperClasses = len(index_to_super) + 1
numPos1Classes = len(index_to_pos1) + 1
numPos2Classes = len(index_to_pos2) + 1
# load ELMo embedder
print('Loading French ELMo embeddings')
e = Embedder('/Users/moot/Software/FrenchELMo/')
# load corpus data
def read_text_file(filename):
with open(filename, 'r') as f:
lines = 0
text = []
pos = {}
for line in f:
outwords = []
outpos = []
line = line.strip().split()
length = len(line)
if (length > maxLen):
print("Skipped long sentence (", end='')
print(length, end='')
print("):")
print(line)
else:
for i in range(length):
item = line[i]
iitems = item.split('|')
word = iitems[0]
outwords.append(word)
if len(iitems) > 1:
ipos = iitems[1]
outpos.append(ipos)
text.append(outwords)
if outpos != []:
pos[lines] = outpos
lines = lines + 1
return text, pos, lines
print('Reading input file')
text, pos, numLines = read_text_file(inputfile)
print('Computing French ELMo embeddings for input text')
text_emb = e.sents2elmo(text)
ll = len(text_emb)
Xarr= np.zeros((ll,maxLen,1024))
for i in range(ll):
sl = len(text[i])
for j in range(sl):
Xarr[i][j]= text_emb[i][j]
sentence_embeddings = Input(shape = (maxLen,1024,), dtype = 'float32')
model = load_model(modelfile)
predict_pos1, predict_pos2, predict_super = model.predict(Xarr)
os.chdir(current_dir)
f = open(outputfile, 'w')
index_to_super[0] = '*UNK*'
for i in range(len(text)):
string = ""
for j in range(len(text[i])):
if pos != {}:
sentpos = pos[i]
jpos = sentpos[j]
posstr = str(jpos) + "|"
else:
pos1num = np.argmax(predict_pos1[i][j])
pos2num = np.argmax(predict_pos2[i][j])
posstr = index_to_pos1[pos1num] + "-" + index_to_pos2[pos2num] + "|"
if beta < 1:
tags = predict_beta(predict_super[i][j],beta)
tagstr = str(len(tags))
while tags != {}:
cmax = max(tags.items(), key=operator.itemgetter(1))[0]
pstr = str(tags[cmax])
del tags[cmax]
tstr = str(index_to_super[cmax])
tagstr = tagstr + "|" + tstr + "|" + pstr
else:
num = np.argmax(predict_super[i][j])
tagstr = str(index_to_super[num])
wrd = text[i][j]
string = string + " " + wrd +'|'+posstr+tagstr
string = string.strip()
print(string)
string = string + "\n"
f.write(string)
f.close()
exit()