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

时间:2017-03-14 11:30:11

标签: java android speech-recognition google-speech-api

我正在使用Googles这个api: -

  

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

用于语音识别,并且工作得非常好。

问题在于数字,例如,如果我说one two three four,结果将为1234 如果我说one thousand two hundred thirty four,结果仍为1234

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

我知道我们无法控制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}

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 + " ";
          }
      }
  }

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

更新

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

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

还尝试了speech_context,但也没有效果。

2 个答案:

答案 0 :(得分:2)

看看这个question and answer

您可以提供API&#34;语音环境&#34;提示,像这样:

"speech_context": {
  "phrases":["zero", "one", "two", ... "nine", "ten", "eleven", ... "twenty", "thirty,..., "ninety"]
 }

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

"speech_context": {
  "phrases":["eins", "zwei", "drei", ..., "elf", "zwölf" ... ]
 }

答案 1 :(得分:0)

您可能需要自己将数字(不是数字)转换为单词。由于大多数语言都有一些逻辑(例如英语,德语),您可以使用算法方法来实现这一点。

请参阅 How to convert number to words in java