-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
175 lines (146 loc) · 5.13 KB
/
script.js
File metadata and controls
175 lines (146 loc) · 5.13 KB
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
import { dict } from "./dict_verbs.js";
function hasFields(word) {
return (
word.sentence.phonetics.length > 0 &&
word.sentence.syllabary.length > 0 &&
word.sentence.phonetics.includes("*") &&
word.sentence.syllabary.includes("*") &&
!word.sentence.phonetics.startsWith("[")
);
}
function validWordsForForm(form) {
return Object.keys(dict).filter((id) => hasFields(dict[id]) && dict[id][form].trim().replaceAll("-", "").length > 0);
}
function pickNRandom(options, n) {
const randomNumbers = new Array(n)
.fill(0)
.map((_, idx) => Math.floor(Math.random() * (options.length - idx)));
const [picked] = randomNumbers.reduce(
([pickedOptions, pickedIdc], nextRandomNumber) => {
const nextIdx = pickedIdc.reduce(
(adjustedIdx, alreadyPickedIdx) =>
// bump up the index for each element we've removed if we are past it
// eg. [3] has been picked from [0 1 2 _3_ 4 5] and we have 3 has nextRandom number
// we bump up to 4, as if 3 weren't there
adjustedIdx + Number(adjustedIdx >= alreadyPickedIdx),
nextRandomNumber
);
return [
[...pickedOptions, options[nextIdx]],
[...pickedIdc, nextIdx].sort(),
];
},
[[], []]
);
return picked;
}
function shuffled(list) {
const indexed = list.map((e) => [Math.random(), e]);
indexed.sort(([a], [b]) => a - b);
return indexed.map(([_, e]) => e);
}
function subAsterisk(text, pattern) {
var bold = /\*(.*?)\*/gm;
var html = text.replace(bold, pattern);
return html;
}
function removeAsterisk(text) {
return subAsterisk(text, "$1");
}
function boldAsterisk(text) {
return subAsterisk(text, "<strong>$1</strong>");
}
const underdot = "\u0323"; // dot for under short vowels
function formatTone(text) {
return text
.replaceAll(/1/gm, "¹")
.replaceAll(/23/gm, "²³")
.replaceAll(/32/gm, "³²")
.replaceAll(/2/gm, "²")
.replaceAll(/3/gm, "³")
.replaceAll(/4/gm, "⁴")
.replaceAll(/\?/gm, "Ɂ")
.replaceAll(/([aeiouv])\./gm, "$1" + underdot);
}
function setExampleSentence(word) {
sentenceSyllabary.innerHTML = boldAsterisk(word.sentence.syllabary);
sentencePhonetics.innerHTML = boldAsterisk(word.sentence.phonetics);
sentenceEnglish.innerHTML = "";
}
function setOptions(options, revealTranslation, form) {
const renderedOptions = options.map((option, idx) => {
let clicked = false;
const elm = document.createElement("li");
const button = document.createElement("button");
button.innerHTML =
option[form+"_syllabary"] + " / " + formatTone(option[form]);
button.addEventListener("click", () => {
if (clicked) return;
else clicked = true;
const wordTranslation = document.createElement("span");
wordTranslation.innerHTML = option.definition + " - ";
wordTranslation.classList.add("translation");
const cedLink = document.createElement("a");
cedLinkForWord(option).then((link) => (cedLink.href = link));
cedLink.innerHTML = "see more";
cedLink.target = "_blank";
wordTranslation.append(cedLink);
elm.append(wordTranslation);
if (idx === 0) {
button.style = "background: var(--color-bg);";
revealTranslation();
} else {
button.style = "background: var(--color-bad);";
}
});
elm.append(button);
return elm;
});
optionsElm.replaceChildren(...shuffled(renderedOptions));
}
function nextWord() {
const form = targetForm === "rand" ? pickNRandom(possibleForms, 1)[0] : targetForm;
if (wordIds === null || targetForm === "rand") {
wordIds = validWordsForForm(form)
}
const options = pickNRandom(wordIds, 4).map((id) => dict[id]);
const word = options[0];
function revealTranslation() {
sentenceEnglish.innerHTML = boldAsterisk(word.sentence.english);
}
setExampleSentence(word);
setOptions(options, revealTranslation, form);
}
const settingsForm = document.querySelector(".settings");
const nextBtn = document.querySelector(".next");
settingsForm.addEventListener("submit", (e) => {
e.preventDefault();
nextWord();
});
let targetForm = settingsForm.elements["targetForm"].value;
let wordIds = null;
settingsForm.elements["targetForm"].addEventListener("change", (e) => {e.preventDefault(); if (targetForm !== e.target.value) {
targetForm = e.target.value;
wordIds = null;
}})
const possibleForms = [...settingsForm.elements["targetForm"].children].map(e => e.value).filter(f => f!=="rand");
console.log(possibleForms)
const sentenceSyllabary = document.querySelector(".example-syllabary");
const sentencePhonetics = document.querySelector(".example-phonetics");
const sentenceEnglish = document.querySelector(".example-english");
const optionsElm = document.querySelector(".options");
nextWord();
async function cedLinkForWord(word) {
const resp = await fetch(
`https://cherokeedictionary.net/jsonsearch/en/${encodeURIComponent(
word.definition
)}`
);
const json = await resp.json();
console.log(json);
console.log(word);
const [result] = json.filter(
(r) => r.syllabaryb == word.third_present_syllabary
);
return `https://www.cherokeedictionary.net/share/${result.id}`;
}