我已经使用 WS4J 实现了一个句子相似度方法。
我读过文章中的句子相似度,它基于两个句子中的单词相似度。但是我找不到基于单词相似度计算并返回整个句子相似度的单个值的方法。
在这个网站上的sentence-similarity-using-ws4j上提出了类似的问题
如您所见,我已经设法使用 WS4J 进行编码,直到句子 a 中的任何单词在另一个句子中找到同义词集匹配(并且匹配值高于 0.9)都返回匹配消息。但我猜这不是一个好方法。
我找到了 Yuhua 等 [2] 的文章。所有这些都非常有用,但无法弄清楚他们用于整体句子相似性的方法。
public static String sentenceSim(String se1, String se2, RelatednessCalculator rc) {
String similarityMessage = "";
String similarityMessage2 = "";
if (se1 == null || se2 == null) {
return "null";
}
if (nlp == null) {
nlp = OpenNLPSingleton.INSTANCE;
}
// long t00 = System.currentTimeMillis();
String[] words1 = nlp.tokenize(se1); // base
String[] words2 = nlp.tokenize(se2); // sentence
String[] postag1 = nlp.postag(words1);
String[] postag2 = nlp.postag(words2);
String u = "";
int matchCount = 0;
int counter = 0;
String mLC = rc.toString().toLowerCase();
for (int j = 0; j < words2.length; j++) { // sentence
String pt2 = postag2[j];
String w2 = MorphaStemmer.stemToken(words2[j].toLowerCase(), pt2);
POS p2 = mapPOS(pt2);
// System.out.print(words2[j]+"(POS "+pt2+")");
for (int i = 0; i < words1.length; i++) { // base
String pt1 = postag1[i];
String origWord1 = words1[i];
String origWord2 = words2[j];
String w1 = MorphaStemmer.stemToken(words1[i].toLowerCase(), pt1);
POS p1 = mapPOS(pt1);
String popup = mLC + "( " + w1 + "#" + (p1 != null ? p1 : "INVALID_POS") + " , " + w2 + "#"
+ (p2 != null ? p2 : "INVALID_POS") + ")";
String dText;
// boolean acceptable = rc.getPOSPairs().isAcceptable(p1, p2);
// ALL WORDS FROM BASE HAS TO MATCH - IF ONE DOESNT,
// THEN ITS NOT MATCH
double d = -1;
if (p1 != null && p2 != null) {//
double r = wordSim(w1, w2, rc);
if (r > 0.9) {
matchCount++;
similarityMessage += "\t\t Similarity Found (Base : sentence) ('Base Word: " + origWord1 + "=" + w1 + " "
+ p1 + "', Sentence Word: '" + origWord2 + "=" + w2 + " " + p2 + "') = " + r + "\n";
System.out.println(similarityMessage);
}
}
}
// System.out.println();
}
// output if all words in sentence 1 have found matches in sentences 2
if (matchCount == words1.length) {
similarityMessage2 = "\t\tFound all matches for base in sentence: ";
System.out.println("\t\tBase " + se1);
System.out.println("\t\tFound all matches for base in sentence: ");
System.out.println(similarityMessage);
}
similarityMessage = "";
return similarityMessage;
}
我已经用 Java 完成了我的代码,所以我一直在寻找一些 Java 实现。
[2]:Li, Y., McLean, D., Bandar, ZA, O'shea, JD, & Crockett, K. (2006)。基于语义网和语料库统计的句子相似度。知识和数据工程,IEEE Transactions on,18(8),1138-1150。