"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelevanceScorer = void 0;
/**
 * Relevance scorer for determining which content chunks to keep
 */
class RelevanceScorer {
    /**
     * Score relevance of a chunk to a given query or context
     * Returns a score between 0 and 1
     */
    scoreRelevance(chunk, context) {
        if (!chunk || !context)
            return 0;
        // Simple keyword overlap scoring
        const chunkWords = this.toWordSet(chunk.toLowerCase());
        const contextWords = this.toWordSet(context.toLowerCase());
        if (chunkWords.size === 0)
            return 0;
        const overlap = [...chunkWords].filter(word => contextWords.has(word));
        const relevance = overlap.length / chunkWords.size;
        // Boost score for longer chunks that have good overlap
        const lengthFactor = Math.min(1.0, chunk.length / 1000); // Normalize to 1000 chars
        return Math.min(1.0, relevance * (0.5 + lengthFactor * 0.5));
    }
    /**
     * Filter chunks based on relevance threshold
     */
    filterByRelevance(chunks, context, threshold = 0.3) {
        return chunks
            .map(chunk => ({
            chunk,
            score: this.scoreRelevance(chunk, context)
        }))
            .filter(item => item.score >= threshold)
            .sort((a, b) => b.score - a.score); // Higher relevance first
    }
    /**
     * Extract keywords and create a word set
     */
    toWordSet(text) {
        const words = text
            .replace(/[^\w\s]/g, " ") // Remove punctuation
            .split(/\s+/)
            .filter(word => word.length > 2) // Ignore very short words
            .map(word => word.trim());
        return new Set(words);
    }
}
exports.RelevanceScorer = RelevanceScorer;
//# sourceMappingURL=relevance.js.map