Updating dictionaries

This commit is contained in:
Alex Yatskov 2016-04-12 20:58:41 -07:00
parent 7d0991f9c1
commit bf28dea283
3 changed files with 19 additions and 39 deletions

View File

@ -19,31 +19,20 @@
class Dictionary { class Dictionary {
constructor() { constructor() {
this.terms = []; this.termDicts = {};
this.termIndices = {}; this.kanjiDicts = {};
this.kanji = [];
this.kanjiIndices = {};
} }
addTermData(terms) { addTermDict(name, dict) {
let index = this.terms.length; this.termDicts[name] = dict;
for (const [e, r, g, t] of terms) {
this.storeIndex(this.termIndices, e, index);
this.storeIndex(this.termIndices, r, index++);
this.terms.push([e, r, g, t]);
}
} }
addKanjiData(kanji) { addKanjiDict(name, dict) {
let index = this.kanji.length; this.kanjiDicts[name] = dict;
for (const [c, k, o, g] of kanji) {
this.storeIndex(this.kanjiIndices, c, index++);
this.kanji.push([c, k, o, g]);
}
} }
findTerm(term) { findTerm(term) {
const results = [];
return (this.termIndices[term] || []).map(index => { return (this.termIndices[term] || []).map(index => {
const [e, r, g, t] = this.terms[index]; const [e, r, g, t] = this.terms[index];
return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')}; return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')};
@ -56,12 +45,4 @@ class Dictionary {
return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g}; return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g};
}); });
} }
storeIndex(indices, term, index) {
if (term.length > 0) {
const indices = this.termIndices[term] || [];
indices.push(index);
this.termIndices[term] = indices;
}
}
} }

View File

@ -46,11 +46,11 @@ class Translator {
this.deinflector.setRules(JSON.parse(response)); this.deinflector.setRules(JSON.parse(response));
break; break;
case 'kanjidic': case 'kanjidic':
this.dictionary.addKanjiData(Translator.parseCsv(response)); this.dictionary.addKanjiDict('kanjidic', JSON.parse(response));
break; break;
case 'edict': case 'edict':
case 'enamdict': case 'enamdict':
this.dictionary.addTermData(Translator.parseCsv(response)); this.dictionary.addTermDict(key, JSON.parse(response));
break; break;
} }
@ -164,15 +164,4 @@ class Translator {
xhr.open('GET', chrome.extension.getURL(url), true); xhr.open('GET', chrome.extension.getURL(url), true);
xhr.send(); xhr.send();
} }
static parseCsv(data) {
const result = [];
for (const row of data.split('\n')) {
if (row.length > 0) {
result.push(row.split('\t'));
}
}
return result;
}
} }

View File

@ -44,6 +44,7 @@ PARSED_TAGS = {
'aux': 'auxiliary', 'aux': 'auxiliary',
'aux-adj': 'auxiliary adjective', 'aux-adj': 'auxiliary adjective',
'aux-v': 'auxiliary verb', 'aux-v': 'auxiliary verb',
'c': 'company name',
'chn': 'children\'s language', 'chn': 'children\'s language',
'col': 'colloquialism', 'col': 'colloquialism',
'comp': 'computer terminology', 'comp': 'computer terminology',
@ -53,12 +54,15 @@ PARSED_TAGS = {
'eK': 'exclusively kanji', 'eK': 'exclusively kanji',
'ek': 'exclusively kana', 'ek': 'exclusively kana',
'exp': 'Expressions (phrases, clauses, etc.)', 'exp': 'Expressions (phrases, clauses, etc.)',
'f': 'female given name',
'fam': 'familiar language', 'fam': 'familiar language',
'fem': 'female term or language', 'fem': 'female term or language',
'food': 'food term', 'food': 'food term',
'g': 'given name, as-yet not classified by sex',
'geom': 'geometry term', 'geom': 'geometry term',
'gikun': 'gikun (meaning) reading', 'gikun': 'gikun (meaning) reading',
'gram': 'grammatical term', 'gram': 'grammatical term',
'h': 'full (usually family plus given) name of a particular person',
'hon': 'honorific or respectful (sonkeigo) language', 'hon': 'honorific or respectful (sonkeigo) language',
'hum': 'humble (kenjougo) language', 'hum': 'humble (kenjougo) language',
'iK': 'word containing irregular kanji usage', 'iK': 'word containing irregular kanji usage',
@ -68,6 +72,7 @@ PARSED_TAGS = {
'io': 'irregular okurigana usage', 'io': 'irregular okurigana usage',
'iv': 'irregular verb', 'iv': 'irregular verb',
'ling': 'linguistics terminology', 'ling': 'linguistics terminology',
'm': 'male given name',
'm-sl': 'manga slang', 'm-sl': 'manga slang',
'male': 'male term or language', 'male': 'male term or language',
'male-sl': 'male slang', 'male-sl': 'male slang',
@ -84,16 +89,21 @@ PARSED_TAGS = {
'obsc': 'obscure term', 'obsc': 'obscure term',
'ok': 'out-dated or obsolete kana usage', 'ok': 'out-dated or obsolete kana usage',
'on-mim': 'onomatopoeic or mimetic word', 'on-mim': 'onomatopoeic or mimetic word',
'p': 'place-name',
'physics': 'physics terminology', 'physics': 'physics terminology',
'pn': 'pronoun', 'pn': 'pronoun',
'poet': 'poetical term', 'poet': 'poetical term',
'pol': 'polite (teineigo) language', 'pol': 'polite (teineigo) language',
'pr': 'product name',
'pref': 'prefix', 'pref': 'prefix',
'prt': 'particle', 'prt': 'particle',
'rare': 'rare (now replaced by "obsc")', 'rare': 'rare (now replaced by "obsc")',
's': 'surname',
'sens': 'sensitive word', 'sens': 'sensitive word',
'sl': 'slang', 'sl': 'slang',
'st': 'stations',
'suf': 'suffix', 'suf': 'suffix',
'u': 'person name, either given or surname, as-yet unclassified',
'uK': 'word usually written using kanji alone', 'uK': 'word usually written using kanji alone',
'uk': 'word usually written using kana alone', 'uk': 'word usually written using kana alone',
'v1': 'Ichidan verb', 'v1': 'Ichidan verb',