-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlookup.js
103 lines (80 loc) · 2.94 KB
/
lookup.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
// based off of https://github.com/occidental-lang/website-word-lookup/
// need to load PapaParse before this file
var dictionary = {};
const definitionEl = document.getElementById('definition');
function lookupInit() {
Papa.parse('https://raw.githubusercontent.com/Shavian-info/readlex/main/kingsleyreadlexicon.tsv', {
download: true,
delimiter: "\t",
newline: "\n",
complete: results => {
for(var i=0;i < results.data.length; i++) {
var shavianWord = results.data[i][1];
var latinWord = results.data[i][0];
dictionary[shavianWord] = {"latin" : results.data[i][0], "partOfSpeech": results.data[i][2], "IPA": results.data[i][3],"frequency": results.data[i][4]}
}
}
})
document.onselectionchange = debounce(lookup, 1200);
}
function debounce(func, delay) {
let inDebounce
return function() {
const context = this
const args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(() => func.apply(context, args), delay)
}
}
function lookup() {
var s = document.getSelection().toString().trim().toLowerCase();
var punctuationless = s.replace(/[·.,\/#!$?%\^&\*;:{}=_`~()"]/g,"");
var cleaned = punctuationless.replace(/\s{2,}/g," ");
var selection = cleaned.split(' ');
if (selection[0] == '') {
console.log('selected nothing!')
return;
}
console.log(`You selected: ${selection}`);
var definitions = [];
for (var word of selection) {
if (word in dictionary) {
console.log(`Looking up: ${word}`);
definitions.push({"word": word, "info": dictionary[word]});
} else {
console.log(`Not in dictionary: ${word}`);
definitions.push({"word": word, "info": '' });
}
}
if (definitions.length <= 1) {
var payload = `<h4>${definitions[0].word}</h4>
<p>Latin : ${definitions[0].info['latin']}</p>
<hr/>
<p>Part of Speech : ${definitions[0].info['partOfSpeech']}</p>
<p>IPA : ${definitions[0].info['IPA']}</p>
<p>Frequency : ${definitions[0].info['frequency']}</p>`;
displayInfo(payload)
} else {
var payload = '<p>';
for (var d of definitions) {
if (d.info != '') {
if (d.info["latin"] != '') {
payload += ` ${d.info["latin"]} `
} else {
payload += ` [${d.word}] `
}
} else {
payload += ` [${d.word}] `
}
}
payload += '</p>';
displayInfo(payload)
}
}
function displayInfo(htmlString) {
definitionEl.innerHTML = '';
console.log('displaying it!');
definitionEl.innerHTML = htmlString;
definitionEl.style.display = 'block';
definitionEl.onclick = () => { definitionEl.style.display = 'none'; };
}