【发布时间】:2018-08-30 04:15:30
【问题描述】:
所以我有一段代码是一个文本分类神经网络。我正在尝试对其进行修改,以便它可以对用户的输入进行分类。这是原文:
def SpeechToTextAndClassification(sentence, show_details=False):
results = think(sentence, show_details)
results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD ]
results.sort(key=lambda x: x[1], reverse=True)
return_results =[[classes[r[0]],r[1]] for r in results]
print ("%s \n classification: %s" % (sentence, return_results))
return return_results
SpeechToTextAndClassification("yes")
这是修改后的版本:
def SpeechToTextAndClassification(command, sentence, show_details=False):
results = think(sentence, show_details)
results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD ]
results.sort(key=lambda x: x[1], reverse=True)
return_results =[[classes[r[0]],r[1]] for r in results]
print ("%s \n classification: %s" % (sentence, return_results))
return return_results
print ()
print("run check")
talkToMe("I am ready, sir") #text to speech
command = myCommand()
print(command)
SpeechToTextAndClassification(command, sentence, show_details=True)
问题在于它在句子上给出了一个错误-该句子是训练数据的一部分,如下所示:
training_data = []
training_data.append({"class":"yes", "sentence":"Yes "})
training_data.append({"class":"yes", "sentence":"Yeah"})
training_data.append({"class":"yes", "sentence":"Yup "})
training_data.append({"class":"yes", "sentence":"Yes it does"})
出现以下错误:
line 319, in <module>
SpeechToTextAndClassification(command, sentence, show_details=True)
NameError: name 'sentence' is not defined
我将如何更改它以便它可以使用用户的输入来代替?谢谢!
【问题讨论】:
-
能否格式化您的代码
-
为什么要定义
sentence?你从来没有在任何地方定义过它 -
@e4c5 请说明
-
@juanpa.arrivillaga 它在 training_data_append 部分中定义
-
那里没有定义名为
sentence的变量...