有没有办法强制Google Speech api只返回单词作为回复?

sun*_*nny 16 java android speech-recognition google-speech-api

我正在使用Googles这个api: -

https://www.google.com/speech-api/v2/recognize?output=json&lang="+ language_code +"&key ="我的密钥"

用于语音识别,它的工作非常好.

问题在于数字,即,如果我说one two three four结果将是 1234 ,如果我说one thousand two hundred thirty four结果仍然是1234.

另一个问题是使用其他语言,即elf德语中的单词eleven.如果你说elf结果是11,而不是精灵.

我知道我们无法控制api但是有任何参数或黑客可以添加到这个api以强制它只返回单词.

有时候响应的结果是正确的,但并非总是如此.

这些是样本回复

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}
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

我做了什么.

检查结果是否为数字,然后按空格分割每个数字并检查结果数组中是否有相同的序列.在此结果中,结果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 + " ";
          }
      }
  }
Run Code Online (Sandbox Code Playgroud)

所以问题是,当我"说十三四"时,这种方法将13分为三,因此不是一个可靠的解决方案.

更新

我尝试了新的云视觉API(https://cloud.google.com/speech/),它比v2好一点.结果one two three four就是单词本身,我的解决方法也是如此.但是当我说它thirteen four eight仍然与v2中的结果相同时.

精灵在德语中仍然是11岁.

也尝试过speech_context也没用.

bla*_*ert 2

看看这个问题和答案

\n\n

您可以向 API 提供“语音上下文”提示,如下所示:

\n\n
"speech_context": {\n  "phrases":["zero", "one", "two", ... "nine", "ten", "eleven", ... "twenty", "thirty,..., "ninety"]\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想这也适用于其他语言,比如德语。

\n\n
"speech_context": {\n  "phrases":["eins", "zwei", "drei", ..., "elf", "zw\xc3\xb6lf" ... ]\n }\n
Run Code Online (Sandbox Code Playgroud)\n