-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.html
257 lines (220 loc) · 6.21 KB
/
explorer.html
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
<html>
<head>
<style>
#suggestions {
position: absolute;
background-color: #f9f9f9;
border: 1px solid #ccc;
max-height: 150px;
overflow-y: auto;
width: 200px;
}
#suggestions p {
padding: 5px;
margin: 0;
cursor: pointer;
}
#suggestions p:hover {
background-color: #e9e9e9;
}
.results-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.results-table th {
font-weight: bold;
}
.results-table td {
text-align: center;
}
</style>
</head>
<body>
Enter the Bible verse to find related:<br /><input type="text" id="myInput" oninput="handleInput()" placeholder="Loading please wait...">
<input type="radio" name="position" value="top" id="top-radio" checked>
<label for="top-radio">Most Similar</label>
<input type="radio" name="position" value="bottom" id="bottom-radio">
<label for="bottom-radio">Least Similar</label>
<input type="number" min="2" max="500" value="20" id="topn-input">
<div id="suggestions"></div><br /><br />
<div id="results"></div>
<script src="load_data.js"></script>
<script type="text/javascript">
loadAll();
class TrieNode {
constructor() {
this.children = {};
this.isEndOfWord = false;
this.itemIndex = null; // Store the index of the item
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insert(word, index) {
let node = this.root;
for (let i = 0; i < word.length; i++) {
const char = word[i];
if (!node.children[char]) {
node.children[char] = new TrieNode();
}
node = node.children[char];
}
node.isEndOfWord = true;
node.itemIndex = index; // Store the index of the item in the TrieNode
}
search(prefix, index=false) {
let node = this.root;
for (let i = 0; i < prefix.length; i++) {
const char = prefix[i];
if (!node.children[char]) {
return [];
}
node = node.children[char];
}
let out = this._collectWords(node, prefix).sort((a, b) => a.index - b.index);
if (!index) {
return out.map(a => a.word)
} else {
return out
}
}
_collectWords(node, prefix) {
const words = [];
if (node.isEndOfWord) {
words.push({
word: prefix,
index: node.itemIndex // Retrieve the index of the item from the TrieNode
});
}
for (const char in node.children) {
const childNode = node.children[char];
words.push(...this._collectWords(childNode, prefix + char));
}
return words;
}
}
let options = [];
const trie = new Trie();
async function getOptions()
{
while (web === false || webmatrix === false) {
await new Promise(resolve => setTimeout(resolve, 100));
}
options = web.map(pair => pair[0]);
options.forEach((option, index) => trie.insert(option, index));
input.placeholder = "E.g. John 3:16";
}
const input = document.getElementById("myInput");
const suggestions = document.getElementById("suggestions");
const results = document.getElementById("results");
async function handleInput(enter=false) {
const inputText = input.value;
// Load options if not already loaded
if (options.length === 0) {
await getOptions();
}
let matchingOptions = [];
if (inputText.length !== 0)
{
matchingOptions = trie.search(inputText);
}
suggestions.innerHTML = "";
if (matchingOptions.includes(inputText) && enter) {
choose()
} else if (matchingOptions.length > 1) {
matchingOptions.forEach(option => {
const p = document.createElement("p");
p.textContent = option;
p.onclick = () => {
input.value = option;
suggestions.innerHTML = "";
};
suggestions.appendChild(p);
});
} else if (matchingOptions.length == 1) {
choose()
}
}
document.addEventListener("click", event => {
if (event.target !== input && event.target !== suggestions) {
choose()
}
});
document.getElementById("top-radio").addEventListener("click", handleInput);
document.getElementById("bottom-radio").addEventListener("click", handleInput);
document.getElementById("topn-input").addEventListener("change", handleInput);
input.addEventListener("keyup", function(event) {
if (event.key === "Enter" || event.keyCode === 13) {
handleInput(true);
}
});
function rowFromIndex(index, simContent=false) {
const row = document.createElement("tr");
const webItem = web[index];
const verseName = document.createElement("td");
verseName.textContent = webItem[0];
const verseContent = document.createElement("td");
verseContent.textContent = webItem[1];
row.appendChild(verseName);
row.appendChild(verseContent);
if (simContent != false) {
const sim = document.createElement("td");
sim.textContent = simContent.toFixed(4);
row.appendChild(sim);
}
return row
}
function findSim(index, topn=document.getElementById("topn-input").value) {
const sims = dotOneToMany(webmatrix[index], webmatrix)
const topnindexes = argsort(sims, document.querySelector('input[id="bottom-radio"]:checked')).slice(0, topn);
let indexWithSim = []
topnindexes.forEach(index => indexWithSim.push(
{
index: index,
sim: sims[index]
}
));
return indexWithSim
}
function appendResults(index) {
clearResults()
const table = document.createElement("table");
table.classList.add("results-table");
const headerRow = document.createElement("tr")
const columnNames = ["Location", "Text", "Similarity"]
columnNames.forEach(columnName => {
const headerCell = document.createElement("th");
headerCell.textContent = columnName;
headerRow.appendChild(headerCell);
});
table.appendChild(headerRow);
const topVerse = rowFromIndex(index, 1.0);
table.appendChild(topVerse);
const otherIndexesAndSims = findSim(index);
otherIndexesAndSims.forEach(indexAndSim => {
if (indexAndSim.index != index) {
const matchVerse = rowFromIndex(indexAndSim.index, indexAndSim.sim)
table.appendChild(matchVerse);
}
});
results.append(table);
}
function clearResults() {
results.innerHTML = "";
}
function choose() {
suggestions.innerHtml = "";
const item = trie.search(input.value, true).filter(item => item.word === input.value)[0];
const verse = item.word;
const index = item.index;
if (typeof index !== 'undefined') {
appendResults(index);
}
}
getOptions();
</script>
</body>
</html>