0

我正在尝试创建一个小程序,如果在我说的话(不仅是一个词,而且是一个短语)(使用 Speech_recognizer)中有一个单词包含在单词列表(word_list)中,它应该打印一些东西。如果我说一个词或另一个词,输出也应该改变,所以我需要准确检查它是哪个词。

import speech_recognition as sr

word_list = ['Giotto', 'Raffaello', 'Michelangelo']

recognizer_instance = sr.Recognizer()

with sr.Microphone() as source:
    recognizer_instance.adjust_for_ambient_noise(source)
    print("I'm listening...")
    audio = recognizer_instance.listen(source)
    print('Printing what I heard...')

try:
    text = recognizer_instance.recognize_google(audio, language='it-IT')
    print(text)
except Exception as e:
    print(e)

这是我目前的代码,我不知道如何检查 text 变量中包含的单词之一是否在我的 word_list 中

4

1 回答 1

0

如果您只是想检查您所说的是否在列表中,您可以使用:

if text in word_list:
    #Some logic

True如果变量的text内容在里面,这将返回word_list

编辑:
您可以定义字典而不是列表,并像这样存储每个艺术家的简历:

artists_bio = {'artist1': 'artist1 bio', 'artist2': 'artist2 bio'...}

然后,如果识别器听到匹配的单词,您可以使用以下命令打印此简历:

if text in artist_bio.keys():
    print(artist_bio[text])

编辑2:

如果您的文本是一个完整的句子,您可以先将其拆分,然后使用与以前相同的逻辑。

list_text = text.split(' ')
for text in list_text:
    if text in artist_bio.keys():
        print(artist_bio[text])
于 2020-10-01T13:17:17.413 回答