gpt4 book ai didi

java - 与NLP的句子对比

转载 作者:搜寻专家 更新时间:2023-11-01 02:29:55 24 4
gpt4 key购买 nike

我使用 lingpipe 进行句子检测,但我不知道是否有更好的工具。据我所知,没有办法比较两个句子,看看它们是否表示同一件事。

有没有其他好的资源可以让我有一个预先构建的方法来比较两个句子,看看它们是否相似?

我的要求如下:

String sent1 = "Mary and Meera are my classmates.";

String sent2 = "Meera and Mary are my classmates.";

String sent3 = "I am in Meera and Mary's class.";

// several sentences will be formed and basically what I need to do is
// this

boolean bothAreEqual = compareOf(sent1, sent2);

sop(bothAreEqual); // should print true

boolean bothAreEqual = compareOf(sent2, sent3);

sop(bothAreEqual);// should print true

最佳答案

如何测试两个句子的意思是否相同:这将是一个过于开放的问题。

但是,有一些方法可以比较两个句子,看看它们是否相似相似性 有许多可能的定义,可以使用预构建的方法进行测试。

参见示例 http://en.wikipedia.org/wiki/Levenshtein_distance

Distance between 
'Mary and Meera are my classmates.'
and 'Meera and Mary are my classmates.':
6
Distance between
'Mary and Meera are my classmates.'
and 'Alice and Bobe are not my classmates.':
14
Distance between
'Mary and Meera are my classmates.'
and 'Some totally different sentence.':
29

代码:

public class LevenshteinDistance {

private static int minimum(int a, int b, int c) {
return Math.min(Math.min(a, b), c);
}

public static int computeDistance(CharSequence str1,
CharSequence str2) {

int[][] distance = new int[str1.length() + 1][str2.length() + 1];

for (int i = 0; i <= str1.length(); i++){
distance[i][0] = i;
}
for (int j = 0; j <= str2.length(); j++){
distance[0][j] = j;
}
for (int i = 1; i <= str1.length(); i++){
for (int j = 1; j <= str2.length(); j++){
distance[i][j] = minimum(
distance[i - 1][j] + 1,
distance[i][j - 1] + 1,
distance[i - 1][j - 1]
+ ((str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1));
}
}
int result = distance[str1.length()][str2.length()];
//log.debug("distance:"+result);
return result;
}


public static void main(String[] args) {
String sent1="Mary and Meera are my classmates.";
String sent2="Meera and Mary are my classmates.";
String sent3="Alice and Bobe are not my classmates.";
String sent4="Some totally different sentence.";

System.out.println("Distance between \n'"+sent1+"' \nand '"+sent2+"': \n"+computeDistance(sent1, sent2));
System.out.println("Distance between \n'"+sent1+"' \nand '"+sent3+"': \n"+computeDistance(sent1, sent3));
System.out.println("Distance between \n'"+sent1+"' \nand '"+sent4+"': \n"+computeDistance(sent1, sent4));

}
}

关于java - 与NLP的句子对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12053241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com