0

不久前,我写了一个程序,有点像(电影)钢铁侠的 JARVIS,它接受诸如“打开程序”或“读取文本”之类的命令,然后执行相应的操作。我已经编写了它,以便可以通过键盘或语音识别输入命令。我遇到的问题是程序不会接受我口授的命令。如果我输入“阅读文本”,它将遵循命令。如果我使用语音识别输入“阅读文本”,它会调用“我不理解”。请记住,输入的文本完全相同。欢迎和赞赏有关此令人困惑的问题的任何帮助。另外,请原谅我的旧代码,杂乱无章。

这是从命令部分开始的代码:

import random
import time
from win32com.client import constants
import win32com.client
import pythoncom
import webbrowser
import os
import sys
from sys import exit

speaker = win32com.client.Dispatch("SAPI.SpVoice")
POSITIVE_RESPONSES = ('good', 'great', 'fantastic', 'decent', 'fine', 'ok', 'okay')
NEGATIVE_RESPONSES = ('bad', 'terrible', 'sad', 'grumpy', 'angry', 'irritated', 'tired')
CONFIRMATION = ('yes', 'yeah', 'yep', 'yesh', 'sure', 'yeppers', 'yup')
DECLINATION = ('no', 'nope', 'too bad', 'nothing')
QUESTIONS = ('what', 'largest', 'ocean')
BROWSER = ('browser')
PROGRAM = ('program')
URL = ('url')
list1 = ['ccleaner', 'c++','minecraft','VLC', 'steam', 'guitar pro', 'dropbox', 'cbr reader', 'avast', 'paint.net' ]
list2 = ['Read text', 'Open a program', 'Open a browser', 'Open a URL', 'Converse']
NUM_SET_ONE = (1, 2, 3, 4, 5)
NUM_SET_TWO = (6, 7, 8, 9, 10)
NUM_SET_THREE = (11, 12, 13, 14, 15)
NUM_SET_FOUR = (16, 17, 18, 19, 20)
NUM_SET_FIVE = (21, 22, 23, 24, 25)
NUM_SET_SIX = (26, 27, 28, 29, 30)

def opening():
    speaker.Speak('HELLO USER. WELCOME TO D UNDER MIFF LIN. I AM COMP YOO TRON, YOUR ANSWER TO EVERYTHING.')
    print('HELLO, USER. WELCOME TO DUNDER MIFFLIN. I AM COMPUTRON, YOUR ANSWER TO EVERYTHING.')

def reOpening():
    speaker.Speak('GOOD! THEN RE-START THE COVERSATION.')
    varResponse = str(input('GOOD! THEN RE-START THE COVERSATION.'))
    if varResponse in CONFIRMATION: ambiguousResponses()
    elif varResponse in DECLINATION: finalResponses()
    else: ambiguousResponses()

def browser():
    speaker.Speak('OPENING FIREFOX...')
    print('OPENING FIREFOX...')
    handle = webbrowser.get()
    handle.open('http://youtube.com')
    askAgain()

def url():
    speaker.Speak('ENTER YOUR U R L:')
    response = str(input('ENTER YOUR URL:'))
    handle = webbrowser.get()
    handle.open(response)
    askAgain()

def programs():
    speaker.Speak('ENTER THE PROGRAM\'S NAME:')
    response = str(input('ENTER THE PROGRAM\'S NAME:'))
    if response == list1[0]:
        speaker.Speak('OPENING C CLEANER...')
        print('OPENING CCLEANER...')
        os.startfile("C:\Program Files\CCleaner\CCleaner64.exe")
        askAgain()
    elif response == list1[1]:
        speaker.Speak('OPENING C PLUS PLUS...')
        print('OPENING C++...')
        os.startfile("C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe")
        askAgain()
    elif response == list1[2]:
        #speaker.Speak('OPENING WHY GEE OH PRO...')
        print('OPENING YGOPRO...')
        #os.startfile("C:\Users\Rj\Desktop\ygopro-1.030.0-V4-Percy-full")
        #askAgain()

    elif response == list1[3]:
        speaker.Speak('OPENING VLC...')
        print('OPENING VLC...')
        os.startfile('C:\Program Files (x86)\VideoLAN\VLC\vlc.exe')
        askAgain()
    elif response == list1[4]:
        speaker.Speak('OPENING STEAM...')
        print('OPENING STEAM...')
        os.startfile('C:\Program Files (x86)\Steam\Steam.exe')
        askAgain()
    elif response == list1[5]:
        speaker.Speak('OPENING GUITAR PRO...')
        print('OPENING GUITAR PRO...')
        os.startfile('C:\Program Files (x86)\Guitar Pro 6\GuitarPro.exe')
        askAgain()
    elif response == list1[6]:
        speaker.Speak('OPENING DROP BOX...')
        print('OPENING DROP BOX...')
        # os.startfile()
        askAgain()
    elif response == list1[7]:
        speaker.Speak('OPENING CBR READER...')
        print('OPENING CBR READER...')
        os.startfile('C:\Program Files (x86)\CBR Reader\CBRReader.exe')
        askAgain()
    elif response == list1[8]:
        speaker.Speak('OPENING AVAST...')
        print('OPENING AVAST...')
        os.startfile('C:\Program Files\AVAST Software\Avast\AvastUI.exe')
        askAgain()
    elif response == list1[9]:
        speaker.Speak('OPENING PAINT.NET...')
        print('OPENING PAINT.NET...')
        # os.startfile()
        askAgain()
    else:
        speaker.Speak('I DON\'T UNDERSTAND')
        print('I DONT\'T UNDERSTAND')
        askAgain()

def askForQuestions():
    speaker.Speak('WHAT WOULD YOU LIKE ME TO DO? ENTER A COMMAND.')
    print('WHAT WOULD YOU LIKE ME TO DO? ENTER A COMMAND.')
    varResponse = str(input())
    if varResponse == list2[0]:
        readText()
    elif varResponse ==  list2[1]:
        programs()
    elif varResponse == list2[2]:
        browser()
    elif varResponse == list2[3]:
        url()
    elif varResponse == list2[4]:
        responseType()
    elif varResponse not in list2:
        misunderstanding()

def readText():
        print('ENTER YOUR TEXT: ')
        text = input()
        speaker.Speak(text)
        response = str(input('WOULD YOU LIKE TO ENTER MORE TEXT?'))
        if response == 'yes':
            readText()
        elif response == 'no':
            askAgain()
        else:
            print('I DON\'T UNDERSTAND, TRY AGAIN')
            readText()

用较小的程序进行第二次测试:

import random
import time
from win32com.client import constants
import win32com.client
import pythoncom
import webbrowser
import os
import sys
from sys import exit

speaker = win32com.client.Dispatch("SAPI.SpVoice")

var1 = "Read text"
var2 = str(input("say something."))
if var2 == var1:
    print("Sucess!")
else:
    print("FAIL!!!")

听写结果:

>>> 
say something.Read text 
FAIL!!!
>>> 

键盘结果:

>>> 
say something.Read text
Sucess!
>>> 
4

1 回答 1

0

我在 python 中创建了一个类似的程序,我通过让程序在听到“Jarvis”这个词后立即开始监听来解决这个问题。因此“现在几点”不会有响应,但“贾维斯,现在几点”或“贾维斯几点了”会有响应。

于 2013-10-08T11:57:23.683 回答