-
Notifications
You must be signed in to change notification settings - Fork 21
/
3i4k_demo.py
231 lines (208 loc) · 6.94 KB
/
3i4k_demo.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import numpy as np
import sys
print('\n\n\n\n\n\n\n\n\n\n\n')
print('#########################################################\n# #\n# Demonstration video: 3i for Korean (3i4K) #\n# #\n#########################################################')
import fasttext
def read_data(filename):
with open(filename, 'r') as f:
data = [line.split('\t') for line in f.read().splitlines()]
return data
model_ft = fasttext.load_model('vectors/model_drama.bin')
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.01
set_session(tf.Session(config=config))
from keras.models import load_model
import librosa
print('Importing models...\n')
model_fci = load_model('model/rec_self_char_dense_drop-24-0.8882.hdf5')
mse_crs = load_model('model/modelspeech/total_s_rmse_cnn_rnnself-12-0.7714-f0.4412.hdf5')
model_aux = load_model('model/modelaux/bilstm_att_re-29-0.9016.hdf5')
mlen=300
wdim=100
def featurize_charrnn_utt(corpus,maxcharlen):
rec_total = np.zeros((1,maxcharlen,wdim))
s = corpus
for j in range(len(s)):
if s[-j-1] in model_ft and j<maxcharlen:
rec_total[0,-j-1,:]=model_ft[s[-j-1]]
return rec_total
def featurize_charcnn_utt(corpus,maxcharlen):
conv_total = np.zeros((1,maxcharlen,wdim,1))
s = corpus
for j in range(len(s)):
if s[-j-1] in model_ft and j<maxcharlen:
conv_total[0,-j-1,:,0]=model_ft[s[-j-1]]
return conv_total
def make_data(filename):
data=np.zeros((1,mlen,128))
data_conv=np.zeros((1,mlen,128,1))
data_rmse=np.zeros((1,mlen,1))
data_srmse=np.zeros((1,mlen,128))
data_srmse_conv=np.zeros((1,mlen,128,1))
data_s_rmse=np.zeros((1,mlen,129))
data_s_rmse_conv=np.zeros((1,mlen,129,1))
y, sr = librosa.load(filename)
D = np.abs(librosa.stft(y))**2
ss, phase = librosa.magphase(librosa.stft(y))
rmse = librosa.feature.rmse(S=ss)
rmse = rmse/np.max(rmse)
rmse = np.transpose(rmse)
S = librosa.feature.melspectrogram(S=D)
S = np.transpose(S)
Srmse = np.multiply(rmse,S)
if len(S)>=mlen:
data[0][:,:]=S[-mlen:,:]
data_conv[0][:,:,0]=S[-mlen:,:]
data_rmse[0][:,0]=rmse[-mlen:,0]
data_srmse[0][:,:]=Srmse[-mlen:,:]
data_srmse_conv[0][:,:,0]=Srmse[-mlen:,:]
data_s_rmse[0][:,0]=rmse[-mlen:,0]
data_s_rmse[0][:,1:]=S[-mlen:,:]
data_s_rmse_conv[0][:,0,0]=rmse[-mlen:,0]
data_s_rmse_conv[0][:,1:,0]=S[-mlen:,:]
else:
data[0][-len(S):,:]=S
data_conv[0][-len(S):,:,0]=S
data_rmse[0][-len(S):,0]=np.transpose(rmse)
data_srmse[0][-len(S):,:]=Srmse
data_srmse_conv[0][-len(S):,:,0]=Srmse
data_s_rmse[0][-len(S):,0]=np.transpose(rmse)
data_s_rmse[0][-len(S):,1:]=S
data_s_rmse_conv[0][-len(S):,0,0]=np.transpose(rmse)
data_s_rmse_conv[0][-len(S):,1:,0]=S
return data,data_conv,data_rmse,data_srmse,data_srmse_conv,data_s_rmse,data_s_rmse_conv
def pred_into_acc(filename):
data,data_conv,data_rmse,data_srmse,data_srmse_conv,data_s_rmse,data_s_rmse_conv =make_data(filename)
att_source= np.zeros((1,64))
z = mse_crs.predict([data_s_rmse_conv,data_s_rmse,att_source])[0]
y = np.argmax(z)
return y
def pred_only_text(s):
rec=featurize_charrnn_utt(s,80)
att=np.zeros((1,64))
z = model_fci.predict([rec,att])[0]
z = np.argmax(z)
y = int(z)
if y==0:
print(' 부가 정보가 필요합니다...\n')
elif y==1:
print(' 그렇군요. 알겠습니다.\n')
elif y==2:
print(' 궁금하신 점이 있으신 것 같네요.\n')
elif y==3:
print(' 요청 접수되었습니다.\n')
elif y==4:
print(' 진심으로 대답을 원하시는 건 아니죠?\n')
elif y==5:
print(' 일단 알겠습니다.\n')
else:
print(' 목소리가 들렸으면 좋겠어요.\n')
return z
def pred_audio_text(filename,s):
rec=featurize_charrnn_utt(s,80)
att=np.zeros((1,64))
z = model_fci.predict([rec,att])[0]
z = np.argmax(z)
y = int(z)
if y<6:
return y
if y==0:
print(' 부가 정보가 필요합니다...\n')
elif y==1:
print(' 그렇군요. 알겠습니다.\n')
elif y==2:
print(' 궁금하신 점이 있으신 것 같네요.\n')
elif y==3:
print(' 요청 접수되었습니다.\n')
elif y==4:
print(' 진심으로 대답을 원하시는 건 아니죠?\n')
else:
print(' 일단 알겠습니다.\n')
else:
y_into = pred_into_acc(filename)
into_att=np.zeros((1,5))
into_att[0,y_into]=1
conv=featurize_charcnn_utt(s,50)
z1 = model_aux.predict([conv,rec,into_att])[0]
y1 = int(np.argmax(z1))+1
return y1
if y==1:
print(' 그렇군요. 알겠습니다.\n')
elif y==2:
print(' 궁금하신 점이 있으신 것 같네요.\n')
elif y==3:
print(' 요청 접수되었습니다.\n')
elif y==4:
print(' 진심으로 대답을 원하시는 건 아니죠?\n')
else:
print(' 일단 알겠습니다.\n')
def classify_document(filename):
target5 = read_data(filename+'.txt')
f0 = open(filename+'_frag.txt','w')
f1 = open(filename+'_stat.txt','w')
f2 = open(filename+'_ques.txt','w')
f3 = open(filename+'_comm.txt','w')
f4 = open(filename+'_rheq.txt','w')
f5 = open(filename+'_rhec.txt','w')
f6 = open(filename+'_into.txt','w')
for i in range(len(target5)):
print(target5[i][0])
d = pred_only_text(target5[i][0])
if i%2000 == 0:
print(i,'over',len(target5))
if d == 0:
f0.write(target5[i][0]+'\n')
if d == 1:
f1.write(target5[i][0]+'\n')
if d == 2:
f2.write(target5[i][0]+'\n')
if d == 3:
f3.write(target5[i][0]+'\n')
if d == 4:
f4.write(target5[i][0]+'\n')
elif d==5:
f5.write(target5[i][0]+'\n')
else:
f6.write(target5[i][0]+'\n')
f0.close()
f1.close()
f2.close()
f3.close()
f4.close()
f5.close()
f6.close()
print('#########################################################\n\n Intention identification and text classification demo\n\n#########################################################\n\n Enter "i" to identify a sentence\n Enter "c" to classify a corpus\n Enter "bye" to quit\n\n')
from pathlib import Path
def detect():
print('\n Identification demo ...\n Enter "c" to activate classification mode\n Enter "bye" to quit\n')
while 1:
s = input(' You say: ')
if s == 'bye':
sys.exit()
elif s == 'c':
classify()
else:
pred_only_text(s)
def classify():
print('\n Classification demo ...\n Enter "i" to activate identification mode\n Enter "bye" to quit\n')
while 1:
s = input(' Target filename (.txt): ')
if s == 'bye':
sys.exit()
elif s == 'i':
detect()
else:
if Path(s+'.txt').is_file():
classify_document(s)
else:
print(' No such file exists!')
while 1:
s = input(' Choose: ')
if s == 'i':
detect()
elif s == 'c':
classify()
elif s == 'bye':
sys.exit()