package com.tzld.piaoquan.growth.common.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;

public class TitleSimilarCheckUtil {

    public static final double SIMILARITY_THRESHOLD = 0.7;

    public static Set<Character> makeCache(String title) {
        title = title.trim().replace("\u200b", "");
        Set<Character> cacheSet = new HashSet<>(title.length());
        for (char c : title.toCharArray()) {
            cacheSet.add(c);
        }
        return cacheSet;
    }

    public static List<Set<Character>> makeCache(List<String> titles) {
        List<Set<Character>> cache = new ArrayList<>(titles.size());
        for (String title : titles) {
            cache.add(makeCache(title));
        }
        return cache;
    }

    public static boolean isDuplicateContentByCache(String title, List<Set<Character>> existsContentCache, double threshold) {
        if (CollectionUtils.isEmpty(existsContentCache)) {
            return false;
        }
        Set<Character> titleCache = makeCache(title);
        for (Set<Character> existTitleCache : existsContentCache) {
            if (isSimilar(titleCache, existTitleCache, threshold)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isDuplicateContent(String title, List<String> existsContentTitle, double threshold) {
        boolean result = false;
        if (CollectionUtils.isEmpty(existsContentTitle)) {
            return result;
        }
        title = title.trim().replace("\u200b", "");
        for (String existsTitle : existsContentTitle) {
            if (isSimilar(title, existsTitle, threshold)) {
                result = true;
                break;
            }
        }
        return result;
    }

    public static boolean isSimilar(Set<Character> titleA, Set<Character> titleB, double threshold) {
        if (titleA.isEmpty() || titleB.isEmpty()) {
            return false;
        }
        // 确保遍历较小的集合以优化性能
        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 = intersectionCount / (double) smaller.size();
        return rate >= threshold;
    }

    public static boolean isSimilar(String titleA, Set<Character> titleB, double threshold) {
        Set<Character> setA = makeCache(titleA);
        return isSimilar(setA, titleB, threshold);
    }

    public static boolean isSimilar(String titleA, String titleB, double threshold) {
        Set<Character> setA = makeCache(titleA);
        Set<Character> setB = makeCache(titleB);
        return isSimilar(setA, setB, threshold);
    }

    public static void main(String[] args) {
        String title = "多子女家庭，老人大概率过得比独子家庭的要幸福";
        List<String> existsContentTitle = Arrays.asList("以后买房，请记住7字真言：“买旧、买大、不买三！”",
                "人到晚年才明白：多子女家庭，老人大概率过得比独子家庭的要幸福",
                "可供中国使用3800年？山东意外发现巨大宝藏，西方当场酸了！",
                "陕西女孩去医院体检后，发现左肾不见了，意外牵出8年前手术疑云");
        boolean result = isDuplicateContent(title, existsContentTitle, SIMILARITY_THRESHOLD);
        System.out.println(result);

        List<Set<Character>> titlesCache = makeCache(existsContentTitle);
        result = isDuplicateContentByCache(title, titlesCache, SIMILARITY_THRESHOLD);
        System.out.println(result);

        title = "江苏高考文科女状元，遭多所985名校拒绝录取，成为“最惨状元”";
        result = isDuplicateContentByCache(title, titlesCache, SIMILARITY_THRESHOLD);
        System.out.println(result);
    }
}
