-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
56 lines (45 loc) · 1.27 KB
/
Copy pathindex.ts
File metadata and controls
56 lines (45 loc) · 1.27 KB
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
import fs from "fs";
import { createSearchEngine } from "./src/engine/search-engine.ts";
import type { Document } from "./src/indexer/document.ts";
const documents: Document[] = ["1", "2", "3", "4"].map((id) => ({
id,
url: `file://${id}.txt`,
title: `Document ${id}`,
text: fs.readFileSync(`./docs/${id}.txt`, "utf8"),
}));
const engine = createSearchEngine(documents);
console.log("\nComponents");
console.log(
"TF-IDF java / 1:",
engine.calculateTFIDF("java", "1"),
);
console.log(
"BM25 java / 1:",
engine.calculateBM25("java", "1"),
);
console.log(
"TF-IDF programming / 1:",
engine.calculateTFIDF("programming", "1"),
);
console.log(
"BM25 programming / 1:",
engine.calculateBM25("programming", "1"),
);
console.log(
"BM25 IDF java:",
engine.calculateBM25IDF("java"),
);
console.log(
"BM25 TF java / 1:",
engine.calculateBM25TF(
engine.index["java"]?.postings["1"]?.frequency ?? 0,
engine.documentStats["1"].length,
),
);
console.log("new")
console.log("\nTF-IDF OR");
console.table(engine.search("java programming", "OR"));
console.log("\nBM25 OR");
console.table(engine.searchBM25("java programming", "OR"));
console.table(engine.search("java", "OR"));
console.table(engine.searchBM25("java", "OR"));