-
Notifications
You must be signed in to change notification settings - Fork 1
/
searchWord.js
251 lines (213 loc) · 7.6 KB
/
searchWord.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
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
var counterContainer = document.querySelector(".search-counter");
const numChars = 10; // Number of character inputs
const resultDisplayLimit = 500; // Maximum number of words to display
const charInputs = 'charInputs';
const triedChars1 = 'triedChars1';
const triedChars2 = 'triedChars2';
window.onload = createOptions();
function searchWord(dbName) {
let wordLength = document.querySelector('input[name="wordLength"]:checked')?.value;
if (!wordLength) {
window.alert("You need to select word length");
}
document.getElementById('spinner').classList.add('spinner')
let knownCharacters = '';
let charDict = {};
for (let i = 1; i <= wordLength; i++) {
const char = document.getElementById(`${charInputs}-${i}`).value.toUpperCase();
knownCharacters += (char !== '') ? escapeRegExp(char) : checkTried(i); // If no character is provided, use a wildcard '.'
charDict[i - 1] = char;
}
// Convert known characters to regex pattern
const regexPattern = `^${knownCharacters}$`;
fetch('./files/ignis-' + dbName + '.txt')
.then(response => response.text())
.then(data => {
const words = data.split('\n');
const matchingWords = words.filter(word => new RegExp(regexPattern).test(word.toUpperCase()));
displayResult(matchingWords, charDict);
document.getElementById('spinner').classList.remove('spinner');
})
.catch(error => {
console.error('Error fetching words:', error)
document.getElementById('spinner').classList.remove('spinner');
}
);
}
function displayResult(matchingWords, charDict) {
const resultDiv = document.getElementById('result');
const suggestionDiv = document.getElementById('suggestion');
resultDiv.innerHTML = '';
suggestionDiv.innerHTML = '';
if (matchingWords.length > 0) {
const resultList = document.createElement('ul');
matchingWords.sort();
matchingWords.slice(0,resultDisplayLimit).forEach(word => {
const listItem = document.createElement('li');
listItem.textContent = word;
resultList.appendChild(listItem);
});
const guess = nextGuess(matchingWords, charDict);
let hint = "Next most common character (" + Math.round((guess.count / matchingWords.length) * 100) + "%): ";
console.log(JSON.stringify(guess));
console.log(JSON.stringify(charDict));
for (let i = 0; i < matchingWords[0].length; i++) {
if (i === guess.index) {
hint += '<span class="boxed">' + guess.character + '</span> '
} else if( charDict[i] === "") {
hint += "_ ";
} else {
hint += charDict[i];
}
}
suggestionDiv.innerHTML = hint;
resultDiv.appendChild(resultList);
} else {
resultDiv.textContent = 'No matching words found.';
}
}
// Function to create radio buttons for word length options
function createWordLengthOptions() {
const wordLengthOptions = document.getElementById('wordLengthForm');
for (let i = 3; i <= numChars; i++) {
const radioBtn = document.createElement('input');
radioBtn.type = 'radio';
radioBtn.name = 'wordLength';
radioBtn.id = `length${i}`;
radioBtn.value = i;
const label = document.createElement('label');
label.textContent = `${i}`;
label.htmlFor = radioBtn.id;
wordLengthOptions.appendChild(radioBtn);
wordLengthOptions.appendChild(label);
}
disableInputs(0);
wordLengthOptions.addEventListener('change', function (event) {
event.preventDefault();
disableInputs(event.target.value);
})
}
// Function to create character input fields
function createInputFields() {
const characterInputs = document.getElementById(charInputs);
const tried1 = document.getElementById(triedChars1);
const tried2 = document.getElementById(triedChars2);
for (let i = 1; i <= numChars + 1; i++) {
characterInputs.appendChild(createInputContainer(i, charInputs));
tried1.appendChild(createInputContainer(i, triedChars1));
tried2.appendChild(createInputContainer(i, triedChars2));
}
}
function createInputContainer(i, id) {
const divContainer = document.createElement('div');
if (i === numChars + 1) {
const clearButton = document.createElement('button');
clearButton.textContent = 'Clear';
clearButton.classList.add('clear-button')
clearButton.addEventListener('click', function (event) {
event.preventDefault();
resetForm(id)
});
divContainer.appendChild(clearButton);
} else {
const input = document.createElement('input');
input.type = 'text';
input.id = `${id}-${i}`;
input.maxLength = 1;
const label = document.createElement('label');
label.textContent = `${i}:`;
label.htmlFor = input.id;
divContainer.appendChild(label);
divContainer.appendChild(input);
}
return divContainer;
}
// Create character input fields and word length radio buttons when the page loads
function createOptions() {
createInputFields();
createWordLengthOptions();
}
// Function to check if there is already tried character and remove that from regex search
function checkTried(i) {
const triedChar1 = document.getElementById(`${triedChars1}-${i}`).value.toUpperCase();
const triedChar2 = document.getElementById(`${triedChars2}-${i}`).value.toUpperCase();
let regPattern = '';
if (triedChar1 !== '' || triedChar2 !== '') {
regPattern += '(?![';
if (triedChar1 !== '') {
regPattern += triedChar1;
}
if (triedChar2 !== '') {
regPattern += triedChar2;
}
regPattern += ']).';
} else {
regPattern = '.';
}
return regPattern;
}
// Function to escape special characters in the input for regex
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // Escaping special characters
}
function disableInputs(wordLength) {
let inputChars = document.getElementById(charInputs);
let tried1 = document.getElementById(triedChars1);
let tried2 = document.getElementById(triedChars2);
for (let i = numChars - 1; i >= 0; i--) {
const disable = i < wordLength ? false : true;
inputChars[i].disabled = disable;
tried1[i].disabled = disable;
tried2[i].disabled = disable;
inputChars[i].classList.toggle('disabled', i + 1 > wordLength);
tried1[i].classList.toggle('disabled', i + 1 > wordLength);
tried2[i].classList.toggle('disabled', i + 1 > wordLength);
}
}
function resetForm(formName) {
const form = document.getElementById(formName);
form.reset();
}
// Determine a suggestion for the next most likely character
function nextGuess(words, charDict) {
console.log(JSON.stringify(charDict));
const searchLocations = [];
for (let i = 0; i < words[0].length; i++) {
if (charDict[i] === "") {
searchLocations.push(i);
}
}
const nextDict = {};
let key = "";
words.forEach((word) => {
for (let i = 0; i < searchLocations.length; i++) {
key = word[searchLocations[i]] + "_" + searchLocations[i];
if (key in nextDict) {
nextDict[key] = nextDict[key] + 1;
} else {
nextDict[key] = 1;
}
}
});
const keys = Object.keys(nextDict);
let minKey = keys[0];
let min = nextDict[minKey];
for (let i = 1; i < keys.length; i++) {
if (nextDict[keys[i]] > min) {
minKey = keys[i];
min = nextDict[minKey];
}
}
return {"character": minKey[0], "index": parseInt(minKey.substring(2)), "count": min};
}
// function updateCounter() {
// var wordsSearched = Number(counterContainer.innerHTML)
// if (wordsSearched) {
// wordsSearched = Number(wordsSearched) + 1;
// localStorage.setItem("words_searched", wordsSearched);
// } else {
// wordsSearched = 1;
// localStorage.setItem("words_searched", 1);
// }
// counterContainer.innerHTML = wordsSearched;
// }