@odict/opencc-js
Version:
The JavaScript version of Open Chinese Convert (OpenCC)
90 lines (88 loc) • 2.69 kB
JavaScript
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {
get: all[name],
enumerable: true,
configurable: true,
set: (newValue) => all[name] = () => newValue
});
};
// src/trie.ts
class Trie {
map;
constructor() {
this.map = new Map;
}
addWord(sourceText, replacementText) {
let currentMap = this.map;
for (const character of sourceText) {
const codePoint = character.codePointAt(0);
const existingNode = currentMap.get(codePoint);
if (existingNode == null) {
const newNode = new Map;
currentMap.set(codePoint, newNode);
currentMap = newNode;
} else {
currentMap = existingNode;
}
}
currentMap.trie_val = replacementText;
}
loadDict(dict) {
for (const [key, value] of Object.entries(dict)) {
this.addWord(key, value);
}
}
loadDictGroup(dicts) {
dicts.forEach((dict) => {
this.loadDict(dict);
});
}
convert(inputString) {
const rootMap = this.map;
const inputLength = inputString.length;
const resultParts = [];
let unconvertedStartIndex = null;
for (let currentIndex = 0;currentIndex < inputLength; ) {
let currentTrieNode = rootMap;
let longestMatchEndIndex = 0;
let longestMatchValue;
for (let searchIndex = currentIndex;searchIndex < inputLength; ) {
const codePoint = inputString.codePointAt(searchIndex);
searchIndex += codePoint > 65535 ? 2 : 1;
const nextTrieNode = currentTrieNode.get(codePoint);
if (typeof nextTrieNode === "undefined") {
break;
}
currentTrieNode = nextTrieNode;
const currentNodeValue = currentTrieNode.trie_val;
if (typeof currentNodeValue !== "undefined") {
longestMatchEndIndex = searchIndex;
longestMatchValue = currentNodeValue;
}
}
if (longestMatchEndIndex > 0) {
if (unconvertedStartIndex !== null) {
resultParts.push(inputString.slice(unconvertedStartIndex, currentIndex));
unconvertedStartIndex = null;
}
resultParts.push(longestMatchValue);
currentIndex = longestMatchEndIndex;
} else {
if (unconvertedStartIndex === null) {
unconvertedStartIndex = currentIndex;
}
const codePoint = inputString.codePointAt(currentIndex);
currentIndex += codePoint > 65535 ? 2 : 1;
}
}
if (unconvertedStartIndex !== null) {
resultParts.push(inputString.slice(unconvertedStartIndex, inputLength));
}
return resultParts.join("");
}
}
export {
Trie
};