Community,
I want to write code for an Android App that allows the Android SpeechRecognizer to recognize a sequence of spoken numbers as dart Scores (reported in three numbers in quick succession). The main Problem is, that if you say two numbers in quick succession, android logs them together (e.g. "three four fifteen" --> '34 15'. It's nice, that android logs multiple possible results but sadly it doesn't help for this problem (See examples below).
The easiest workaround would be, if there was some way to get android to log the spoken numbers in words not in numbers.
Two main things I have tried:
- Searching through the different results that SpeechRecognizer returns and parse to find the best fitting one.
- Split the string at a certain word
Concerning(2)
I chose the word "and" for splitting the string. I am writing this app for a German audience and there are german numbers that contain "and" (e.g. 25 is "five and twenty"). I could of course use another word but mainly this solution is rather inelegant and not user friendly.
Relevant Code:
private void processResult(List<String> results) {
String res = results.get(0).toLowerCase();
for (String single:
results) {
Log.d("HelloStackflow", single);
}
String[] repeatWords = new String[]{"korrigieren", "quatsch", "korrigiere" , "noch mal"};
int roundScore;
if (stringContainsItemFromList(res, repeatWords)) {
startRecording();
return;
}
String[] singles = res.split("und|&");
if (singles.length > 3) {
speak("Mehr als 3 Zahlen, Sorry.");
return;
}
else if (singles.length < 3) {
speak("Weniger als 3 Zahlen, Sorry");
return;
}
throw1 = analyzeThrow(singles[0], score);
throw2 = analyzeThrow(singles[1], score);
throw3 = analyzeThrow(singles[2], score);
Integer[] throwList = new Integer[]{throw1, throw2, throw3};
for (int anyThrow:
throwList) {
if (anyThrow == -999)
return;
}
roundScore = throw1 + throw2 + throw3;
score = score-roundScore;
scoreView.setText(String.valueOf(score));
speak("Du hast "+ singles[0] + ", " + singles[1] + " und " + singles[2] + " geworfen.");
}
private int analyzeThrow(String s, int currentScore) {
if (stringContainsItemFromList(s, new String[]{"nichts", "daneben"}))
return 0;
int cleanS = invalidThrowException(s);
if (cleanS < 1 | cleanS > 60) {
speak(s+" ist kein gültiger Wurf!");
return -999;
}
if (stringContainsItemFromList(s, new String[]{"doppel", "double"}))
return 2*cleanS;
else if (stringContainsItemFromList(s, new String[]{"triple", "dreifach"}))
return 3*cleanS;
else
return cleanS;
}
private int invalidThrowException(String s) {
try {
return Integer.parseInt(s.replaceAll("[^0-9]", ""));
} catch (NumberFormatException e) {
return -999;
}
}
public static boolean stringContainsItemFromList(String inputStr, String[] items)
{
for (String item : items) {
if (inputStr.contains(item)) {
return true;
}
}
return false;
}
So I would love if my program would log "three five twentyfive" when I say it. Examples for what is being recognized:
This one would work: "double three and seven and triple twenty"
2019-01-08 22:41:16.367 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: double 3 und 7 und Triple 20
2019-01-08 22:41:16.368 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Double 3 und 7 und Triple 20
2019-01-08 22:41:16.368 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: double 3 und 7 und Triple XX
2019-01-08 22:41:16.368 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Double 3 und 7 und Triple XX
2019-01-08 22:41:16.368 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: double 3 und 7 und Triple 20 Uhr
For "three five seven"
2019-01-08 22:38:10.540 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 357
2019-01-08 22:38:10.540 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 3.57
2019-01-08 22:38:10.540 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 35.7
2019-01-08 22:38:10.540 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 3-5-7
2019-01-08 22:38:10.540 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 3.5.7
for "five and twenty and four" (for the problem with 25 see above)
2019-01-08 22:39:16.583 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 25 und 4
2019-01-08 22:39:16.583 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 25 und vier
2019-01-08 22:39:16.583 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: fünfundzwanzig und vier
2019-01-08 22:39:16.583 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: fünfundzwanzig und 4
2019-01-08 22:39:16.583 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: 25 & 4
And of course the onehunderedandeeeeiiightyy
2019-01-08 22:31:19.997 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Triple 20 Triple 20 Triple 20
2019-01-08 22:31:19.997 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Triple 20 Triple 20 Triple XX
2019-01-08 22:31:19.997 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Triple XX Triple XX Triple XX
2019-01-08 22:31:19.997 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Triple XX Triple 20 Triple 20
2019-01-08 22:31:19.997 26075-26075/com.classikonline.dartv2 D/HelloStackoverflow: Triple 20 Triple 20 Triple 20 Uhr
Thank you all very much in advance!