load the dictionary asynchronously
This commit is contained in:
1 parent
35d957d68c
commit
35e520f40b
2 files changed
+76
-49
No files matched your search
+43
-45
@@ -8,64 +8,62 @@
|
||||
// We have to send an HTTP request and store the result in a string before parsing it into an object.
|
||||
// Seriously.
|
||||
|
||||
let text = "";
|
||||
let dict = undefined;
|
||||
let lookup_kan = {};
|
||||
let lookup_kana = {};
|
||||
|
||||
function listener()
|
||||
function builddict()
|
||||
{
|
||||
if (this.readyState === 4)
|
||||
{
|
||||
if (this.status === 200)
|
||||
text = this.responseText;
|
||||
{
|
||||
dict = JSON.parse(this.responseText);
|
||||
|
||||
// Build map of spellings to dictionary entry.
|
||||
|
||||
|
||||
for (let i = 0; i < dict.length; i++)
|
||||
{
|
||||
let entry = dict[i];
|
||||
if (entry.k_ele != undefined) for (let j = 0; j < entry.k_ele.length; j++)
|
||||
{
|
||||
let s = entry.k_ele[j];
|
||||
if (lookup_kan[s.keb] == undefined)
|
||||
lookup_kan[s.keb] = [i];
|
||||
else
|
||||
lookup_kan[s.keb].push(i);
|
||||
}
|
||||
for (let j = 0; j < entry.r_ele.length; j++)
|
||||
{
|
||||
let s = entry.r_ele[j];
|
||||
if (lookup_kana[s.reb] == undefined)
|
||||
lookup_kana[s.reb] = [i];
|
||||
else
|
||||
lookup_kana[s.reb].push(i);
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < 5; i++)
|
||||
{
|
||||
console.log(dict[i]);
|
||||
}
|
||||
console.log("built lookup tables");
|
||||
console.log("size:");
|
||||
console.log(Object.keys(lookup_kan).length);
|
||||
console.log(Object.keys(lookup_kana).length);
|
||||
console.log("dict size:");
|
||||
console.log(dict.length);
|
||||
}
|
||||
else
|
||||
console.error(xhr.statusText);
|
||||
}
|
||||
}
|
||||
|
||||
let req = new XMLHttpRequest();
|
||||
req.addEventListener("load", listener);
|
||||
req.addEventListener("load", builddict);
|
||||
// FIXME: apparently this has to be in an async function now.
|
||||
req.open("GET", browser.extension.getURL("dict/JMdict.json"), false);
|
||||
req.open("GET", browser.extension.getURL("dict/JMdict.json"));
|
||||
req.send();
|
||||
|
||||
let dict = JSON.parse(text);
|
||||
text = "";
|
||||
|
||||
// Build map of spellings to dictionary entry.
|
||||
|
||||
let lookup_kan = {};
|
||||
let lookup_kana = {};
|
||||
|
||||
for (let i = 0; i < dict.length; i++)
|
||||
{
|
||||
let entry = dict[i];
|
||||
if (entry.k_ele != undefined) for (let j = 0; j < entry.k_ele.length; j++)
|
||||
{
|
||||
let s = entry.k_ele[j];
|
||||
if (lookup_kan[s.keb] == undefined)
|
||||
lookup_kan[s.keb] = [i];
|
||||
else
|
||||
lookup_kan[s.keb].push(i);
|
||||
}
|
||||
for (let j = 0; j < entry.r_ele.length; j++)
|
||||
{
|
||||
let s = entry.r_ele[j];
|
||||
if (lookup_kana[s.reb] == undefined)
|
||||
lookup_kana[s.reb] = [i];
|
||||
else
|
||||
lookup_kana[s.reb].push(i);
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < 5; i++)
|
||||
{
|
||||
console.log(dict[i]);
|
||||
}
|
||||
console.log("built lookup tables");
|
||||
console.log("size:");
|
||||
console.log(Object.keys(lookup_kan).length);
|
||||
console.log(Object.keys(lookup_kana).length);
|
||||
console.log("dict size:");
|
||||
console.log(dict.length);
|
||||
|
||||
// In JMdict, part-of-speech tags are XML entities.
|
||||
// We processed JMdict's XML with entity processing disabled so we can just use the bare tags (e.g. "v1", not "ichidan verb").
|
||||
// This left the entity syntax &this; behind so we want to cut it off when we return results.
|
||||
|
||||
+33
-4
@@ -92,6 +92,8 @@ function build_div (text, result)
|
||||
for(let i = 0; i < result.length; i++)
|
||||
{
|
||||
let term = result[i];
|
||||
if(term.deconj.size > 0)
|
||||
text = term.deconj.values().next().value.text;
|
||||
//print_object(term);
|
||||
let temptag = "<span class=nazeka_word>";
|
||||
let found_kanji = true;
|
||||
@@ -234,6 +236,21 @@ function build_div (text, result)
|
||||
if(!found_kanji)
|
||||
{
|
||||
temptag += text;
|
||||
if(term.deconj)
|
||||
{
|
||||
for(let form of term.deconj)
|
||||
{
|
||||
if(form.process.length > 0)
|
||||
temptag += "~";
|
||||
for(let f = 0; f < form.process.length; f++)
|
||||
{
|
||||
let info = form.process[f];
|
||||
temptag += info;
|
||||
if(f+1 < form.process.length)
|
||||
temptag += "―";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// list alternatives
|
||||
let alternatives = [];
|
||||
@@ -350,13 +367,14 @@ function build_div (text, result)
|
||||
// deconjugation rules
|
||||
let rules = [];
|
||||
|
||||
// TODO: load rules from an underlay
|
||||
rules.push({type: "stdrule", dec_end:"る", con_end:"た", dec_tag:"v1", con_tag:"stem_past", detail:"past"});
|
||||
rules.push({type: "stdrule", dec_end:"る", con_end:"て", dec_tag:"v1", con_tag:"stem_te", detail:"te form"});
|
||||
rules.push({type: "stdrule", dec_end:"る", con_end:"ない", dec_tag:"v1", con_tag:"negative", detail:"negative form"});
|
||||
rules.push({type: "stdrule", dec_end:"る", con_end:"らない", dec_tag:"v5r", con_tag:"negative", detail:"negative form"});
|
||||
rules.push({type: "stdrule", dec_end:"る", con_end:"ない", dec_tag:"v1", con_tag:"negative", detail:"negative"});
|
||||
rules.push({type: "stdrule", dec_end:"る", con_end:"らない", dec_tag:"v5r", con_tag:"negative", detail:"negative"});
|
||||
rules.push({type: "rewriterule", dec_end:"です", con_end:"でした", dec_tag:"exp", con_tag:"aux", detail:"past"});
|
||||
|
||||
// return deconjugated form if stdrule applies to form, return otherwise
|
||||
// TODO: load rules from an underlay
|
||||
function stdrule_deconjugate(my_form, my_rule)
|
||||
{
|
||||
// can't deconjugate nothingness
|
||||
@@ -399,6 +417,17 @@ function stdrule_deconjugate(my_form, my_rule)
|
||||
|
||||
return newform;
|
||||
};
|
||||
function rewriterule_deconjugate(my_form, my_rule)
|
||||
{
|
||||
if(my_form.text != my_rule.con_end)
|
||||
return;
|
||||
return stdrule_deconjugate(my_form, my_rule);
|
||||
};
|
||||
|
||||
let rule_functions = {
|
||||
stdrule: stdrule_deconjugate,
|
||||
rewriterule: rewriterule_deconjugate,
|
||||
};
|
||||
|
||||
Set.prototype.union = function(setB)
|
||||
{
|
||||
@@ -430,7 +459,7 @@ function deconjugate(mytext)
|
||||
{
|
||||
let rule = rules[i];
|
||||
|
||||
let newform = stdrule_deconjugate(form, rule);
|
||||
let newform = rule_functions[rule.type](form, rule);
|
||||
|
||||
if(newform != undefined && !processed.has(newform) && !novel.has(newform) && !new_novel.has(newform))
|
||||
new_novel.add(newform);
|
||||
|
||||
Reference in new issue
Block a user