基本来自网上,但网上版本太多,也不知道哪个是原创的
有所精简和改进 | Const SLODynamic = 0 | | | | Dim CommandDictionary | | Dim WshShell | | Dim SR | | Dim Grammar | | | | | | Set CommandDictionary = CreateObject("Scripting.Dictionary") | | CommandDictionary.Add "计算器", "calc" | | CommandDictionary.Add "记事本", "notepad" | | CommandDictionary.Add "测试", "notepad ceshi.vbs" | | CommandDictionary.Add "打开键盘", "osk" | | | | Set WshShell = CreateObject("WScript.Shell") | | | | | | Set SR = WScript.CreateObject("SAPI.SpSharedRecoContext", "RecoContext_") | | Set Grammar = SR.CreateGrammar | | Grammar.Reset | | Grammar.CmdLoadFromFile "COMMAND.XML", SLODynamic | | Grammar.CmdSetRuleIdState 0, 1 | | | | Set objTTS = CreateObject( "SAPI.SpVoice" ) | | | | MsgBox "语音识别系统测试" | | WScript.Sleep 100000000 | | | | | | Sub RecoContext_Recognition(ByVal StreamNumber, ByVal StreamPosition, ByVal RecognitionType, ByVal Result ) | | Text = Result.PhraseInfo.GetText | | | | WScript.Sleep 1000 | | | | Set fso=CreateObject("scripting.filesystemobject").opentextfile("记录.log",2) | | fso.write Text & vbCrLf & "Text" | | | | If CommandDictionary.Exists(Text) Then | | objTTS.Speak "正在打开" & Text | | WshShell.Run CommandDictionary.Item(Text) | | End If | | | | If Text = "命令结束" Then | | objTTS.Speak "命令结束,再见!" | | WScript.Sleep 2000 | | Set objTTS = Nothing | | WScript.Quit | | End If | | End SubCOPY |
COMMAND.XML 的内容如下: | <?xml version="1.0" encoding="gb2312" ?><GRAMMAR LANGID="804"><RULE NAME="命令" TOPLEVEL="ACTIVE"><L> | | | | <P>计算器</P> | | <P>记事本</P> | | <P>打开键盘</P> | | <P>命令结束</P> | | | | </L></RULE></GRAMMAR>COPY |
|