【问题标题】:Why does my code ask to define a variable that can't be defined?为什么我的代码要求定义一个无法定义的变量?
【发布时间】: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的变量...

标签: python nameerror


【解决方案1】:

对不起!我能够修复自己的代码。我将命令(语音到文本)更改为句子,然后像这样重新声明:

def SpeechAndTextClassification(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

sentence = myCommand()
sentence #executing the function 
talkToMe("hello world")
SpeechAndTextClassification(sentence)

现在就像一个魅力!

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2021-11-26
    相关资源
    最近更新 更多