2012-06-23 33 views
1

我正在爲iOS設計一個語音控制程序,並使用Pocketsphinx作爲我的識別引擎。我希望它能識別口頭命令是否包含單詞「Morning」,並用morningGreetings數組中的某個短語作出迴應。我的代碼看起來像這個 -讓PocketSphinx識別口頭字符串是否包含單詞?

if([hypothesis rangeOfString:@"morning"].location == !NSNotFound) { 
    NSString *text= [morningGreetings objectAtIndex:arc4random() % [morningGreetings count]];; 
    [self.fliteController say:[NSString stringWithFormat:text] withVoice:self.firstVoiceToUse]; 
} 

然而,與此代碼識別器只exectues的命令,如果「早晨」是口語字符串中的第一個字。我希望它能迴應「早上好」,「早上好,不是嗎」,「今天早上好嗎?」等等。爲了達到這個目的我可以改變什麼?

+0

哦,我從來不知道PocketSphinx。很好找! [這是一個關於如何將它集成到iOS應用程序的教程](http://www.rajeevan.co.uk/pocketsphinx_in_iphone/)。 –

回答

1

我希望它能識別口頭命令是否包含單詞「Morning」(...)但是,如果「早上」是口頭字符串中的第一個單詞,則識別器僅執行該命令。

你的條件location == !NSNotFound相當於location == 0因爲!NSNotFound等於0,因此只執行,如果「早晨」是字符串中的第一個詞。你想要的是location != NSNotFound

變化的條件,像這樣:

if ([hypothesis rangeOfString:@"morning"].location != NSNotFound) { ... } 
+0

像魅力一樣工作。謝謝。 – kdsprogrammer

相關問題