-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
352 lines (332 loc) · 13.5 KB
/
index.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<!DOCTYPE html>
<html>
<head>
<title>Anagram Solver</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="icon" type="image/png" href="favicon.png" />
<script type="text/javascript">
function readTextFile(file) {
var dictionary = "";
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === 4) {
if(rawFile.status === 200 || rawFile.status == 0) {
dictionary = rawFile.responseText;
}
}
}
rawFile.send(null);
return dictionary;
}
function updateVars(btn) {
var remove = ""; // clear remove if 'Go!'
if (btn == 1) { // update remove if 'Update'
remove = document.getElementById("remove").value;
}
var letters = document.getElementById("letters").value;
newURL = setGetParameter("letters",letters,window.location.href);
window.location.href = setGetParameter("remove",remove,newURL);
}
function changeDictionary(dict) {
if (typeof getUrlVars()["dict"] == 'undefined') {
paramVal = "000".split("");
}
else {paramVal = getUrlVars()["dict"].split("");}
paramVal[dict] = "" + (1 - parseInt(paramVal[dict]));
paramVal = paramVal.join("");
window.location.href = setGetParameter("dict",paramVal,window.location.href);
}
function loadUrlVars() {
var lettersParam = getUrlVars()["letters"];
var removeParam = getUrlVars()["remove"];
var dictParam = getUrlVars()["dict"];
if (typeof lettersParam != 'undefined') {
document.getElementById('letters').value = lettersParam;
}
if (typeof removeParam != 'undefined') {
document.getElementById('remove').value = removeParam;
}
if (typeof dictParam != 'undefined') {
if (dictParam[0]=="1") {
document.getElementById('dictionary1').checked=true;
}
if (dictParam[1]=="1") {
document.getElementById('dictionary2').checked=true;
}
if (dictParam[2]=="1") {
document.getElementById('dictionary3').checked=true;
}
}
else {
document.getElementById('dictionary1').checked=true;
window.location.href = setGetParameter("dict","001",window.location.href);
}
if (typeof lettersParam != 'undefined') {
solveAnagram();
}
}
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = decodeURIComponent(value);
});
return vars;
}
function setGetParameter(a,b,c){
var d=location.hash;if(c=c.replace(d,""),c.indexOf("?")>=0){var e=c.substring(c.indexOf("?")+1).split("&"),f=!1;e.forEach(function(c,d){c.split("=")[0]==a&&(e[d]=a+"="+encodeURIComponent(b),f=!0)}),f||e.push(a+"="+encodeURIComponent(b)),c=c.substring(0,c.indexOf("?")+1)+e.join("&")}else c+="?"+a+"="+encodeURIComponent(b);return c+d
}
function del(word) {
document.getElementById("remove").value = (document.getElementById("remove").value + " " + word).trim();
updateVars(1);
}
function solveAnagram() {
var input = document.getElementById("letters").value.toUpperCase().replace(/[^A-Z0-9\*]/g,"").split('').sort().join('');
var remove = document.getElementById("remove").value.toUpperCase().replace(/[^A-Z0-9\*]/g,"");
var errors = "";
for (var i = 0; i < remove.length; i++) {
if (input == (input = input.replace(remove[i],""))) {
errors += remove[i]+" is missing.\n";
}
}
document.getElementById("errors").style.display = "none";
if (errors != "") {
document.getElementById("errors").style.display = "inline-block";
document.getElementById("errors").innerText = "Warning: Remove list contains letters not in anagram.\n"+errors;
}
// load word lists
var dictParam = getUrlVars()["dict"];
var jsonText = '{"words": [';
if (dictParam[0]=="1") {jsonText += readTextFile("words/hpDictionary.txt");}
if (dictParam[1]=="1") {jsonText += readTextFile("words/words8kDictionary.txt");}
if (dictParam[2]=="1") {jsonText += readTextFile("words/words100kDictionary.txt");}
jsonText = jsonText.substring(0, jsonText.length - 1);
jsonText += ']}';
var list = JSON.parse(jsonText);
list["words"].sort(function(a, b) {
return b["letters"].length-a["letters"].length || // sort by length, if equal then
(a["letters"]).localeCompare(b["letters"]); // sort by alphabetical order
});
// remove duplicates
var dictionary = {"words": []};
for(var i=0,len=list["words"].length-1;i<len;i++){
// if adjacent elements are not the same, push element
if(list["words"][i]["letters"]!==list["words"][i+1]["letters"]){
dictionary["words"].push(list["words"][i]);
}
}
dictionary["words"].push(list["words"][i]); // put last element into array
gd = dictionary;
// start finding anagrams
var numWords = dictionary["words"].length;
var result = "";
var wordsFound = 0;
for (var i = 0; i < numWords; i++) {
if(contains(dictionary["words"][i]["letters"],input)) {
result += "<a href=\"javascript:del(\'"+dictionary["words"][i]["display"].replace(/'/g,"\\\'")+"\');\">" + dictionary["words"][i]["display"] + "</a><br/>";
++wordsFound;
}
}
// output results
document.getElementById("search").innerText = "Letters: "+input+"\n"+"Number of letters: "+input.length+", Words found: "+wordsFound;
var r = document.getElementById("results");
r.innerHTML = result;
// resize columns to fit contents
var longestWord = r.getElementsByTagName("A")[0].offsetWidth+15;
r.style.WebkitColumns = longestWord+"px 6";
r.style.MozColumns = longestWord+"px 6";
r.style.columns = longestWord+"px 6";
}
// returns whether word is contained in input
function contains(word, input) {
// if word has more letters, return false
if (word.length > input.length) {
return false;
}
while(word.length > 0) {
// remove first occurance of current letter from input
// if it is now different, letter is available
if (input != (input = binaryReplace(input, word[0]))) {
// remove first letter
word = word.substr(1);
}
// check for '*' wildcard instead
else if (input != (input = binaryReplace(input, "*"))) {
// remove first letter
word = word.substr(1);
}
else {
// missing letter, not an anagram
return false;
}
}
return true;
}
// remove x from sorted string str
function binaryReplace(str, x) {
var start=0, end=str.length-1;
// Iterate while start not meets end
while (start<=end){
// Find the mid index
let mid=Math.floor((start + end)/2);
// If element is present at mid, remove mid and return str
if (str[mid]==x)
return str.slice(0, mid) + str.slice(mid+1);
// Else look in left or right half accordingly
else if (str[mid] < x)
start = mid + 1;
else
end = mid - 1;
}
return str; // return original
}
</script>
<style>
body {
margin: 0;
font-family: Lato, Roboto, Arial, sans-serif;
font-size: 15px;
line-height: 24px;
color: #c5c5c5;
background-color: #111111;
}
a {
color: #a8cbf1;
text-decoration: none;
}
a:hover {
color: #d3e7fd;
text-decoration: underline;
}
div#form {
margin-left: 1.5em;
}
div#errors {
margin: 1em 0 0 1em;
color: #ff7171;
border: 2px solid #ff7171;
padding: 10px;
width: auto;
display: none;
}
div#search {
margin: 3em 0 0 1em;
}
div#results {
margin: 3em 1em;
font-size: 130%;
line-height: 28px;
-webkit-columns: 310px 5; /* Chrome, Safari, Opera */
-moz-columns: 310px 5; /* Firefox */
columns: 310px 5;
}
@media screen and (min-width: 480px) {
div#results {
font-size: 125%;
line-height: 26px;
}
}
@media screen and (min-width: 680px) {
div#form-container {
margin-left: 5em;
}
.cb {
display: inline;
}
}
input[type=radio],input[type=checkbox],input[type=text] {
margin:0px 0px 8px 10px;
}
input[type="text"], button {
padding: 6px 11px;
font-size: 120%;
color: #fff;
transition: 0.3s;
}
input[type="text"] {
border-style: ridge;
border: none;
border-bottom: 2px solid #666;
background: #111;
border-radius: 0;
}
button {
background: #222;
border: 2px solid #666;
margin-left: 8px;
margin-bottom: 10px;
}
button:hover {
background: #333;
border-color: #777;
}
input[type="text"]:focus, button:focus {
outline: none;
border-color: #c5c5c5;
}
#title {
margin-left:10px;
margin-top:3em;
line-height: 32px;
color: #00c587;
}
label.inputlabel {
margin-left: 10px;
font-size: 116%;
display: inline-block;
width: 60px;
}
@media screen and (max-width: 440px) {
label.inputlabel {
display: block;
margin-bottom: 3px;
margin-top: 5px;
}
button {
width: 100px;
margin-right: 10px;
}
}
</style>
</head>
<body>
<div id="form">
<h1 id="title">Eyl's Anagram Solver</h1>
<label class="inputlabel">Letters:</label>
<input id="letters" type="text" placeholder="letters to anagram">
<button onclick="updateVars(0);">Go!</button>
<br/>
<label class="inputlabel">Remove:</label>
<input id="remove" type="text" placeholder="words selected">
<button onclick="updateVars(1);">Update</button>
<div style="margin-top:10px;">
<div class="cb"><input type="checkbox" id="dictionary1" name="dictionary" onclick="changeDictionary(0)"> Harry Potter Words</div>
<div class="cb"><input type="checkbox" id="dictionary2" name="dictionary" onclick="changeDictionary(1)"> English Words (8,000+)</div>
<div class="cb"><input type="checkbox" id="dictionary3" name="dictionary" onclick="changeDictionary(2)"> English words (100,000+)</div>
</div>
<div id="errors"></div>
<div id="search"></div>
<div id="results"></div>
</div>
</body>
<script>
document.getElementById('letters').onkeypress = function(e) {
var event = e || window.event;
var charCode = event.which || event.keyCode;
if ( charCode == '13' ) {
updateVars(0);
return false;
}
}
document.getElementById('remove').onkeypress = function(e) {
var event = e || window.event;
var charCode = event.which || event.keyCode;
if ( charCode == '13' ) {
updateVars(1);
return false;
}
}
window.onload = loadUrlVars;
</script>
</html>