package com.agent.management.rag.capability;

import org.springframework.stereotype.Component;
import java.util.*;
import java.util.regex.*;

@Component
public class RagEntityMentionExtractor {
    private static final Pattern IDENTIFIER = Pattern.compile(
            "(?<![A-Za-z0-9_])(?:[A-Z][A-Z0-9]*_)+(?:[A-Z0-9]+)(?![A-Za-z0-9_])");
    private static final Pattern QUOTED = Pattern.compile("[\"'“‘]([^\"'”’]{2,80})[\"'”’]");
    private static final Pattern DOMAIN = Pattern.compile("[\\p{IsHan}A-Za-z0-9_-]{2,30}(?:号|舰|船|队|组|任务|阶段|事件|机构|部门)");
    public List<String> extract(String question) {
        LinkedHashSet<String> result = new LinkedHashSet<>();
        add(question, IDENTIFIER, result); add(question, QUOTED, result); add(question, DOMAIN, result);
        return result.stream().limit(12).toList();
    }
    private static void add(String text, Pattern pattern, Set<String> out) {
        Matcher matcher = pattern.matcher(text == null ? "" : text);
        while (matcher.find()) {
            String value=matcher.groupCount()>0?matcher.group(1):matcher.group();
            value=value.replaceFirst("^(?:查询|当前|以及|和|与|及|关于)","");
            if(value.length()>=2) out.add(value);
        }
    }
}
