在我的应用程序中对语音识别结果使用 AND 和 OR
Using AND and OR on speech recognition result in my app
我正在为我的游戏使用 google 语音识别 API,我想计算用户说 "my forced" 的次数,但如果用户说 "my unforced"
为此,我使用此代码:
//save the result from speech recognition
public void onResults(Bundle arg0) {
Log.i(TAG, "on results");
ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String s = "";
for (String result:matches)
s += result + "\n";
Log.i(TAG,"results: "+ matches.get(0));
Log.i("what I said",s);
gamePoints(s);
}
//count "s" if it adheres to the condition
public void gamePoints(String s) {
if ((s.contains("my")) &&
(s.contains("fau")||s.contains("for")) &&
(!s.contains("on")||!s.contains("un"))){
count++;
}
}
我在日志中的发言结果是:
我的非受迫
玛雅森林
我的强制
强迫症
我自己的强迫症
如果我的代码没问题,它不会算那句话,但出于某种原因它算了。
我的条件逻辑是:每次"s"中的字母包含("my")和("for"或"fau")的时候都统计为长"s" 不包含 ("on" 或 "un")
有人可以解释一下我做错了什么吗?
谢谢!
这是我根据您更新后的逻辑编写的if
声明:
if ( s.contains("my") &&
(s.contains("fau") || s.contains("for")) &&
(!s.contains("on") && !s.contains("un")) ) {
count++;
}
请注意,ORed 项在括号中。此外,根据 DeMorgan 定律,doesn't contain ("on" or "un")
的反义词是 doesn't contain "on" AND doesn't contain "un"
。
我正在为我的游戏使用 google 语音识别 API,我想计算用户说 "my forced" 的次数,但如果用户说 "my unforced"
为此,我使用此代码:
//save the result from speech recognition
public void onResults(Bundle arg0) {
Log.i(TAG, "on results");
ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String s = "";
for (String result:matches)
s += result + "\n";
Log.i(TAG,"results: "+ matches.get(0));
Log.i("what I said",s);
gamePoints(s);
}
//count "s" if it adheres to the condition
public void gamePoints(String s) {
if ((s.contains("my")) &&
(s.contains("fau")||s.contains("for")) &&
(!s.contains("on")||!s.contains("un"))){
count++;
}
}
我在日志中的发言结果是:
我的非受迫
玛雅森林
我的强制
强迫症
我自己的强迫症
如果我的代码没问题,它不会算那句话,但出于某种原因它算了。
我的条件逻辑是:每次"s"中的字母包含("my")和("for"或"fau")的时候都统计为长"s" 不包含 ("on" 或 "un")
有人可以解释一下我做错了什么吗? 谢谢!
这是我根据您更新后的逻辑编写的if
声明:
if ( s.contains("my") &&
(s.contains("fau") || s.contains("for")) &&
(!s.contains("on") && !s.contains("un")) ) {
count++;
}
请注意,ORed 项在括号中。此外,根据 DeMorgan 定律,doesn't contain ("on" or "un")
的反义词是 doesn't contain "on" AND doesn't contain "un"
。