Pyspeech两个未知输入来自两个列表

2024-09-28 05:17:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在python2.7.9上玩pyspeech 0.5.2,结果碰到了一堵墙。 你可以通过给pyspeech一个要听的单词列表来听特定的单词,如下所示:

color = ['green', 'red', 'yellow', 'brown'...]
phrase = speech.input("I'm listening", color)
print phrase

只有当它识别出上面列表中的任何单词时,它才会继续。 我想要达到的目标,需要两个单独的清单。如果没有提供的列表,我在这些列表中真正拥有的单词对于语音识别来说太复杂了,无法猜测,而且列表太大了,不足以构成一个组合表。 假设我使用的是上面的颜色列表,我想为它们中的每一个分配额外的命令: 搜索绿色。或涂成绿色。你知道吗

这些单词,搜索绘制,以及其他单词在另一个列表中,我需要将每个列表中的一个单词组合成一个两个单词的句子。我该怎么做?你知道吗

以下是我想要的形象:

color = ['green', 'red', 'yellow', 'brown'...]
commands = ['search', 'paint', 'show'...]
phrase = speech.input("I'm listening", commands, color,)

---saying green & paint out loud---
>>> phrase
['green', 'paint'] # OR 'green paint'

或者,如果我从颜色列表中得到一个单词,让识别人员猜测另一个单词,也会有所帮助,比如:

phrase = speech.listenfor  ->  'open %s'
#where %s would be replaced by anything after open
prhase = speech.listenfor  ->  color + "%s"
#one word from color, and %s would be replaced with anything I said after

(上面的“代码”更像是一种表示)


Tags: 列表input颜色greenred单词speechcolor
1条回答
网友
1楼 · 发布于 2024-09-28 05:17:31

在内部pyspeech使用语法接口从短语列表中设置语法:

    grammar.DictationSetState(0)
    # dunno why we pass the constants that we do here
    rule = grammar.Rules.Add("rule",
            _constants.SRATopLevel + _constants.SRADynamic, 0)
    rule.Clear()

    for phrase in phraselist:
        rule.InitialState.AddWordTransition(None, phrase)

    # not sure if this is needed - was here before but dupe is below
    grammar.Rules.Commit()

您可以修改这部分代码以添加更复杂的规则,这在pyspeech之外是不可能通过公共pyspeechapi实现的。或者,您可以直接使用winapi而不使用pyspeech。你知道吗

相关问题 更多 >

    热门问题