This commit is contained in:
Alex Yatskov 2016-04-01 22:13:37 -07:00
parent 7eadff3457
commit 92e62c798d
2 changed files with 67 additions and 62 deletions

View File

@ -26,7 +26,7 @@ class Dictionary {
this.kanjiIndices = {}; this.kanjiIndices = {};
} }
addTermDict(terms) { addTermData(terms) {
let index = this.terms.length; let index = this.terms.length;
for (const [e, r, g, t] in terms) { for (const [e, r, g, t] in terms) {
this.storeIndex(this.termIndices, e, index); this.storeIndex(this.termIndices, e, index);
@ -35,7 +35,7 @@ class Dictionary {
} }
} }
addKanjiDict(kanji) { addKanjiData(kanji) {
let index = this.kanji.length; let index = this.kanji.length;
for (const [c, k, o, g] in kanji) { for (const [c, k, o, g] in kanji) {
this.storeIndex(this.kanjiIndices, c, index++); this.storeIndex(this.kanjiIndices, c, index++);
@ -46,26 +46,14 @@ class Dictionary {
findTerm(term) { findTerm(term) {
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 { return {id: index, expression: e, reading: r, glossary: g, tags: t.split(' ')};
id: index,
expression: e,
reading: r,
glossary: g,
tags: t.split(' ')
};
}); });
} }
findKanji(kanji) { findKanji(kanji) {
return (this.kanjiIndices[kanji] || []).map(index => { return (this.kanjiIndices[kanji] || []).map(index => {
const [c, k, o, g] = def; const [c, k, o, g] = def;
return { return {id: index, character: c, kunyomi: k, onyomi: o, glossary: g};
id: kanji.charCodeAt(0),
character: c,
kunyomi: k,
onyomi: o,
glossary: g
};
}); });
} }

View File

@ -19,9 +19,9 @@
class Translator { class Translator {
constructor() { constructor() {
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
this.deinflector = new Deinflector(); this.deinflector = new Deinflector();
this.initialized = false; this.pendingLoads = [];
} }
loadData(paths, callback) { loadData(paths, callback) {
@ -30,26 +30,29 @@ class Translator {
return; return;
} }
const loaders = [];
for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) { for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) {
loaders.push( this.pendingLoads.push(key);
$.getJSON(chrome.extension.getURL(paths[key])) Translator.loadData(paths[key], (response) => {
); switch (key) {
case 'rules':
this.deinflector.setRules(JSON.parse(response));
break;
case 'kanjidic':
this.dictionary.addKanjiData(Translator.parseCsv(response));
break;
case 'edict':
case 'enamdict':
this.dictionary.addTermsData(Translator.parseCsv(response));
break;
}
const index = this.pendingLoads.indexOf(key);
this.pendingLoads = this.pendingLoads.splice(index, 1);
if (this.pendingLoads.length === 0) {
callback();
}
});
} }
$.when.apply($, loaders).done((rules, edict, enamdict, kanjidic) => {
this.deinflector.setRules(rules[0]);
this.dictionary.addTermDict(edict[0]);
this.dictionary.addTermDict(enamdict[0]);
this.dictionary.addKanjiDict(kanjidic[0]);
this.initialized = true;
if (callback) {
callback();
}
});
} }
findTerm(text) { findTerm(text) {
@ -80,7 +83,33 @@ class Translator {
results.push(groups[key]); results.push(groups[key]);
} }
results = results.sort(this.resultSorter); results = results.sort((v1, v2) => {
const sl1 = v1.source.length;
const sl2 = v2.source.length;
if (sl1 > sl2) {
return -1;
} else if (sl1 < sl2) {
return 1;
}
const p1 = v1.tags.indexOf('P') >= 0;
const p2 = v2.tags.indexOf('P') >= 0;
if (p1 && !p2) {
return -1;
} else if (!p1 && p2) {
return 1;
}
const rl1 = v1.rules.length;
const rl2 = v2.rules.length;
if (rl1 < rl2) {
return -1;
} else if (rl2 > rl1) {
return 1;
}
return 0;
});
let length = 0; let length = 0;
for (const result of results) { for (const result of results) {
@ -121,31 +150,19 @@ class Translator {
} }
} }
resultSorter(v1, v2) { static loadData(url, callback) {
const sl1 = v1.source.length; const xhr = new XMLHttpRequest();
const sl2 = v2.source.length; xhr.addEventListener('load', () => callback(xhr.responseText));
if (sl1 > sl2) { xhr.open('GET', chrome.extension.getURL(url), true);
return -1; xhr.send();
} else if (sl1 < sl2) { }
return 1;
static parseCsv(data) {
const result = [];
for (const row in data.split('\n')) {
result.push(row.split('\t'));
} }
const p1 = v1.tags.indexOf('P') >= 0; return result;
const p2 = v2.tags.indexOf('P') >= 0;
if (p1 && !p2) {
return -1;
} else if (!p1 && p2) {
return 1;
}
const rl1 = v1.rules.length;
const rl2 = v2.rules.length;
if (rl1 < rl2) {
return -1;
} else if (rl2 > rl1) {
return 1;
}
return 0;
} }
} }