MCPcopy Create free account
hub / github.com/codemix/graph / generateDocumentWithKeywords

Function generateDocumentWithKeywords

packages/text-search/src/performance.test.ts:334–361  ·  view source on GitHub ↗

* Generate a document with specific keywords embedded.

(
  wordCount: number,
  keywords: string[],
  keywordDensity: number = 0.05,
  seed: number = 0,
)

Source from the content-addressed store, hash-verified

332 * Generate a document with specific keywords embedded.
333 */
334function generateDocumentWithKeywords(
335 wordCount: number,
336 keywords: string[],
337 keywordDensity: number = 0.05,
338 seed: number = 0,
339): string {
340 const baseDoc = generateDocument(wordCount, seed);
341 const words = baseDoc.split(/\s+/);
342
343 // Sprinkle keywords throughout the document
344 const keywordPositions = new Set<number>();
345 let random = seed + 1000;
346 const nextRandom = () => {
347 random = (random * 1103515245 + 12345) & 0x7fffffff;
348 return random / 0x7fffffff;
349 };
350
351 const numKeywords = Math.floor(wordCount * keywordDensity);
352 for (let i = 0; i < numKeywords; i++) {
353 const pos = Math.floor(nextRandom() * words.length);
354 if (!keywordPositions.has(pos)) {
355 keywordPositions.add(pos);
356 words[pos] = keywords[Math.floor(nextRandom() * keywords.length)]!;
357 }
358 }
359
360 return words.join(" ");
361}
362
363test("Performance and Large Document Tests large document handling should handle a 1000-word document", () => {
364 const doc = generateDocument(1000);

Callers 1

Calls 4

generateDocumentFunction · 0.85
nextRandomFunction · 0.85
addMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected