之前使用拼音相同来寻找所录入学生,虽较直接结果对比成功率高,但是识别错误的情况也存在。查找百度提供包中可用内容,发现它提供了百度提供了一个工具,可以从拼音角度判断相似度,score越小说明拼音越相似,挺好用,非常适合本项目中已知姓名列表将识别结果从中匹配。使用该工具会进一步提高正确率。百度提供工具类Search,将其添加方法写作工具类。
首先添加拼音依赖:
com.github.stuxuhai jpinyin 1.1.8
工具类具体内容:
package org.jeecg.modules.demo.ScoresInput.controller;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Search {
final List targets = new ArrayList();
public Search(String[] list) throws PinyinException {
for (String s : list) {
Word w = new Word(s);
targets.add(w);
}
}
public List search(String input, int limit) throws PinyinException {
Word w = new Word(input);
return targets.stream().map(x -> {
Score s = new Score();
s.word = x;
s.score = x.compareTo(w);
return s;
}).sorted().limit(limit).collect(Collectors.toList());
}
public static int getEditDistance(String s, String t) {
int d[][]; // matrix
int n; // length of s
int m; // length of t
int i; // iterates through s
int j; // iterates through t
char s_i; // ith character of s
char t_j; // jth character of t
int cost; // cost
// Step 1
n = s.length();
m = t.length();
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
d = new int[n + 1][m + 1];
// Step 2
for (i = 0; i <= n; i++) {
d[i][0] = i;
}
for (j = 0; j <= m; j++) {
d[0][j] = j;
}
// Step 3
for (i = 1; i <= n; i++) {
s_i = s.charAt(i - 1);
// Step 4
for (j = 1; j <= m; j++) {
t_j = t.charAt(j - 1);
// Step 5
cost = (s_i == t_j) ? 0 : 1;
// Step 6
d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1,
d[i - 1][j - 1] + cost);
}
}
// Step 7
return d[n][m];
}
private static int Minimum(int a, int b, int c) {
int im = a < b ? a : b;
return im < c ? im : c;
}
class Word implements Comparable {
final String word;
final String pinyin1;
final String pinyin2;
Word(String word) throws PinyinException {
this.word = word;
this.pinyin1 = PinyinHelper.convertToPinyinString(word, ",", PinyinFormat.WITH_TONE_NUMBER);
this.pinyin2 = PinyinHelper.convertToPinyinString(word, ",", PinyinFormat.WITHOUT_TONE);
}
@Override
public String toString() {
return word;
}
@Override
public int compareTo(Object o) {
if (o instanceof Word) {
Word o1 = (Word) o;
int score1 = getEditDistance(this.pinyin1, o1.pinyin1);
int score2 = getEditDistance(this.pinyin2, o1.pinyin2);
return score1 + score2;
}
return 0;
}
}
class Score implements Comparable {
Word word;
int score;
@Override
public int compareTo(Object o) {
if (o instanceof Score) {
return score - ((Score) o).score;
}
return 0;
}
//词组和相似度
@Override
public String toString() {
return "{" +
"word=" + word +
", score=" + score +
'}';
}
}
//将姓名数组和识别结果传入,得到最相似的结果
public static String getSimilarWord(String input,String[] arr){
try {
Search d = new Search(arr);
List list=d.search(input,10);
System.out.println("拼音相似度列表:"+list);
if(list!=null&&list.size()>0){
Search.Score score=list.get(0);
return score.word.toString();
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
使用getSimilarWord(String input,String[] arr)每次语音录入都会有最相似得匹配结果,不会录入无效内容。



