少し前に、(映画の) アイアンマンの JARVIS に少し似たプログラムを書きました。このプログラムは、「プログラムを開く」や「テキストを読む」などのコマンドを受け取り、適切なアクションを実行します。キーボードまたは音声認識のいずれかを介してコマンドを入力できるように記述しました。私が抱えている問題は、プログラムが口述コマンドを受け入れないことです。「テキストを読む」と入力すると、コマンドに従います。音声認識を使用して「Read text」と入力すると、「I DON'T UNDERSTAND」と表示されます。まったく同じテキストが入力されていることに注意してください。この交絡問題に関するヘルプは大歓迎です。また、古くて乱雑なコードをお許しください。
コマンド部分までの最初のコードは次のとおりです。
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()
より小さいプログラムでの 2 番目のテスト:
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!
>>>