我正在尝试在android中实现语音识别。
这里我有一个段落设置为TextView。 我有由“”空格分隔的单词数组列表。 我正在使用 onPartialResult 来获取用户所说的话。 我们得到这样的输入词
[I]
[I am]
[I am android]
[I am android user]
所以我通过按空格分割将其转换为单词数组列表 并只取数组的最后一个元素。所以每次我只会得到新词。
现在在获取新单词时,我必须将其与段落中的单词数组列表进行匹配。
我有一个全局索引,它在匹配时不断增加。 另外,在匹配时,我使用 spannable 在 TextView 的段落文本中获取索引。 然后为匹配的单词设置颜色。
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);
}
问题: 当说出“无忧无虑”之类的单词时,识别会返回“无忧无虑”,这会导致有两个元素[关心,自由],因此这使得匹配单词不合适。 虽然有时它会检测到carefree并给出一个单词carefree,但大多数时候它会返回carefree。我们如何在 android 中处理这个问题?
检测和匹配单词的好解决方案是什么。
脑海中浮现出最初的想法,这很可怕,而且似乎不明智。 如果我检查段落的单词数组“carefree”包含care,那么下一步检查carefree包含free,然后检查上一个和当前索引care和free是否相同,然后将它们连接并匹配。并获取在 TextView 中更改其颜色的位置。
如果有任何算法或库可以处理这个问题,我期待的另一件事。
请分享您的意见