-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
59 lines (45 loc) · 1.82 KB
/
main.js
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
import TfIdf from "./rank/tf-idf.mjs"
import Bm25 from "./rank/bm25.mjs"
import CorpusProcessor from "./text/textCleaner.mjs"
import { cleanPreviousResults, showResults, getParameters } from "./utils/dom.mjs"
import { corpusMap } from "./corpus/all-corpus.mjs"
import { getTokenizedQuery } from "./utils/utils.mjs"
import { toggleParameters } from "./utils/dom.mjs"
import { BM25 } from "./utils/constants.mjs"
const searchForm = document.getElementById("search-form")
const selectedCorpus = document.getElementById("corpus")
const queryInput = document.getElementById("query")
const rankingAlgorithm = document.getElementById("ranking-algorithm")
rankingAlgorithm.addEventListener("change", toggleParameters)
const corpusCleaner = new CorpusProcessor()
const fitAndScore = (rankAlgorithm, query, strategy) => {
const tokenizedQuery = getTokenizedQuery(corpusCleaner, query)
rankAlgorithm.fit()
return rankAlgorithm.search(tokenizedQuery, strategy)
}
const getRankAlgorithm = (corpus, parameters) => {
if (parameters.algorithm == BM25) {
const bm25 = new Bm25(corpus)
bm25.k1 = parameters.k1
bm25.b = parameters.b
return bm25
}
return new TfIdf(corpus)
}
const onSubmitHandler = (event) => {
event.preventDefault()
cleanPreviousResults()
const query = queryInput?.value
if (!query) {
alert("Need a query")
return
}
const corpus = corpusMap.get(selectedCorpus.value)
const cleanCorpus = corpusCleaner.cleanAndTokenize(corpus)
const parameters = getParameters()
const rankAlgorithm = getRankAlgorithm(cleanCorpus, parameters)
const startSearch = Date.now()
const result = fitAndScore(rankAlgorithm, query, parameters?.tfStrategy)
showResults(corpus, result, startSearch)
}
searchForm.addEventListener("submit", onSubmitHandler)