I have modified script to get dictionary meaning for Chinese characters.
In following in loadDictData()
method when localhost used to get dictionary data file then the search
return correct result but when cdn used to get dictionary data file then the search
method return different result.
This is result for all files loaded from localhost.
git clone https://github.com/cschiller/zhongwen.gitcd zhongwenpython -m http.server
Then open browser and paste below script the result will be similar.
This is result for dictionary data cedict_ts.u8 loaded from cdn. Open new tab with cdn url, then paste below script in dev console. The result is different.
let host = "http://127.0.0.1:8000";async function loadDictData() { let wordIndex = fetch(`${host}/data/cedict.idx`).then(r => r.text()); let grammarKeywords = fetch(`${host}/data/grammarKeywordsMin.json`).then(r => r.json()); let vocabKeywords = fetch(`${host}/data/vocabularyKeywordsMin.json`).then(r => r.json()); // comment and uncomment to test the result host = "https://cdn.jsdelivr.net/gh/cschiller/zhongwen@latest"; let wordDict = fetch(`${host}/data/cedict_ts.u8`).then(r => r.text()); return Promise.all([wordDict, wordIndex, grammarKeywords, vocabKeywords]);}class ZhongwenDictionary { constructor(wordDict, wordIndex, grammarKeywords, vocabKeywords) { this.wordDict = wordDict; this.wordIndex = wordIndex; this.grammarKeywords = grammarKeywords; this.vocabKeywords = vocabKeywords; this.cache = {}; } static find(needle, haystack) { let beg = 0; let end = haystack.length - 1; while (beg < end) { let mi = Math.floor((beg + end) / 2); let i = haystack.lastIndexOf('\n', mi) + 1; let mis = haystack.substr(i, needle.length); if (needle < mis) { end = i - 1; } else if (needle > mis) { beg = haystack.indexOf('\n', mi + 1) + 1; } else { return haystack.substring(i, haystack.indexOf('\n', mi + 1)); } } return null; } hasGrammarKeyword(keyword) { return this.grammarKeywords[keyword]; } hasVocabKeyword(keyword) { return this.vocabKeywords[keyword]; } wordSearch(word, max) { let entry = { data: [] }; let dict = this.wordDict; let index = this.wordIndex; let maxTrim = max || 7; let count = 0; let maxLen = 0; WHILE: while (word.length > 0) { let ix = this.cache[word]; if (!ix) { ix = ZhongwenDictionary.find(word +',', index); if (!ix) { this.cache[word] = []; continue; } ix = ix.split(','); this.cache[word] = ix; } for (let j = 1; j < ix.length; ++j) { let offset = ix[j]; let dentry = dict.substring(offset, dict.indexOf('\n', offset)); if (count >= maxTrim) { entry.more = 1; break WHILE; }++count; if (maxLen === 0) { maxLen = word.length; } entry.data.push([dentry, word]); } word = word.substr(0, word.length - 1); } if (entry.data.length === 0) { return null; } entry.matchLen = maxLen; return entry; }}async function loadDictionary() { const [wordDict, wordIndex, grammarKeywords, vocabKeywords] = await loadDictData(); return new ZhongwenDictionary(wordDict, wordIndex, grammarKeywords, vocabKeywords);}let dict;await loadDictionary().then(r => dict = r);function search(text) { if (!dict) { return; } let entry = dict.wordSearch(text); console.log("entry", entry); if (entry) { for (let i = 0; i < entry.data.length; i++) { let word = entry.data[i][1]; if (dict.hasGrammarKeyword(word) && (entry.matchLen === word.length)) { // the final index should be the last one with the maximum length entry.grammar = { keyword: word, index: i }; } if (dict.hasVocabKeyword(word) && (entry.matchLen === word.length)) { // the final index should be the last one with the maximum length entry.vocab = { keyword: word, index: i }; } } } return entry;}let res = search("你好");console.log(res);