-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
242 lines (206 loc) · 6.73 KB
/
index.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
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// 1. Libraries //
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/**
* An implementation of a set that preserves the insertion order.
* @class
*/
class OrderedSet {
/**
* Creates a new OrderedSet.
* @constructor
*/
constructor() {
this.set = new Set();
this.array = [];
}
/**
* Adds an element to the set.
* @param {*} value - The value to add.
*/
add(value) {
if (!this.has(value)) {
this.set.add(value);
this.array.push(value);
}
}
/**
* Removes an element from the set.
* @param {*} value - The value to remove.
* @returns {boolean} - Returns true if the value was successfully removed, false otherwise.
*/
delete(value) {
if (this.has(value)) {
this.set.delete(value);
this.array = this.array.filter((item) => item !== value);
return true;
}
return false;
}
/**
* Checks if the set contains a given value.
* @param {*} value - The value to check.
* @returns {boolean} - Returns true if the value is present, false otherwise.
*/
has(value) {
return this.set.has(value);
}
/**
* Returns the number of elements in the set.
* @returns {number} - The size of the set.
*/
size() {
return this.set.size;
}
/**
* Returns an iterator that contains the values in insertion order.
* @returns {Iterator} - An iterator object.
*/
values() {
return this.array[Symbol.iterator]();
}
}
/**
* JavaScript version of Python's defaultdict.
* @class
*/
class DefaultDict {
/**
* Creates a new DefaultDict.
* @constructor
* @param {function} defaultFactory - A function that returns the default value for missing keys.
*/
constructor(defaultFactory) {
this.defaultFactory = defaultFactory;
return new Proxy({}, this);
}
/**
* The handler for the proxy object.
* @param {Object} target - The target object.
* @param {string} prop - The property being accessed.
* @returns {*} - The value of the property or the default value.
*/
get(target, prop) {
if (!(prop in target)) {
target[prop] = this.defaultFactory();
}
return target[prop];
}
}
/**
* Zips two iterables together, yielding pairs of corresponding elements.
* @param {Iterable} iterable1 - The first iterable.
* @param {Iterable} iterable2 - The second iterable.
* @returns {Iterable} An iterable object containing pairs of corresponding elements.
*/
const zip = (iterable1, iterable2) => {
const iterator1 = iterable1[Symbol.iterator]();
const iterator2 = iterable2[Symbol.iterator]();
return {
*[Symbol.iterator]() {
while (true) {
const result1 = iterator1.next();
const result2 = iterator2.next();
if (result1.done || result2.done) {
return;
}
yield [result1.value, result2.value];
}
},
};
};
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// 2. Functions //
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
const rawDictionaryInput = document.getElementById("rawDictionaryInput");
const rawTableSpecsInput = document.getElementById("rawTableSpecsInput");
const outputArea = document.getElementById("outputArea");
const parseRawDictionary = (rawDictionary) =>
rawDictionary
.replace(/\n+$/, "")
.split("\n")
.reduce((acc, line) => {
const [字頭, 拼音, 備註] = line.split("\t");
const 拼音_ = !備註 ? 拼音 : `${拼音}(${備註})`;
acc[字頭].push(拼音_);
return acc;
}, new DefaultDict(() => []));
const parseRawTableSpecs = (rawTableSpecs) =>
rawTableSpecs
.replace(/\n+$/, "")
.split("\n")
.map((line) => {
const [name, expression] = line.split("\t");
return { name, expression };
});
const buildTables = (dictionary, tableSpecs) => {
const tables = [];
for (const { name, expression, target } of tableSpecs) {
const table = new DefaultDict(() => new OrderedSet());
for (const 音韻地位 of Qieyun.資料.iter音韻地位()) {
const { 母, 呼, 等, 重紐, 韻, 聲, 攝 } = 音韻地位; // eslint-disable-line no-unused-vars
const lhs = eval(expression);
table[lhs]; // initialise dictionary key
for (const { 字頭, 解釋 } of Qieyun.資料.query音韻地位(音韻地位)) {
for (const 拼音 of dictionary[字頭] || []) {
const 字音 = `${字頭}${拼音}`;
table[lhs].add(字音);
}
}
}
tables.push(table);
}
return tables;
};
const handleSubmit = () => {
const dictionary = parseRawDictionary(rawDictionaryInput.value);
const tableSpecs = parseRawTableSpecs(rawTableSpecsInput.value);
const tables = buildTables(dictionary, tableSpecs);
outputArea.innerHTML = ""; // clear output area
for (const [{ name }, table_] of zip(tableSpecs, tables)) {
const myDefaultDict = table_;
const table = document.createElement("table");
const tr = document.createElement("tr");
table.appendChild(tr);
const th = document.createElement("th");
th.textContent = name;
th.colSpan = 3;
tr.appendChild(th);
for (const [key, orderedSet] of Object.entries(myDefaultDict)) {
const row = document.createElement("tr");
const keyCell = document.createElement("td");
keyCell.textContent = key;
row.appendChild(keyCell);
const valuesCell = document.createElement("td");
valuesCell.textContent = Array.from(orderedSet.values()).join(", ");
row.appendChild(valuesCell);
table.appendChild(row);
}
outputArea.appendChild(table);
}
};
const handleLoadSampleDict = async () => {
const request = await fetch("sample-dict.txt");
const response = await request.text();
rawDictionaryInput.value = response;
};
rawDictionaryInput.addEventListener("input", () => {
const content = rawDictionaryInput.value;
localStorage.setItem("rawDictionaryInputContent", content);
});
window.addEventListener("DOMContentLoaded", () => {
const storedContent = localStorage.getItem("rawDictionaryInputContent");
if (storedContent) rawDictionaryInput.value = storedContent;
});
rawTableSpecsInput.addEventListener("input", () => {
const content = rawTableSpecsInput.value;
localStorage.setItem("rawTableSpecsInputContent", content);
});
window.addEventListener("DOMContentLoaded", () => {
const storedContent = localStorage.getItem("rawTableSpecsInputContent");
if (storedContent) rawTableSpecsInput.value = storedContent;
});