-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
67 lines (61 loc) · 1.97 KB
/
example.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
import { MecabWorker } from "./dist/index.js";
const inputElement = document.getElementById("input");
const outputElement = document.getElementById("output");
const outTableElement = document.getElementById("outtable");
const logElement = document.getElementById("log");
const progressElement = document.getElementById("progress");
function appendToLog(message) {
const textNode = document.createTextNode(message);
const brElement = document.createElement("br");
logElement.appendChild(textNode);
logElement.appendChild(brElement);
brElement.scrollIntoView();
}
let worker;
let size = 0;
progressElement.value = size;
try {
worker = await MecabWorker.create("../ipadic-2.7.0_bin.zip", {
onLoad: (message) => {
if (size && message.total) {
size += message.size;
progressElement.value = size;
progressElement.max = message.total;
} else {
progressElement.removeAttribute("value");
}
appendToLog(`load file with ${message.type}: ${message.name}`);
},
});
progressElement.value = 1;
progressElement.max = 1;
} catch (error) {
appendToLog(error);
progressElement.value = 0;
progressElement.max = 1;
throw error;
}
async function parse() {
try {
const parsed = await worker.parse(inputElement.value);
const textNode = document.createTextNode(parsed);
outputElement.replaceChildren(textNode);
const parsedNodes = await worker.parseToNodes(inputElement.value);
const trElements = parsedNodes.map((node) => {
const trElement = document.createElement("tr");
node.features.forEach((feature) => {
const tdElement = document.createElement("td");
const textNode = document.createTextNode(feature);
tdElement.appendChild(textNode);
trElement.appendChild(tdElement);
});
return trElement;
});
outTableElement.replaceChildren(...trElements);
} catch (error) {
appendToLog(error);
throw error;
}
}
parse();
inputElement.oninput = parse;