-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
51 lines (45 loc) · 1.35 KB
/
script.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
var currentWordElement = document.getElementById('currentWord');
// start listening for changes in the current word
if(window.addEventListener) {
currentWordElement.addEventListener('DOMSubtreeModified', handler, false);
} else if(window.attachEvent) {
currentWordElement.attachEvent('DOMSubtreeModified', handler); // IE
}
// re-search hints upon current word change
function handler() {
var text = currentWordElement.innerText;
if(text && text.includes('_')) {
matching_words = find_matching_words(text);
console.log(matching_words.slice(0, 100));
document.getElementById("hints").innerHTML = "<b><i><u>Hints:</u> " + matching_words.slice(0, 100).toString() + "...</i></b>";
}
}
var words;
// load words from file
document.addEventListener('loadWordsEvent', function (e)
{
words = e.detail;
});
function is_pattern_matching(pattern, text) {
if (pattern.length != text.length) {
return false;
}
for (var i=0; i < pattern.length; i++) {
if (pattern[i] != '_' && pattern[i] != text[i]) {
return false;
}
}
return true;
}
function find_matching_words(pattern) {
if (!(pattern.length in words)) {
return [];
}
var res=[];
for (var i=0; i < words[pattern.length].length; i++) {
if (is_pattern_matching(pattern, words[pattern.length][i])) {
res.push(words[pattern.length][i]);
}
}
return res;
}