File size: 9,564 Bytes
fbad523 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | import type {
CorpusDocument,
TermStats,
DocIndexMeta,
LengthHistogramBin,
IndexTelemetry,
BuiltInvertedIndex,
} from '../types/corpus';
import { computeEmbedding, computeSpladeTokens } from './retrievalScoring';
// Standard Lucene / English Stopwords
export const STANDARD_STOPWORDS = new Set([
'a', 'about', 'above', 'after', 'again', 'against', 'all', 'am', 'an', 'and', 'any',
'are', 'aren\'t', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below',
'between', 'both', 'but', 'by', 'can', 'cannot', 'could', 'did', 'do', 'does',
'doing', 'don\'t', 'down', 'during', 'each', 'few', 'for', 'from', 'further',
'had', 'has', 'have', 'having', 'he', 'her', 'here', 'hers', 'herself', 'him',
'himself', 'his', 'how', 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'itself',
'me', 'more', 'most', 'my', 'myself', 'no', 'nor', 'not', 'of', 'off', 'on',
'once', 'only', 'or', 'other', 'ought', 'our', 'ours', 'ourselves', 'out', 'over',
'own', 'same', 'she', 'should', 'so', 'some', 'such', 'than', 'that', 'the',
'their', 'theirs', 'them', 'themselves', 'then', 'there', 'these', 'they', 'this',
'those', 'through', 'to', 'too', 'under', 'until', 'up', 'very', 'was', 'we',
'were', 'what', 'when', 'where', 'which', 'while', 'who', 'whom', 'why', 'with',
'would', 'you', 'your', 'yours', 'yourself', 'yourselves'
]);
export interface TokenWithPos {
term: string;
position: number;
}
// Tokenize text while tracking word position offsets
export function tokenizeWithPositions(
text: string,
filterStopwords: boolean = false
): TokenWithPos[] {
const result: TokenWithPos[] = [];
const regex = /[a-zA-Z0-9]+(?:'[a-zA-Z0-9]+)?/g;
let match: RegExpExecArray | null;
let pos = 0;
while ((match = regex.exec(text)) !== null) {
const term = match[0].toLowerCase();
if (!filterStopwords || !STANDARD_STOPWORDS.has(term)) {
result.push({ term, position: pos });
}
pos++;
}
return result;
}
// Compute length histogram bins
export function computeLengthHistogram(docLengths: { id: string; length: number }[]): LengthHistogramBin[] {
if (docLengths.length === 0) return [];
const lengths = docLengths.map((d) => d.length);
const min = Math.min(...lengths);
const max = Math.max(...lengths);
if (min === max) {
return [
{
binLabel: `${min} tokens`,
rangeMin: min,
rangeMax: max,
count: docLengths.length,
percentage: 100,
docIds: docLengths.map((d) => d.id),
},
];
}
const numBins = Math.min(6, Math.max(3, Math.ceil(Math.sqrt(docLengths.length))));
const binWidth = Math.max(1, Math.ceil((max - min + 1) / numBins));
const bins: LengthHistogramBin[] = [];
for (let i = 0; i < numBins; i++) {
const rangeMin = min + i * binWidth;
const rangeMax = i === numBins - 1 ? max : rangeMin + binWidth - 1;
bins.push({
binLabel: `${rangeMin} - ${rangeMax}`,
rangeMin,
rangeMax,
count: 0,
percentage: 0,
docIds: [],
});
}
docLengths.forEach(({ id, length }) => {
let placed = false;
for (const bin of bins) {
if (length >= bin.rangeMin && length <= bin.rangeMax) {
bin.count++;
bin.docIds.push(id);
placed = true;
break;
}
}
if (!placed && bins.length > 0) {
bins[bins.length - 1].count++;
bins[bins.length - 1].docIds.push(id);
}
});
const total = docLengths.length;
bins.forEach((b) => {
b.percentage = Number(((b.count / total) * 100).toFixed(1));
});
return bins.filter((b) => b.count > 0);
}
export interface IndexBuildOptions {
filterStopwords?: boolean;
}
/**
* Builds a complete client-side inverted index for a collection of documents.
* Emulates Apache Lucene's core in-memory index structures and telemetry.
*/
export function buildInvertedIndex(
documents: CorpusDocument[],
options: IndexBuildOptions = {}
): BuiltInvertedIndex {
const startTime = performance.now();
const filterStopwords = options.filterStopwords ?? false;
const N = documents.length;
const dictionaryMap = new Map<string, TermStats>();
const docMap = new Map<string, DocIndexMeta>();
const docIndexList: DocIndexMeta[] = [];
let totalTokens = 0;
let rawSizeBytes = 0;
// 1. Process documents and construct postings
documents.forEach((doc, internalId) => {
const fullText = (doc.title ? `${doc.title} ` : '') + doc.text;
rawSizeBytes += new TextEncoder().encode(fullText).length;
const tokensWithPos = tokenizeWithPositions(fullText, filterStopwords);
const docLength = tokensWithPos.length;
totalTokens += docLength;
// Per-document term frequency and positions
const docTfMap = new Map<string, { count: number; positions: number[] }>();
tokensWithPos.forEach(({ term, position }) => {
let entry = docTfMap.get(term);
if (!entry) {
entry = { count: 0, positions: [] };
docTfMap.set(term, entry);
}
entry.count++;
entry.positions.push(position);
});
// Populate global dictionary & inverted posting lists
docTfMap.forEach((tfData, term) => {
let termStat = dictionaryMap.get(term);
if (!termStat) {
termStat = {
term,
docFreq: 0,
collectionFreq: 0,
idf: 0,
postings: [],
};
dictionaryMap.set(term, termStat);
}
termStat.docFreq++;
termStat.collectionFreq += tfData.count;
termStat.postings.push({
docId: doc.id,
docIndex: internalId,
termFreq: tfData.count,
positions: tfData.positions,
docLength,
});
});
// Term vector for document
const termVector: { term: string; tf: number }[] = [];
docTfMap.forEach((data, term) => {
termVector.push({ term, tf: data.count });
});
termVector.sort((a, b) => b.tf - a.tf);
// Compute dense embedding & SPLADE activations
const denseVector = computeEmbedding(fullText, 768);
const spladeWeights = computeSpladeTokens(fullText);
const spladeExpansions = Object.entries(spladeWeights).map(([token, weight]) => ({
token,
weight,
is_expansion: !fullText.toLowerCase().includes(token.toLowerCase()),
}));
const docMeta: DocIndexMeta = {
id: doc.id,
internalId,
title: doc.title,
text: doc.text,
length: docLength,
uniqueTerms: docTfMap.size,
termVector,
denseVector,
denseDim: 768,
denseSource: 'in-browser-simulation',
denseLatencyMs: 0.1,
spladeWeights,
spladeExpansions,
spladeSource: 'in-browser-simulation',
spladeLatencyMs: 0.1,
totalActivatedSplade: Object.keys(spladeWeights).length,
mlmExpansionsCount: spladeExpansions.filter((e) => e.is_expansion).length,
};
docMap.set(doc.id, docMeta);
docIndexList.push(docMeta);
});
// 2. Compute Lucene Robertson-Spärck Jones IDF for every term
// IDF formula: ln(1 + (N - DF + 0.5) / (DF + 0.5))
dictionaryMap.forEach((termStat) => {
const df = termStat.docFreq;
const rawIdf = Math.log(1.0 + (N - df + 0.5) / (df + 0.5));
termStat.idf = Number(Math.max(0.1, rawIdf).toFixed(4));
// Sort postings by Lucene internal Doc ID ascending (standard posting list invariant)
termStat.postings.sort((a, b) => a.docIndex - b.docIndex);
});
const termsList: TermStats[] = Array.from(dictionaryMap.values());
// Default sort by Document Frequency descending, then Collection Frequency
termsList.sort((a, b) => {
if (b.docFreq !== a.docFreq) return b.docFreq - a.docFreq;
return b.collectionFreq - a.collectionFreq;
});
const endTime = performance.now();
const indexingTimeMs = Number((endTime - startTime).toFixed(2));
// 3. Compute telemetry metrics
const avgdl = N > 0 ? Number((totalTokens / N).toFixed(2)) : 0;
const vocabSize = dictionaryMap.size;
const docLengths = docIndexList.map((d) => ({ id: d.id, length: d.length }));
docLengths.sort((a, b) => a.length - b.length);
const shortestDoc = docLengths[0] || { id: '-', length: 0 };
const longestDoc = docLengths[docLengths.length - 1] || { id: '-', length: 0 };
const histogram = computeLengthHistogram(docLengths);
// Index size estimate:
// Term dictionary: ~16 bytes overhead per term + string length
// Postings: 4 bytes docDelta + 2 bytes tf + 2 bytes * positions
let indexSizeBytes = 0;
termsList.forEach((t) => {
indexSizeBytes += 16 + t.term.length; // dictionary entry
t.postings.forEach((p) => {
indexSizeBytes += 4 + 2 + (p.positions ? p.positions.length * 2 : 0);
});
});
const compressionRatio =
indexSizeBytes > 0 ? Number((rawSizeBytes / indexSizeBytes).toFixed(2)) : 1.0;
const telemetry: IndexTelemetry = {
docCount: N,
totalTokens,
vocabSize,
avgdl,
indexingTimeMs,
rawSizeBytes,
indexSizeBytes,
compressionRatio,
shortestDoc,
longestDoc,
histogram,
};
return {
telemetry,
terms: termsList,
dictionary: dictionaryMap,
documents: docIndexList,
docMap,
};
}
// Term search filter (prefix or contains)
export function searchIndexTerms(
index: BuiltInvertedIndex,
query: string,
mode: 'prefix' | 'contains' = 'contains'
): TermStats[] {
if (!query.trim()) return index.terms;
const q = query.trim().toLowerCase();
return index.terms.filter((t) => {
if (mode === 'prefix') {
return t.term.startsWith(q);
}
return t.term.includes(q);
});
}
|