我正在使用谷歌这个API:-
"+ language_code"& key="我的密钥"
用于语音识别,并且效果非常好。
问题在于数字,即如果我说one two three four
结果将是1234
如果我说one thousand two hundred thirty four
结果还是1234
.
另一个问题是对于其他语言,即这个词elf
德语是eleven
。如果你说elf
结果是11
,而不是精灵。
我知道我们无法控制 api,但是我们可以向此 api 添加任何参数或 hack 来强制它仅返回单词。
有时响应会产生正确的结果,但并非总是如此。
这些是示例响应
1)当我说“一二三四”时
{"result":[{"alternative":[{"transcript":"1234","confidence":0.47215959},{"transcript":"1 2 3 4","confidence":0.25},{"transcript":"one two three four","confidence":0.25},{"transcript":"1 2 34","confidence":0.33333334},{"transcript":"1 to 34","confidence":1}],"final":true}],"result_index":0}
2)当我说“一千二百三十四”时
{"result":[{"alternative":[{"transcript":"1234","confidence":0.94247383},{"transcript":"1.254","confidence":1},{"transcript":"1284","confidence":1},{"transcript":"1244","confidence":1},{"transcript":"1230 4","confidence":1}],"final":true}],"result_index":0}
我做了什么。
检查结果是否为数字,然后将每个数字按空格分开,并检查结果数组中是否存在相同的序列。在这个例子中结果 1234 变为 1 2 3 4,将搜索结果数组中是否有相似的序列,然后将其转换为单词。在第二种情况下,没有 1 2 3 4,因此将保留原始结果。
这是代码。
String numberPattern = "[0-9]";
Pattern r1 = Pattern.compile(numberPattern);
Matcher m2 = r1.matcher(output);
if (m2.find()) {
char[] digits2 = output.toCharArray();
String digit = "";
for (char c: digits2) {
digit += c + " ";
}
for (int i = 1; i < jsonArray2.length(); i++) {
String value = jsonArray2.getJSONObject(i).getString("transcript");
if (digit.trim().equals(value.trim())) {
output = digit + " ";
}
}
}
所以问题是,当我“说十三四八”时,这种方法会将 13 分成一三,因此不是一个可靠的解决方案。
Update
我尝试了新的云视觉 api (https://cloud.google.com/speech/ https://cloud.google.com/speech/)并且比 v2 好一点。结果为one two three four
就语言本身而言,我的解决方法也有效。但当我说thirteen four eight
仍然和 v2 中的结果相同。
而且 elf 在德语中仍然是 11 岁。
也尝试过speech_context
那也没用。