Simple lookup now works.

This commit is contained in:
Alex Yatskov 2016-03-20 17:15:40 -07:00
parent 7208872b9f
commit d8a630fa28
5 changed files with 65 additions and 39 deletions

View File

@ -52,4 +52,4 @@ function onMouseDown(e) {
window.addEventListener('mousedown', onMouseDown, false); // window.addEventListener('mousedown', onMouseDown, false);

View File

@ -18,11 +18,55 @@
class Dictionary { class Dictionary {
constructor(rules) { constructor() {
this.rules = rules; this.termDicts = [];
this.kanjiDicts = [];
} }
deinflect(term, validator) { addTermDict(termDict) {
this.termDicts.push(termDict);
}
addKanjiDict(kanjiDict) {
this.kanjiDicts.push(kanjiDict);
}
findTerm(term) {
let results = [];
for (let dict of this.termDicts) {
results = results.concat(this.findTermInDict(term, dict));
}
return results;
}
findKanji(kanji) {
const results = [];
for (let dict of this.kanjiDicts) {
const result = this.findKanjiInDict(kanji, dict);
if (result !== null) {
results.push(result);
}
}
return results;
}
findTermInDict(term, dict) {
return (dict.indices[term] || []).map(index => {
const [e, r, g, t] = dict.defs[index];
return {expression: e, reading: r, glossary: g, tags: t};
});
}
findKanjiInDict(kanji, dict) {
const def = dict.defs[kanji];
if (def === null) {
return null;
}
const [c, k, o, g] = def;
return {character: c, kunyomi: k, onyomi: o, glossary: g};
} }
} }

View File

@ -18,10 +18,7 @@
class Translator { class Translator {
constructor() { constructor() {
this.rules = {}; this.dictionary = new Dictionary();
this.edict = {};
this.enamdict = {};
this.kanjidic = {};
this.initialized = false; this.initialized = false;
} }
@ -38,10 +35,9 @@ class Translator {
} }
$.when.apply($, loaders).done((rules, edict, enamdict, kanjidic) => { $.when.apply($, loaders).done((rules, edict, enamdict, kanjidic) => {
this.rules = rules; this.dictionary.addTermDict(edict[0]);
this.edict = edict; this.dictionary.addTermDict(enamdict[0]);
this.enamdict = enamdict; this.dictionary.addKanjiDict(kanjidic[0]);
this.kanjidic = kanjidic;
this.initialized = true; this.initialized = true;
@ -60,5 +56,6 @@ trans.initialize({
enamdict: 'jp/data/enamdict.json', enamdict: 'jp/data/enamdict.json',
kanjidic: 'jp/data/kanjidic.json', kanjidic: 'jp/data/kanjidic.json',
}, function() { }, function() {
alert('Loaded'); // alert('Loaded');
// alert(trans.dictionary.findTerm('猫'));
}); });

View File

@ -4,15 +4,12 @@
"description": "Yomichan Extension", "description": "Yomichan Extension",
"version": "0.0.1", "version": "0.0.1",
"browser_action": { "browser_action": {"default_icon": "icon.png"},
"default_icon": "icon.png" "background": {"page": "background.html"},
},
"background": { "page": "background.html" },
"content_scripts": [{ "content_scripts": [{
"matches": [ "http://*/*", "https://*/*" ], "matches": ["http://*/*", "https://*/*"],
"js": [ "content.js" ], "js": ["content.js"],
"run_at": "document_end" "run_at": "document_end"
}] }]
} }

View File

@ -158,27 +158,15 @@ def parse_edict(path):
defs.append((term, reading, glossary, tags)) defs.append((term, reading, glossary, tags))
term_indices = {} indices = {}
reading_indices = {}
for i, d in enumerate(defs): for i, d in enumerate(defs):
term, reading = d[:2] for key in d[:2]:
if key is not None:
values = indices.get(key, [])
values.append(i)
indices[key] = values
if term is not None: return {'defs': defs, 'indices': indices}
term_list = term_indices.get(term, [])
term_list.append(i)
term_indices[term] = term_list
if reading is not None:
reading_list = reading_indices.get(reading, [])
reading_list.append(i)
reading_indices[reading] = reading_list
return {
'defs': defs,
't_idx': term_indices,
'r_idx': reading_indices
};
def build_dict(output_dir, input_file, parser): def build_dict(output_dir, input_file, parser):