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

View File

@ -19,9 +19,9 @@
class Translator {
constructor() {
this.dictionary = new Dictionary();
this.deinflector = new Deinflector();
this.initialized = false;
this.dictionary = new Dictionary();
this.deinflector = new Deinflector();
this.pendingLoads = [];
}
loadData(paths, callback) {
@ -30,26 +30,29 @@ class Translator {
return;
}
const loaders = [];
for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) {
loaders.push(
$.getJSON(chrome.extension.getURL(paths[key]))
);
this.pendingLoads.push(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) {
@ -80,7 +83,33 @@ class Translator {
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;
for (const result of results) {
@ -121,31 +150,19 @@ class Translator {
}
}
resultSorter(v1, v2) {
const sl1 = v1.source.length;
const sl2 = v2.source.length;
if (sl1 > sl2) {
return -1;
} else if (sl1 < sl2) {
return 1;
static loadData(url, callback) {
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => callback(xhr.responseText));
xhr.open('GET', chrome.extension.getURL(url), true);
xhr.send();
}
static parseCsv(data) {
const result = [];
for (const row in data.split('\n')) {
result.push(row.split('\t'));
}
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;
return result;
}
}