修复了语言模型的概率计算错误的Bug,并提高API服务器的健壮性

This commit is contained in:
nl8590687 2018-06-07 12:48:31 +08:00
parent caefbc835a
commit a2588dec1a
2 changed files with 26 additions and 8 deletions

View File

@ -137,8 +137,9 @@ class ModelLanguage(): # 语音模型类
#print('tmp_words: ',tmp_words,tmp_words in self.model2)
if(tmp_words in self.model2): # 判断它们是不是再状态转移表里
#print(tmp_words,tmp_words in self.model2)
tuple_word[1] = tuple_word[1] * float(self.model2[tmp_words]) / float(self.model1[tmp_words[-1]])
tuple_word[1] = tuple_word[1] * float(self.model2[tmp_words]) / float(self.model1[tmp_words[-2]])
# 核心在当前概率上乘转移概率公式化简后为第n-1和n个字出现的次数除以第n-1个字出现的次数
#print(self.model2[tmp_words],self.model1[tmp_words[-2]])
else:
tuple_word[1] = 0.0
continue
@ -238,6 +239,7 @@ if(__name__=='__main__'):
#str_pinyin = ['wo3','qu4','a4','mei2','shi4','er2','la1']
#str_pinyin = ['wo3', 'men5', 'qun2', 'li3', 'xiong1', 'di4', 'jian4', 'mei4', 'dou1', 'zai4', 'shuo1']
#str_pinyin = ['su1', 'an1', 'ni3', 'sui4', 'li4', 'yun4', 'sui2', 'cong2', 'jiao4', 'ming2', 'tao2', 'qi3', 'yu2', 'peng2', 'ya4', 'yang4', 'chao1', 'dao3', 'jiang1', 'li3', 'yuan2', 'kang1', 'zhua1', 'zou3']
str_pinyin = ['da4', 'jia1', 'hao3']
#r = ml.decode(str_pinyin)
r=ml.SpeechToText(str_pinyin)
print('语音转文字结果:\n',r)

View File

@ -14,7 +14,7 @@ from LanguageModel import ModelLanguage
datapath = 'data/'
modelpath = 'model_speech/'
ms = ModelSpeech(datapath)
ms.LoadModel(modelpath + 'speech_model24_e_0_step_216500.model')
ms.LoadModel(modelpath + 'm24/speech_model24_e_0_step_411000.model')
ml = ModelLanguage('model_language')
ml.LoadModel()
@ -55,7 +55,7 @@ class TestHTTPHandle(http.server.BaseHTTPRequestHandler):
for line in datas_split:
[key, value]=line.split('=')
if('wavs' == key):
if('wavs' == key and '' != value):
wavs.append(int(value))
elif('fs' == key):
fs = int(value)
@ -66,8 +66,18 @@ class TestHTTPHandle(http.server.BaseHTTPRequestHandler):
else:
print(key, value)
if(token != 'qwertasd'):
buf = '403'
print(buf)
buf = bytes(buf,encoding="utf-8")
self.wfile.write(buf)
return
#if('python-list' == type):
r = self.recognize([wavs], fs)
if(len(wavs)>0):
r = self.recognize([wavs], fs)
else:
r = ''
#else:
# r = self.recognize_from_file('')
@ -82,14 +92,20 @@ class TestHTTPHandle(http.server.BaseHTTPRequestHandler):
self._set_response()
#buf = '<!DOCTYPE HTML> \n<html> \n<head>\n<title>Post page</title>\n</head> \n<body>Post Data:%s <br />Path:%s\n</body> \n</html>'%(datas,self.path)
print(buf)
buf = bytes(buf,encoding="utf-8")
self.wfile.write(buf)
def recognize(self, wavs, fs):
r_speech = ms.RecognizeSpeech(wavs, fs)
str_pinyin = r_speech
r = ml.SpeechToText(str_pinyin)
r=''
try:
r_speech = ms.RecognizeSpeech(wavs, fs)
print(r_speech)
str_pinyin = r_speech
r = ml.SpeechToText(str_pinyin)
except:
r=''
print('[*Message] Server raise a bug. ')
return r
pass