I’m iterating through an array list of voice data to decipher if the user answered ‘yes’ or ‘no’. Simple hey…
This is the initial check I have to detect an unclear response that contains both ‘yes’ and ‘no’. It works perfectly, but just looking at it, I know I should be embarrassed to post it and it can be greatly simplified!
if ((element.toString().startsWith("yes ")
|| element.toString().endsWith(" yes")
|| element.toString().contains(" yes "))
&& (element.toString().startsWith("no ")
|| element.toString().endsWith(" no")
|| element.toString().contains(" no "))) {
// I heard both yes and no - inform user I don't understand
I want the user to be able to accept or decline using whatever natural speech they desire and therefore need to consider the unlikely eventualities of the following appearing in the array data:
- yes no
- sey on
- yes knocking
- yesterday no
- yesterday knocking
- bayes theorem porno
I’ve been through so many regular expression posts and tutorials, but no matter what I do, I cannot find a solution that works better than the posted code. The white space [\\s] being there or ‘|’ not, I can’t resolve…
I thank you in advance for your help!
If you want just the word “yes” or “no” (i.e. “bayes theorem porno” and the “yesterday” ones don’t match) then you can use
\bas a boundary character in a regex:PatternJavaDoc, Boundaries tutorialAssuming you’re already lower-casing the input then this should work:
Test code: