import __wbg_init, { TokenizerBuilder, Tokenizer, Mode, load_dictionary, getVersion } from '../../pkg/lindera_wasm.js';
let tokenizer = null;
__wbg_init().then(() => {
try {
const version = getVersion();
document.title = `Lindera WASM v${version}`;
document.getElementById('title').textContent = `Lindera WASM v${version}`;
} catch (e) {
console.error("Failed to get version:", e);
}
try {
let builder = new TokenizerBuilder();
builder.setDictionary("embedded://ipadic");
builder.setMode("normal");
tokenizer = builder.build();
console.log("Tokenizer is ready.");
} catch (e) {
console.error("Failed to create Tokenizer:", e);
}
}).catch(e => {
console.error("Failed to initialize WASM module:", e);
});
document.getElementById('runButton').addEventListener('click', () => {
if (!tokenizer) {
console.error("Tokenizer is not initialized yet.");
return;
}
const inputText = document.getElementById('inputText').value;
const tokens = tokenizer.tokenize(inputText);
const resultList = document.getElementById('resultList');
resultList.innerHTML = '';
const table = document.createElement('table');
table.className = 'token-table';
table.innerHTML = `
<thead>
<tr>
<th>Surface</th>
<th>Position</th>
<th>Details</th>
</tr>
</thead>
<tbody></tbody>
`;
const tbody = table.querySelector('tbody');
tokens.forEach((token, index) => {
const tr = document.createElement('tr');
const surface = token.surface;
const position = token.position;
const detail = token.getDetail(0) || '*';
const allDetails = token.details.join(', ');
tr.innerHTML = `
<td><strong>${surface}</strong></td>
<td>${position}</td>
<td><small>${allDetails}</small></td>
`;
tbody.appendChild(tr);
});
resultList.appendChild(table);
});