我對兩個句子之間的單詞相似度有疑問。例如:杰克去打籃球。杰克去籃球比賽我想知道通過將相同的單詞數除以長句的單詞數來找到相似率的代碼。我應該使用哪個庫,謝謝。
There are many kinds of similarity as similarity. I want to know which similarity title this similarity belongs to.
uj5u.com熱心網友回復:
您可以使用一些類似的檔案技術,例如Cosine similariy
在這里,我根據您的描述實施了一個解決方案。
double findSimilarityRatio (String sentence1, String sentence2) {
HashMap<String, Integer> firstSentenceMap = new HashMap<>();
HashMap<String, Integer> secondSentenceMap = new HashMap<>();
String[] firstSentenceWords = sentence1.split(" ");
String[] secondSentenceWords = sentence2.split(" ");
for (String word : firstSentenceWords) {
if (firstSentenceMap.containsKey(word)) {
firstSentenceMap.put(word, firstSentenceMap.get(word) 1);
}
else {
firstSentenceMap.put(word, 1);
}
}
for (String word : secondSentenceWords) {
if (secondSentenceMap.containsKey(word)) {
secondSentenceMap.put(word, secondSentenceMap.get(word) 1);
}
else {
secondSentenceMap.put(word, 1);
}
}
double totalWords = 0;
double totalHits = 0;
if (firstSentenceWords.length >= secondSentenceWords.length) {
totalWords = firstSentenceWords.length;
for (Map.Entry<String, Integer> entry : firstSentenceMap.entrySet()) {
String key = entry.getKey();
if (secondSentenceMap.containsKey(key)) {
totalHits = totalHits Math.min(secondSentenceMap.get(key), firstSentenceMap.get(key));
}
}
}
else {
totalWords = secondSentenceWords.length;
for (Map.Entry<String, Integer> entry : secondSentenceMap.entrySet()) {
String key = entry.getKey();
if (firstSentenceMap.containsKey(key)) {
totalHits = totalHits Math.min(secondSentenceMap.get(key), firstSentenceMap.get(key));
}
}
}
return totalHits/totalWords;
}
希望有幫助,加油!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/526139.html
標籤:爪哇弹簧靴
