欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

查询文档标题相似度

程序员文章站 2022-07-14 22:23:50
...
/**
* 文档相似度查询
*/
@SuppressWarnings("unchecked")
@Override
public List<MyLibraryModel> queryDocList(MyLibraryModel model) {
List<MyLibraryModel> list = new ArrayList<MyLibraryModel>();
List<MyLibraryModel> librarylist = dao.queryDocListItems(model);
for (int c = 0; c < librarylist.size(); c++) {
MyLibraryModel myModel = librarylist.get(c);
String str1 = myModel.getLibTitle();
String str2 = model.getLibTitle();
// 计算两个字符串的长度。
int len1 = str1.length();
int len2 = str2.length();

// 建立上面说的数组,比字符长度大一个空间
int[][] dif = new int[len1 + 1][len2 + 1];
// 赋初值,步骤B。
for (int a = 0; a <= len1; a++) {
dif[a][0] = a;
}
for (int a = 0; a <= len2; a++) {
dif[0][a] = a;
}
// 计算两个字符是否一样,计算左上的值
int temp;
for (int k = 1; k <= len1; k++) {
for (int j = 1; j <= len2; j++) {
if (str1.charAt(k - 1) == str2.charAt(j - 1)) {
temp = 0;
} else {
temp = 1;
}
// 取三个值中最小的
dif[k][j] = min(dif[k - 1][j - 1] + temp,
dif[k][j - 1] + 1, dif[k - 1][j] + 1);
}
}
// 取数组右下角的值,同样不同位置代表不同字符串的比较
// 计算相似度
float similarity = 1 - (float) dif[len1][len2]
/ Math.max(str1.length(), str2.length());
if (similarity > 0) {
myModel.setSimilarity(similarity);
list.add(myModel);
}
}
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
float one = ((MyLibraryModel) a).getSimilarity();
float two = ((MyLibraryModel) b).getSimilarity();
if (one > two)
return 0;
return 1;
}
});
if (list.size() > 4) {
return list.subList(0, 4);
}
return list;
}

/**
* 得到最小值
*
* @param is
* @return
*/
private static int min(int... is) {
int min = Integer.MAX_VALUE;
for (int i : is) {
if (min > i) {
min = i;
}
}
return min;
}