Почему мой код просит определить переменную, которая не может быть определена? - PullRequest
0 голосов
/ 30 августа 2018

Итак, у меня есть фрагмент кода, который представляет собой текстовую классифицирующую нейронную сеть. Я пытаюсь изменить его, чтобы он мог классифицировать ввод пользователя. Это оригинал:

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

Как бы я изменил его, чтобы вместо него можно было использовать ввод пользователя? Спасибо!

1 Ответ

0 голосов
/ 30 августа 2018

Извини! Я смог исправить свой собственный код. Я изменил команду (речь в текст) на предложение, а затем объявил его так:

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)

Теперь работает как шарм!

...