Browse Source

Merge branch 'wyp/0123-similarCheck' of Server/long-article-recommend into master

wangyunpeng 3 months ago
parent
commit
f11982eb57

+ 9 - 5
long-article-recommend-service/src/main/java/com/tzld/longarticle/recommend/server/util/TitleSimilarCheckUtil.java

@@ -58,13 +58,17 @@ public class TitleSimilarCheckUtil {
         if (titleA.isEmpty() || titleB.isEmpty()) {
             return false;
         }
-        int commonCount = 0;
-        for (Character c : titleA) {
-            if (titleB.contains(c)) {
-                commonCount++;
+        // 确保遍历较小的集合以优化性能
+        Set<Character> smaller = titleA.size() < titleB.size() ? titleA : titleB;
+        Set<Character> larger = titleA.size() < titleB.size() ? titleB : titleA;
+
+        int intersectionCount = 0;
+        for (Character c : smaller) {
+            if (larger.contains(c)) {
+                intersectionCount++;
             }
         }
-        double rate = commonCount / (double) Math.min(titleA.size(), titleB.size());
+        double rate = intersectionCount / (double) smaller.size();
         return rate >= threshold;
     }