我正在尝试在android中实现语音识别。
这里我有一个段落设置为TextView。 我有由“”空格分隔的单词数组列表。 我正在使用 onPartialResult 来获取用户所说的话。 我们得到这样的输入词
[I]
[I am]
[I am android]
[I am android user]
所以我通过按空格分割将其转换为单词数组列表 并只取数组的最后一个元素。所以每次我只会得到新词。
现在在获取新单词时,我必须将其与段落中的单词数组列表进行匹配。
我有一个全局索引,它在匹配时不断增加。 另外,在匹配时,我使用 spannable 在 TextView 的段落文本中获取索引。 然后为匹配的单词设置颜色。
问题:
[here]
[here there]
目前看来,在按空格分割后,我还必须检查此类符号并再次分割它。
检测和匹配单词的好解决方案是什么。
Spannable 文本和 TextView 文本内容应该相同,以便我们可以正确应用颜色。所以在匹配时我们不能修改我们用来显示的段落文本。
这是我现在正在使用的代码
private void applyColor(String param){
ArrayList<String> params = paraToArray(param);
// from string only get last word
String newWord = params.get(params.size()-1);
// get current word of paragraph by using global index tracking
String currentWord_FromParagraph = paragraphWords.get(indexHistory);
// handle 's words
if(currentWord_FromParagraph.contains("'s")){
String rootWord = currentWord_FromParagraph.replaceAll("'s\\b", "");
newWord = rootWord+"'s";
}
// index of word on paragraph
int start = paragraph.indexOf(newWord, indexHistoryChar);
int end = newWord.length();
// match
if(start != -1 && newWord.equalsIgnoreCase(currentWord_FromParagraph) ){
// spannable contains paragraph and its global
spannableString.setSpan(new ForegroundColorSpan(Color.GREEN), start,
start+end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// global index for array of words of paragraph. after matching ignore current index so we look for next word
indexHistory++;
// similar as above but it is for paragraph character index
indexHistoryChar = start+end;
spannableString.setSpan(new ForegroundColorSpan(Color.WHITE), startNext, endNext, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
txtv_paragraph.setText(spannableString);
}
我找到了匹配发音相似的单词的解决方案。 apache 的 Soundex 和另一个是 Google 的 Metaphone3 它很容易匹配这样的词 或 == 我们的, 基肖尔的==基肖尔, 天 == 天 通过==再见 y==为什么 堆栈.: == 堆栈 (我使用 Metaphone3 得到了上述匹配,而 Soundex 不匹配某些单词,甚至像 y 和 Why,但匹配 bye==bye,或 ==our) 您可以根据使用情况使用 contains 和 equals 进行更多调整。
对于apache的Soundex 将其添加到 build.gradle
// https://mvnrepository.com/artifact/commons-codec/commons-codec
implementation 'commons-codec:commons-codec:1.16.0'
适用于Metaphone3 使用这个类并调用它的函数。您需要删除 main 方法和包名称才能将其用作模块 https://github.com/OpenRefine/OpenRefine/blob/master/main/src/com/google/refine/clustering/binning/Metaphone3.java
但是仍然存在一个问题,即当我们将单词作为单个单词时,如何通过语音识别处理单词分割。即我们有“无忧无虑”,但认可返回[关心,自由]。 所以需要处理它。