package com.agent.management.rag.capability;

import java.util.*;

public final class RagTextSimilarity {
    private RagTextSimilarity() {}

    public static double score(String left, String right) {
        Set<String> a = tokens(left); Set<String> b = tokens(right);
        if (a.isEmpty() || b.isEmpty()) return 0;
        long intersection = a.stream().filter(b::contains).count();
        return intersection / (double) Math.max(1, Math.min(a.size(), b.size()));
    }

    public static Set<String> tokens(String text) {
        if (text == null) return Set.of();
        String normalized = text.toLowerCase(Locale.ROOT).replaceAll("[^\\p{L}\\p{N}_]+", " ").trim();
        Set<String> tokens = new LinkedHashSet<>();
        for (String part : normalized.split("\\s+")) {
            if (part.isBlank()) continue;
            tokens.add(part);
            if (part.codePointCount(0, part.length()) > 1) {
                int[] points = part.codePoints().toArray();
                for (int i = 0; i < points.length - 1; i++) {
                    tokens.add(new String(points, i, 2));
                }
            }
        }
        return tokens;
    }
}
