2013-07-26 93 views
7

我剛開始嘗試使用C#.Net中的Windows Speech to Text功能。我目前有基本的工作(IE - 說一些東西,它會根據你說的提供輸出)。然而,我正在努力弄清楚如何實際接收用戶輸入作爲變量。從語音中獲取用戶輸入?

什麼意思呢,就是例如。如果用戶說:

"Call me John" 

然後,我希望能夠繼續走字John作爲一個變量,然後存儲爲說,人員的用戶名。

我現在SpeechRecognized事件如下:

void zeusSpeechRecognised(object sender, SpeechRecognizedEventArgs e) 
    { 
     writeConsolas(e.Result.Text, username); 
     switch (e.Result.Grammar.RuleName) 
     { 
      case "settingsRules": 
       switch (e.Result.Text) 
       { 
        case "test": 
         writeConsolas("What do you want me to test?", me); 
         break; 
        case "change username": 
         writeConsolas("What do you want to be called?", me); 
         break; 
        case "exit": 
         writeConsolas("Do you wish me to exit?", me); 
         break; 
       } 
       break; 
     } 
    } 

NB:writeConsolas只是一個華而不實的附加行到RichTextBox

我想補充另一個case其執行以下操作:

case "call me" 
    username = e.Result.GetWordFollowingCallMe() //Obv not a method, but thats the general idea. 
    break; 

顯然,有沒有這樣的方法,但是這是我希望實現的總體思路。有沒有辦法搜索特定的短語(IE:Call me)並採取以下詞語?

編輯:我要指出的是,e.Result.Text只返回它可以匹配在字典文本中的單詞。

+0

+1爲一個理智的有趣的編程問題,但只是我的2美分:如果一臺計算機問我的名字,我的第一本能會與_just我的名_,響亮而明確的回答。不是「叫我Ishmael」或「我的名字是泥」或類似的東西......你可能想要測試,如果你下去,「給我打電話」的道路:) –

+1

@MiklosAubert感謝您的意見。不過,我打算實施它,以便用戶開始說「Call me blah」而不是提示 – JosephGarrone

回答

4

它看起來並不像您的情況e.Result.Text代表的東西,你可以列舉:您所檢查的啓動文本,而不是在其整個案文的話。在這樣的情況下,你不應該使用switch,和去的if鏈 - then - else!而非:

var text = e.Result.Text; 
if (text.StartsWith("test")) { 
    writeConsolas("What do you want me to test?", me); 
} else if (text.StartsWith("change username")) { 
    writeConsolas("What do you want to be called?", me); 
} else if (text.StartsWith("exit")) { 
    writeConsolas("Do you wish me to exit?", me); 
} else if (text.StartsWith("call me")) { 
    // Here you have the whole text. Chop off the "call me" part, 
    // using Substring(), and do whatever you need to do with the rest of it 
} else 
    ... 
4

那麼它不能在switch使用上e.Result.Text,因爲這將考驗在整個值:Call Me John

你應該有一個default情況下的條件,或者您switch

之外,但我真的重構了這一切,試圖避免switch或塊狀if..else if...else

const string Callme = "call me"; 
var text = e.Result.Text; 

switch (text) 
    { 
    case "test": 
     writeConsolas("What do you want me to test?", me); 
    break; 
    case "change username": 
     writeConsolas("What do you want to be called?", me); 
    break; 
    case "exit": 
     writeConsolas("Do you wish me to exit?", me); 
    break; 

    } 
    if (text.StartsWith(CallMe) 
     userName = text.Replace(CallMe, string.Empty).Trim(); 
2

我想看看更新您的語法使用SemanticValues,以便您可以直接提取結果,而不必通過識別結果進行解析。有演示SemanticValuesSemanticResultKeysSemanticResultValues一個簡單的例子here