From b24c70523479c16eca848f5aafaa887549689ac6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 29 Sep 2017 05:41:29 +0300 Subject: basic structure for feature-merge-similar-results --- ext/bg/js/dictionary.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 57acbe5e..f3f573d3 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -207,7 +207,8 @@ async function dictFieldFormat(field, definition, mode, options) { const data = { marker, definition, - group: options.general.groupResults, + group: options.general.resultOutputMode === 'group', + merge: options.general.resultOutputMode === 'merge', modeTermKanji: mode === 'term-kanji', modeTermKana: mode === 'term-kana', modeKanji: mode === 'kanji' -- cgit v1.2.3 From 69ad4a7c9b1f859733909a75534e2005a9f56178 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 3 Oct 2017 07:20:02 +0300 Subject: merged mode: implement missing stuff, refactoring - use correct tags - indicate popular and rare terms - indicate definitions restricted to specific terms - frequencies (Innocent Corpus) --- ext/bg/js/database.js | 19 ++++-- ext/bg/js/dictionary.js | 110 ++++++++++++++++++++++++++++++++ ext/bg/js/handlebars.js | 12 ---- ext/bg/js/templates.js | 158 +++++++++++++++++++++++++--------------------- ext/bg/js/translator.js | 137 ++++++++++++++++++++++------------------ ext/bg/js/util.js | 26 ++++++++ ext/mixed/css/display.css | 8 +++ tmpl/terms.html | 17 ++++- 8 files changed, 331 insertions(+), 156 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 9787b0f8..0de0505d 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -30,7 +30,7 @@ class Database { this.db = new Dexie('dict'); this.db.version(2).stores({ - terms: '++id,dictionary,expression,reading,sequence', + terms: '++id,dictionary,expression,reading', kanji: '++,dictionary,character', tagMeta: '++,dictionary', dictionaries: '++,title,version' @@ -40,6 +40,9 @@ class Database { kanjiMeta: '++,dictionary,character', tagMeta: '++,dictionary,name' }); + this.db.version(4).stores({ + terms: '++id,dictionary,expression,reading,sequence' + }); await this.db.open(); } @@ -74,7 +77,7 @@ class Database { score: row.score, dictionary: row.dictionary, id: row.id, - sequence: row.sequence + sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence }); } }); @@ -82,14 +85,15 @@ class Database { return results; } - async findEntry(sequence) { + async findTermsBySequence(sequence, dictionary) { if (!this.db) { throw 'Database not initialized'; } - const entry = []; + const results = []; await this.db.terms.where('sequence').equals(sequence).each(row => { - entry.push({ + // if (dictionary === row.dictionary) { + results.push({ expression: row.expression, reading: row.reading, tags: dictFieldSplit(row.tags), @@ -97,11 +101,12 @@ class Database { glossary: row.glossary, score: row.score, dictionary: row.dictionary, - id: row.id + id: row.id, + sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence }); }); - return entry; + return results; } async findTermMeta(term, titles) { diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index f3f573d3..2b289a23 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -144,6 +144,77 @@ function dictTermsGroup(definitions, dictionaries) { return dictTermsSort(results); } +function dictTermsMergeBySequence(definitions) { + const definitionsBySequence = {'-1': []}; + for (const definition of definitions) { + if (definition.sequence > 0) { + if (!definitionsBySequence[definition.sequence]) { + definitionsBySequence[definition.sequence] = { + reasons: definition.reasons, + score: Number.MIN_SAFE_INTEGER, + expression: new Set(), + reading: new Set(), + expressions: new Map(), + source: definition.source, + dictionary: definition.dictionary, + definitions: [] + }; + } + const score = Math.max(definitionsBySequence[definition.sequence].score, definition.score); + definitionsBySequence[definition.sequence].score = score; + } else { + definitionsBySequence['-1'].push(definition); + } + } + + return definitionsBySequence; +} + +function dictTermsMergeByGloss(result, definitions) { + const definitionsByGloss = {}; + for (const definition of definitions) { + + const gloss = JSON.stringify(definition.glossary); + if (!definitionsByGloss[gloss]) { + definitionsByGloss[gloss] = { + expression: new Set(), + reading: new Set(), + tags: new Set(), + source: result.source, + reasons: [], + score: definition.score, + id: definition.id, + dictionary: definition.dictionary + }; + } + + definitionsByGloss[gloss].expression.add(definition.expression); + definitionsByGloss[gloss].reading.add(definition.reading); + + result.expression.add(definition.expression); + result.reading.add(definition.reading); + + // result->expressions[ Expression1[ Reading1[ Tag1, Tag2 ] ], Expression2, ... ] + if (!result.expressions.has(definition.expression)) { + result.expressions.set(definition.expression, new Map()); + } + if (!result.expressions.get(definition.expression).has(definition.reading)) { + result.expressions.get(definition.expression).set(definition.reading, new Set()); + } + + for (const tag of definition.tags) { + if (dictIsJmdictTermTag(tag)) { + // TODO: expand tags + result.expressions.get(definition.expression).get(definition.reading).add(tag); + } else { + definitionsByGloss[gloss].tags.add(tag); + } + } + } + + return definitionsByGloss; +} + function dictTagBuildSource(name) { return dictTagSanitize({name, category: 'dictionary', order: 100}); } @@ -178,6 +249,45 @@ function dictTagsSort(tags) { }); } +function dictIsJmdictTermTag(tag) { + return [ + 'P', + 'news', + 'ichi', + 'spec', + 'gai', + 'ik', + 'iK', + 'ok', + 'oK', + 'ek', + 'eK', + 'io', + 'oik', + 'ateji', + 'gikun' + ].includes(tag); +} + +function dictJmdictTermTagsRare(tags) { + const rareTags = [ + 'ik', + 'iK', + 'ok', + 'oK', + 'ek', + 'eK', + 'io', + 'oik' + ]; + for (const tag of tags) { + if (rareTags.includes(tag)) { + return true; + } + } + return false; +} + function dictFieldSplit(field) { return field.length === 0 ? [] : field.split(' '); } diff --git a/ext/bg/js/handlebars.js b/ext/bg/js/handlebars.js index 08304d43..66d5fa2b 100644 --- a/ext/bg/js/handlebars.js +++ b/ext/bg/js/handlebars.js @@ -71,16 +71,6 @@ function handlebarsKanjiLinks(options) { return result; } -function handlebarsExpressions(options) { - const definition = options.fn(this); - return definition.expression; -} - -function handlebarsReadings(options) { - const definition = options.fn(this); - return definition.reading; -} - function handlebarsMultiLine(options) { return options.fn(this).split('\n').join('
'); } @@ -93,8 +83,6 @@ function handlebarsRegisterHelpers() { Handlebars.registerHelper('furiganaPlain', handlebarsFuriganaPlain); Handlebars.registerHelper('kanjiLinks', handlebarsKanjiLinks); Handlebars.registerHelper('multiLine', handlebarsMultiLine); - Handlebars.registerHelper('expressions', handlebarsExpressions); - Handlebars.registerHelper('readings', handlebarsReadings); } } diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 2cea8b07..b9303ee4 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -204,15 +204,30 @@ templates['model.html'] = template({"1":function(container,depth0,helpers,partia templates['terms.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); - return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(5, data, 0),"inverse":container.program(9, data, 0),"data":data})) != null ? stack1 : ""); + return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.only : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(9, data, 0),"inverse":container.program(13, data, 0),"data":data})) != null ? stack1 : ""); },"2":function(container,depth0,helpers,partials,data) { var stack1; + return "
\n (" + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.only : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + " only)\n
\n"; +},"3":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = container.lambda(depth0, depth0)) != null ? stack1 : "") + + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "\n"; +},"4":function(container,depth0,helpers,partials,data) { + return ", "; +},"6":function(container,depth0,helpers,partials,data) { + var stack1; + return "
\n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(7, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
\n"; -},"3":function(container,depth0,helpers,partials,data) { +},"7":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; return " " + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) + "\n"; -},"5":function(container,depth0,helpers,partials,data) { +},"9":function(container,depth0,helpers,partials,data) { var stack1; return "\n"; -},"6":function(container,depth0,helpers,partials,data) { +},"10":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
  • "; - stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(7, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); + stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(11, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.multiLine) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
  • \n"; -},"7":function(container,depth0,helpers,partials,data) { +},"11":function(container,depth0,helpers,partials,data) { return container.escapeExpression(container.lambda(depth0, depth0)); -},"9":function(container,depth0,helpers,partials,data) { +},"13":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    "; - stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(10, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); + stack1 = ((helper = (helper = helpers.multiLine || (depth0 != null ? depth0.multiLine : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"multiLine","hash":{},"fn":container.program(14, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.multiLine) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
    \n"; -},"10":function(container,depth0,helpers,partials,data) { +},"14":function(container,depth0,helpers,partials,data) { var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)); -},"12":function(container,depth0,helpers,partials,data) { +},"16":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); return "
    \n
    \n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(13, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.playback : depth0),{"name":"if","hash":{},"fn":container.program(15, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(17, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.playback : depth0),{"name":"if","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + " \n
    \n\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(17, data, 0),"inverse":container.program(21, data, 0),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(21, data, 0),"inverse":container.program(28, data, 0),"data":data})) != null ? stack1 : "") + "\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reasons : depth0),{"name":"if","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reasons : depth0),{"name":"if","hash":{},"fn":container.program(30, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.frequencies : depth0),{"name":"if","hash":{},"fn":container.program(28, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.frequencies : depth0),{"name":"if","hash":{},"fn":container.program(34, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n
    \n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.grouped : depth0),{"name":"if","hash":{},"fn":container.program(31, data, 0),"inverse":container.program(37, data, 0),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.grouped : depth0),{"name":"if","hash":{},"fn":container.program(37, data, 0),"inverse":container.program(43, data, 0),"data":data})) != null ? stack1 : "") + "
    \n\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(40, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(46, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"13":function(container,depth0,helpers,partials,data) { +},"17":function(container,depth0,helpers,partials,data) { return " \n \n \n"; -},"15":function(container,depth0,helpers,partials,data) { +},"19":function(container,depth0,helpers,partials,data) { return " \n"; -},"17":function(container,depth0,helpers,partials,data) { - var stack1, helper, options, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=helpers.blockHelperMissing, buffer = - "
    "; - stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : alias2),(options={"name":"kanjiLinks","hash":{},"fn":container.program(18, data, 0),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); - if (!helpers.kanjiLinks) { stack1 = alias4.call(depth0,stack1,options)} - if (stack1 != null) { buffer += stack1; } - buffer += "
    \n
    "; - stack1 = ((helper = (helper = helpers.readings || (depth0 != null ? depth0.readings : depth0)) != null ? helper : alias2),(options={"name":"readings","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); - if (!helpers.readings) { stack1 = alias4.call(depth0,stack1,options)} +},"21":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.expressions : depth0),{"name":"each","hash":{},"fn":container.program(22, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"22":function(container,depth0,helpers,partials,data) { + var stack1, helper, options, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", buffer = + "
    \n "; + stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : alias2),(options={"name":"kanjiLinks","hash":{},"fn":container.program(23, data, 0),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); + if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } - return buffer + "
    \n"; -},"18":function(container,depth0,helpers,partials,data) { + return buffer + "\n " + + ((stack1 = helpers.unless.call(alias1,(data && data.last),{"name":"unless","hash":{},"fn":container.program(26, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "\n
    \n"; +},"23":function(container,depth0,helpers,partials,data) { var stack1, helper, options; - stack1 = ((helper = (helper = helpers.expressions || (depth0 != null ? depth0.expressions : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"expressions","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); - if (!helpers.expressions) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} + stack1 = ((helper = (helper = helpers.furigana || (depth0 != null ? depth0.furigana : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"furigana","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); + if (!helpers.furigana) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { return stack1; } else { return ''; } -},"19":function(container,depth0,helpers,partials,data) { +},"24":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.lambda(depth0, depth0)) != null ? stack1 : ""); -},"21":function(container,depth0,helpers,partials,data) { +},"26":function(container,depth0,helpers,partials,data) { + return "、"; +},"28":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    "; - stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"kanjiLinks","hash":{},"fn":container.program(22, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); + stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"kanjiLinks","hash":{},"fn":container.program(23, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
    \n"; -},"22":function(container,depth0,helpers,partials,data) { - var stack1, helper, options; - - stack1 = ((helper = (helper = helpers.furigana || (depth0 != null ? depth0.furigana : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"furigana","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); - if (!helpers.furigana) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} - if (stack1 != null) { return stack1; } - else { return ''; } -},"24":function(container,depth0,helpers,partials,data) { +},"30":function(container,depth0,helpers,partials,data) { var stack1; return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.reasons : depth0),{"name":"each","hash":{},"fn":container.program(25, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.reasons : depth0),{"name":"each","hash":{},"fn":container.program(31, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"25":function(container,depth0,helpers,partials,data) { +},"31":function(container,depth0,helpers,partials,data) { var stack1; return " " + container.escapeExpression(container.lambda(depth0, depth0)) + " " - + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(26, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(32, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; -},"26":function(container,depth0,helpers,partials,data) { +},"32":function(container,depth0,helpers,partials,data) { return "«"; -},"28":function(container,depth0,helpers,partials,data) { +},"34":function(container,depth0,helpers,partials,data) { var stack1; return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.frequencies : depth0),{"name":"each","hash":{},"fn":container.program(29, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.frequencies : depth0),{"name":"each","hash":{},"fn":container.program(35, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"29":function(container,depth0,helpers,partials,data) { +},"35":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; return " " @@ -335,67 +349,67 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia + ":" + alias4(((helper = (helper = helpers.frequency || (depth0 != null ? depth0.frequency : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"frequency","hash":{},"data":data}) : helper))) + "\n"; -},"31":function(container,depth0,helpers,partials,data) { +},"37":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(32, data, 0),"inverse":container.program(35, data, 0),"data":data})) != null ? stack1 : ""); -},"32":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(38, data, 0),"inverse":container.program(41, data, 0),"data":data})) != null ? stack1 : ""); +},"38":function(container,depth0,helpers,partials,data) { var stack1; return "
      \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(33, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(39, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"33":function(container,depth0,helpers,partials,data) { +},"39":function(container,depth0,helpers,partials,data) { var stack1; return "
  • " + ((stack1 = container.invokePartial(partials.definition,depth0,{"name":"definition","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + "
  • \n"; -},"35":function(container,depth0,helpers,partials,data) { +},"41":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.invokePartial(partials.definition,((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["0"] : stack1),{"name":"definition","data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"37":function(container,depth0,helpers,partials,data) { +},"43":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(31, data, 0),"inverse":container.program(38, data, 0),"data":data})) != null ? stack1 : ""); -},"38":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(37, data, 0),"inverse":container.program(44, data, 0),"data":data})) != null ? stack1 : ""); +},"44":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.invokePartial(partials.definition,depth0,{"name":"definition","data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + " "; -},"40":function(container,depth0,helpers,partials,data) { +},"46":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    ";
    -  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
    +  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
       if (!helpers.dumpObject) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)}
       if (stack1 != null) { buffer += stack1; }
       return buffer + "
    \n"; -},"42":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"48":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(43, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"43":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(49, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"49":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(44, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(50, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" + ((stack1 = container.invokePartial(partials.term,depth0,{"name":"term","hash":{"playback":(depths[1] != null ? depths[1].playback : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1]),"merged":(depths[1] != null ? depths[1].merged : depths[1]),"grouped":(depths[1] != null ? depths[1].grouped : depths[1]),"debug":(depths[1] != null ? depths[1].debug : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"44":function(container,depth0,helpers,partials,data) { +},"50":function(container,depth0,helpers,partials,data) { return "
    "; -},"46":function(container,depth0,helpers,partials,data) { +},"52":function(container,depth0,helpers,partials,data) { return "

    No results found.

    \n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return "\n\n" - + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(42, data, 0, blockParams, depths),"inverse":container.program(46, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); + + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(48, data, 0, blockParams, depths),"inverse":container.program(52, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); },"main_d": function(fn, props, container, depth0, data, blockParams, depths) { var decorators = container.decorators; fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(1, data, 0, blockParams, depths),"inverse":container.noop,"args":["definition"],"data":data}) || fn; - fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(12, data, 0, blockParams, depths),"inverse":container.noop,"args":["term"],"data":data}) || fn; + fn = decorators.inline(fn,props,container,{"name":"inline","hash":{},"fn":container.program(16, data, 0, blockParams, depths),"inverse":container.noop,"args":["term"],"data":data}) || fn; return fn; } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index db287d63..81253374 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -49,77 +49,79 @@ class Translator { } async findTermsMerged(text, dictionaries, alphanumeric) { - // const titles = Object.keys(dictionaries); + const titles = Object.keys(dictionaries); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); - // const definitionsMerged = dictTermsMerge(definitions, dictionaries, this.database); - // for (const definition of definitionsMerged) { - // await this.buildTermFrequencies(definition, titles); - // } + const definitionsBySequence = dictTermsMergeBySequence(definitions); - const sequences = {}; - const stray = []; - for (const definition of definitions) { - if (typeof definition.sequence !== 'undefined') { - if (!sequences[definition.sequence]) { - sequences[definition.sequence] = { - reasons: definition.reasons, - score: Number.MIN_SAFE_INTEGER, - expression: new Set(), - reading: new Set(), - source: definition.source, - definitions: [] - }; - } - const seq = sequences[definition.sequence]; - seq.score = Math.max(seq.score, definition.score); - } else { - stray.push(definition); + const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries); + for (const sequence in definitionsBySequence) { + if (!(sequence > 0)) { + continue; } - } - const definitionsMerged = dictTermsGroup(stray, dictionaries); - for (const sequence in sequences) { - const entry = await this.database.findEntry(Number(sequence)); - - const result = sequences[sequence]; - const glossaries = new Map(); - for (const definition of entry) { - - const gloss = definition.glossary.join('||'); - if (!glossaries.get(gloss)) { - const tags = await this.expandTags(definition.tags, definition.dictionary); - tags.push(dictTagBuildSource(definition.dictionary)); - glossaries.set(gloss, { - expressions: new Set(), - readings: new Set(), - tags: dictTagsSort(tags), // TODO: use correct tags - source: result.source, - reasons: [], - score: definition.score, - id: definition.id, - dictionary: definition.dictionary - }); + const result = definitionsBySequence[sequence]; + + const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence)); + const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); + + // postprocess glossaries + for (const gloss in definitionsByGloss) { + const definition = definitionsByGloss[gloss]; + definition.glossary = JSON.parse(gloss); + + const tags = await this.expandTags(definition.tags, definition.dictionary); + tags.push(dictTagBuildSource(definition.dictionary)); + definition.tags = dictTagsSort(tags); + + definition.only = []; + if (!utilSetEqual(definition.expression, result.expression)) { + for (const expression of utilSetIntersection(definition.expression, result.expression)) { + definition.only.push(expression); + } + } + if (!utilSetEqual(definition.reading, result.reading)) { + for (const reading of utilSetIntersection(definition.reading, result.reading)) { + definition.only.push(reading); + } } - glossaries.get(gloss).expressions.add(definition.expression); - glossaries.get(gloss).readings.add(definition.reading); - result.expression.add(definition.expression); - result.reading.add(definition.reading); + result.definitions.push(definition); } - for (const gloss of glossaries.keys()) { - const definition = glossaries.get(gloss); - definition.glossary = gloss.split('||'); - result.definitions.push(definition); + result.definitions.sort(definition => -definition.id); + + // turn the Map()/Set() mess to [{expression: E1, reading: R1}, {...}] and tag popular/normal/rare instead of actual tags + const expressions = []; + for (const expression of result.expressions.keys()) { + for (const reading of result.expressions.get(expression).keys()) { + expressions.push({ + expression: expression, + reading: reading, + jmdictTermFrequency: (tags => { + if (tags.has('P')) { + return 'popular'; + } else if (dictJmdictTermTagsRare(tags)) { + return 'rare'; + } else { + return 'normal'; + } + })(result.expressions.get(expression).get(reading)) + }); + } } - //dictTermsSort(groupDefs, dictionaries) + + result.expressions = expressions; result.expression = Array.from(result.expression).join(', '); result.reading = Array.from(result.reading).join(', '); definitionsMerged.push(result); } + for (const definition of definitionsMerged) { + await this.buildTermFrequencies(definition, titles); + } + return {length, definitions: dictTermsSort(definitionsMerged)}; } @@ -234,14 +236,23 @@ class Translator { } async buildTermFrequencies(definition, titles) { - definition.frequencies = []; - for (const meta of await this.database.findTermMeta(definition.expression, titles)) { - if (meta.mode === 'freq') { - definition.frequencies.push({ - expression: meta.expression, - frequency: meta.data, - dictionary: meta.dictionary - }); + let terms = []; + if (definition.expressions) { + terms = terms.concat(definition.expressions); + } else { + terms.push(definition); + } + + for (const term of terms) { + term.frequencies = []; + for (const meta of await this.database.findTermMeta(term.expression, titles)) { + if (meta.mode === 'freq') { + term.frequencies.push({ + expression: meta.expression, + frequency: meta.data, + dictionary: meta.dictionary + }); + } } } } diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index f44582eb..9a3a2a3e 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -26,6 +26,32 @@ function utilIsolate(data) { return JSON.parse(JSON.stringify(data)); } +function utilSetEqual(setA, setB) { + if (setA.size !== setB.size) { + return false; + } + + for (const value of setA) { + if (!setB.has(value)) { + return false; + } + } + + return true; +} + +function utilSetIntersection(setA, setB) { + return new Set( + [...setA].filter(value => setB.has(value)) + ); +} + +function utilSetDifference(setA, setB) { + return new Set( + [...setA].filter(value => !setB.has(value)) + ); +} + function utilBackend() { return chrome.extension.getBackgroundPage().yomichan_backend; } diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index cdc1be8c..04e6a326 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -124,6 +124,14 @@ hr { text-decoration: none; } +.expression-popular, .expression-popular a { + color: #0275d8; +} + +.expression-rare, .expression-rare a { + color: #999; +} + .reasons { color: #777; display: inline-block; diff --git a/tmpl/terms.html b/tmpl/terms.html index ca0dcbe9..36901b5b 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -1,4 +1,13 @@ {{#*inline "definition"}} +{{#if only}} +
    + ( + {{~#each only~}} + {{{.}}}{{#unless @last}}, {{/unless}} + {{/each}} + only) +
    +{{/if}} {{#if tags}}
    {{#each tags}} @@ -32,8 +41,12 @@
    {{#if merged}} -
    {{#kanjiLinks}}{{#expressions}}{{{.}}}{{/expressions}}{{/kanjiLinks}}
    -
    {{#readings}}{{{.}}}{{/readings}}
    + {{#each expressions}} +
    + {{#kanjiLinks}}{{#furigana}}{{{.}}}{{/furigana}}{{/kanjiLinks}} + {{#unless @last}}、{{/unless}} +
    + {{/each}} {{else}}
    {{#kanjiLinks}}{{#furigana}}{{{.}}}{{/furigana}}{{/kanjiLinks}}
    {{/if}} -- cgit v1.2.3 From cfad3b309976213c45f99bef3b8fad072c6bb9ec Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 5 Oct 2017 05:21:07 +0300 Subject: merged mode: add main dictionary selection --- ext/bg/js/database.js | 27 ++++++++++++++------------- ext/bg/js/dictionary.js | 4 ++-- ext/bg/js/options.js | 4 ++++ ext/bg/js/settings.js | 30 ++++++++++++++++++++++++++---- ext/bg/js/templates.js | 4 +++- ext/bg/js/translator.js | 5 +++-- tmpl/dictionary.html | 1 + 7 files changed, 53 insertions(+), 22 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 0de0505d..7a893fc0 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -85,25 +85,26 @@ class Database { return results; } - async findTermsBySequence(sequence, dictionary) { + async findTermsBySequence(sequence, mainDictionary) { if (!this.db) { throw 'Database not initialized'; } const results = []; await this.db.terms.where('sequence').equals(sequence).each(row => { - // if (dictionary === row.dictionary) { - results.push({ - expression: row.expression, - reading: row.reading, - tags: dictFieldSplit(row.tags), - rules: dictFieldSplit(row.rules), - glossary: row.glossary, - score: row.score, - dictionary: row.dictionary, - id: row.id, - sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence - }); + if (row.dictionary === mainDictionary) { + results.push({ + expression: row.expression, + reading: row.reading, + tags: dictFieldSplit(row.tags), + rules: dictFieldSplit(row.rules), + glossary: row.glossary, + score: row.score, + dictionary: row.dictionary, + id: row.id, + sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence + }); + } }); return results; diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 2b289a23..c20df400 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -144,10 +144,10 @@ function dictTermsGroup(definitions, dictionaries) { return dictTermsSort(results); } -function dictTermsMergeBySequence(definitions) { +function dictTermsMergeBySequence(definitions, mainDictionary) { const definitionsBySequence = {'-1': []}; for (const definition of definitions) { - if (definition.sequence > 0) { + if (mainDictionary === definition.dictionary && definition.sequence > 0) { if (!definitionsBySequence[definition.sequence]) { definitionsBySequence[definition.sequence] = { reasons: definition.reasons, diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index b49c32da..0aa6d7c6 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -154,6 +154,10 @@ function optionsSetDefaults(options) { dictionaries: {}, + dictionary: { + main: '' + }, + anki: { enable: false, server: 'http://127.0.0.1:8765', diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index c5a28a45..4d248304 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -60,7 +60,11 @@ async function formRead() { const title = dictionary.data('title'); const priority = parseInt(dictionary.find('.dict-priority').val(), 10); const enabled = dictionary.find('.dict-enabled').prop('checked'); - optionsNew.dictionaries[title] = {priority, enabled}; + const main = dictionary.find('.dict-main').prop('checked'); + if (main) { + optionsNew.dictionary.main = title; + } + optionsNew.dictionaries[title] = {priority, enabled, main}; }); return {optionsNew, optionsOld}; @@ -237,6 +241,18 @@ function dictionaryGroupsSort() { dictGroups.append(dictGroupChildren); } +function dictionarySetMain(e) { + const mainDictionary = $(e.target).closest('.dict-group'); + const mainDictionaryTitle = mainDictionary.data('title'); + + $('.dict-group').each((index, element) => { + const dictionary = $(element); + if (dictionary.data('title') !== mainDictionaryTitle) { + dictionary.find('.dict-main').prop('checked', false); + } + }); +} + async function dictionaryGroupsPopulate(options) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); @@ -247,13 +263,14 @@ async function dictionaryGroupsPopulate(options) { } for (const dictRow of dictRowsSort(dictRows, options)) { - const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0}; + const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, main: false}; const dictHtml = await apiTemplateRender('dictionary.html', { title: dictRow.title, version: dictRow.version, revision: dictRow.revision, priority: dictOptions.priority, - enabled: dictOptions.enabled + enabled: dictOptions.enabled, + main: dictOptions.main }); dictGroups.append($(dictHtml)); @@ -265,6 +282,11 @@ async function dictionaryGroupsPopulate(options) { dictionaryGroupsSort(); onFormOptionsChanged(e); }); + + $('.dict-main').change(e => { + dictionarySetMain(e); + onFormOptionsChanged(e); + }); } async function onDictionaryPurge(e) { @@ -308,7 +330,7 @@ async function onDictionaryImport(e) { const options = await optionsLoad(); const summary = await utilDatabaseImport(e.target.files[0], updateProgress); - options.dictionaries[summary.title] = {enabled: true, priority: 0}; + options.dictionaries[summary.title] = {enabled: true, priority: 0, main: false}; await optionsSave(options); await dictionaryGroupsPopulate(options); diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index c6de8580..4341322a 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -13,7 +13,9 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p + alias4(((helper = (helper = helpers.revision || (depth0 != null ? depth0.revision : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"revision","hash":{},"data":data}) : helper))) + "\n\n
    \n \n
    \n
    \n \n \n
    \n
    \n \n +
    -- cgit v1.2.3 From 72fe83d353f83d0559f9c53e7e18f609805d6fd6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 5 Oct 2017 06:56:45 +0300 Subject: merged mode: merge other results with main dict --- ext/bg/js/dictionary.js | 2 +- ext/bg/js/translator.js | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index c20df400..41c90b1c 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -89,7 +89,7 @@ function dictTermsSort(definitions, dictionaries=null) { return 1; } - return v2.expression.localeCompare(v1.expression); + return v2.expression.toString().localeCompare(v1.expression.toString()); }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 3442124f..f50c5725 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -55,7 +55,8 @@ class Translator { const definitionsBySequence = dictTermsMergeBySequence(definitions, options.dictionary.main); - const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries); + // const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries); + const definitionsMerged = []; for (const sequence in definitionsBySequence) { if (!(sequence > 0)) { continue; @@ -114,11 +115,43 @@ class Translator { result.expressions = expressions; - result.expression = Array.from(result.expression).join(', '); - result.reading = Array.from(result.reading).join(', '); + // result.expression = Array.from(result.expression).join(', '); + // result.reading = Array.from(result.reading).join(', '); definitionsMerged.push(result); } + const postMergedIndices = new Set(); + const mergeeIndicesByGloss = {}; + for (const [i, definition] of definitionsBySequence['-1'].entries()) { + for (const [j, mergedDefinition] of definitionsMerged.entries()) { + if (mergedDefinition.expression.has(definition.expression)) { + if (mergedDefinition.reading.has(definition.reading) || (definition.reading === '' && mergedDefinition.reading.size === 0)) { + if (!mergeeIndicesByGloss[definition.glossary]) { + mergeeIndicesByGloss[definition.glossary] = new Set(); + } + if (mergeeIndicesByGloss[definition.glossary].has(j)) { + continue; + } + mergedDefinition.definitions.push(definition); + mergeeIndicesByGloss[definition.glossary].add(j); + postMergedIndices.add(i); + } + } + } + } + + const strayDefinitions = []; + for (const [i, definition] of definitionsBySequence['-1'].entries()) { + if (postMergedIndices.has(i)) { + continue; + } + strayDefinitions.push(definition); + } + + for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) { + definitionsMerged.push(groupedDefinition); + } + for (const definition of definitionsMerged) { await this.buildTermFrequencies(definition, titles); } -- cgit v1.2.3 From 981d9eddb69322494589c241d049652c9091ed06 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 7 Oct 2017 01:19:40 +0300 Subject: merged mode: rewrite previous commit --- ext/bg/js/dictionary.js | 44 +++++++++++++++++++++++++++++++++++++++--- ext/bg/js/translator.js | 51 +++++++------------------------------------------ 2 files changed, 48 insertions(+), 47 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 41c90b1c..7d6d176c 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -170,9 +170,31 @@ function dictTermsMergeBySequence(definitions, mainDictionary) { return definitionsBySequence; } -function dictTermsMergeByGloss(result, definitions) { - const definitionsByGloss = {}; - for (const definition of definitions) { +function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { + const definitionsByGloss = appendTo || {}; + for (const [index, definition] of definitions.entries()) { + if (appendTo) { + let match = false; + for (const expression of result.expressions.keys()) { + if (definition.expression === expression) { + for (const reading of result.expressions.get(expression).keys()) { + if (definition.reading === reading) { + match = true; + break; + } + } + } + if (match) { + break; + } + } + + if (!match) { + continue; + } else if (mergedIndices) { + mergedIndices.add(index); + } + } const gloss = JSON.stringify(definition.glossary); if (!definitionsByGloss[gloss]) { @@ -180,6 +202,7 @@ function dictTermsMergeByGloss(result, definitions) { expression: new Set(), reading: new Set(), tags: new Set(), + glossary: definition.glossary, source: result.source, reasons: [], score: definition.score, @@ -212,6 +235,21 @@ function dictTermsMergeByGloss(result, definitions) { } } + for (const gloss in definitionsByGloss) { + const definition = definitionsByGloss[gloss]; + definition.only = []; + if (!utilSetEqual(definition.expression, result.expression)) { + for (const expression of utilSetIntersection(definition.expression, result.expression)) { + definition.only.push(expression); + } + } + if (!utilSetEqual(definition.reading, result.reading)) { + for (const reading of utilSetIntersection(definition.reading, result.reading)) { + definition.only.push(reading); + } + } + } + return definitionsByGloss; } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f50c5725..f959ea35 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -55,8 +55,8 @@ class Translator { const definitionsBySequence = dictTermsMergeBySequence(definitions, options.dictionary.main); - // const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries); const definitionsMerged = []; + const mergedByTermIndices = new Set(); for (const sequence in definitionsBySequence) { if (!(sequence > 0)) { continue; @@ -66,32 +66,19 @@ class Translator { const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.dictionary.main); const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); + dictTermsMergeByGloss(result, definitionsBySequence['-1'], definitionsByGloss, mergedByTermIndices); - // postprocess glossaries for (const gloss in definitionsByGloss) { const definition = definitionsByGloss[gloss]; - definition.glossary = JSON.parse(gloss); const tags = await this.expandTags(definition.tags, definition.dictionary); tags.push(dictTagBuildSource(definition.dictionary)); definition.tags = dictTagsSort(tags); - definition.only = []; - if (!utilSetEqual(definition.expression, result.expression)) { - for (const expression of utilSetIntersection(definition.expression, result.expression)) { - definition.only.push(expression); - } - } - if (!utilSetEqual(definition.reading, result.reading)) { - for (const reading of utilSetIntersection(definition.reading, result.reading)) { - definition.only.push(reading); - } - } - result.definitions.push(definition); } - result.definitions.sort(definition => -definition.id); + dictTermsSort(result.definitions, dictionaries); // turn the Map()/Set() mess to [{expression: E1, reading: R1}, {...}] and tag popular/normal/rare instead of actual tags const expressions = []; @@ -120,34 +107,7 @@ class Translator { definitionsMerged.push(result); } - const postMergedIndices = new Set(); - const mergeeIndicesByGloss = {}; - for (const [i, definition] of definitionsBySequence['-1'].entries()) { - for (const [j, mergedDefinition] of definitionsMerged.entries()) { - if (mergedDefinition.expression.has(definition.expression)) { - if (mergedDefinition.reading.has(definition.reading) || (definition.reading === '' && mergedDefinition.reading.size === 0)) { - if (!mergeeIndicesByGloss[definition.glossary]) { - mergeeIndicesByGloss[definition.glossary] = new Set(); - } - if (mergeeIndicesByGloss[definition.glossary].has(j)) { - continue; - } - mergedDefinition.definitions.push(definition); - mergeeIndicesByGloss[definition.glossary].add(j); - postMergedIndices.add(i); - } - } - } - } - - const strayDefinitions = []; - for (const [i, definition] of definitionsBySequence['-1'].entries()) { - if (postMergedIndices.has(i)) { - continue; - } - strayDefinitions.push(definition); - } - + const strayDefinitions = definitionsBySequence['-1'].filter((definition, index) => !mergedByTermIndices.has(index)); for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) { definitionsMerged.push(groupedDefinition); } @@ -294,6 +254,9 @@ class Translator { async expandTags(names, title) { const tags = []; for (const name of names) { + if (typeof name !== 'string') { + continue; + } const base = name.split(':')[0]; const meta = await this.database.findTagForTitle(base, title); -- cgit v1.2.3 From 94d590fc85f87028d8264e93c8ad58c71d0977f2 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Sat, 7 Oct 2017 07:02:50 +0300 Subject: merged mode: support audio and tags for terms --- ext/bg/js/dictionary.js | 1 - ext/bg/js/templates.js | 116 ++++++++++++++++++++++++++++------------------ ext/bg/js/translator.js | 3 ++ ext/mixed/css/display.css | 20 ++++++-- ext/mixed/js/display.js | 10 ++-- tmpl/terms.html | 16 ++++++- 6 files changed, 111 insertions(+), 55 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 7d6d176c..b81d1411 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -227,7 +227,6 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { for (const tag of definition.tags) { if (dictIsJmdictTermTag(tag)) { - // TODO: expand tags result.expressions.get(definition.expression).get(definition.reading).add(tag); } else { definitionsByGloss[gloss].tags.add(tag); diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 4341322a..01507e1c 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -265,85 +265,109 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia var stack1; return container.escapeExpression(container.lambda(((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["0"] : stack1), depth0)); -},"16":function(container,depth0,helpers,partials,data) { +},"16":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); return "
    \n
    \n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(17, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.playback : depth0),{"name":"if","hash":{},"fn":container.program(19, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.addable : depth0),{"name":"if","hash":{},"fn":container.program(17, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.merged : depth0),{"name":"unless","hash":{},"fn":container.program(19, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + " \n
    \n\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(21, data, 0),"inverse":container.program(28, data, 0),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(22, data, 0, blockParams, depths),"inverse":container.program(34, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "") + "\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reasons : depth0),{"name":"if","hash":{},"fn":container.program(30, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reasons : depth0),{"name":"if","hash":{},"fn":container.program(36, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.frequencies : depth0),{"name":"if","hash":{},"fn":container.program(34, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.frequencies : depth0),{"name":"if","hash":{},"fn":container.program(40, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n
    \n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.grouped : depth0),{"name":"if","hash":{},"fn":container.program(37, data, 0),"inverse":container.program(43, data, 0),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.grouped : depth0),{"name":"if","hash":{},"fn":container.program(43, data, 0, blockParams, depths),"inverse":container.program(49, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "") + "
    \n\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(46, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(52, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; },"17":function(container,depth0,helpers,partials,data) { return " \n \n \n"; },"19":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.playback : depth0),{"name":"if","hash":{},"fn":container.program(20, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"20":function(container,depth0,helpers,partials,data) { return " \n"; -},"21":function(container,depth0,helpers,partials,data) { +},"22":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.expressions : depth0),{"name":"each","hash":{},"fn":container.program(22, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"22":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.expressions : depth0),{"name":"each","hash":{},"fn":container.program(23, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"23":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1, helper, options, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", buffer = "
    \n "; - stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : alias2),(options={"name":"kanjiLinks","hash":{},"fn":container.program(23, data, 0),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); + stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : alias2),(options={"name":"kanjiLinks","hash":{},"fn":container.program(24, data, 0, blockParams, depths),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } - return buffer + "" - + ((stack1 = helpers.unless.call(alias1,(data && data.last),{"name":"unless","hash":{},"fn":container.program(26, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return buffer + "
    " + + ((stack1 = helpers["if"].call(alias1,(depths[1] != null ? depths[1].playback : depths[1]),{"name":"if","hash":{},"fn":container.program(27, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(29, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "
    " + + ((stack1 = helpers.unless.call(alias1,(data && data.last),{"name":"unless","hash":{},"fn":container.program(32, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n
    \n"; -},"23":function(container,depth0,helpers,partials,data) { +},"24":function(container,depth0,helpers,partials,data) { var stack1, helper, options; - stack1 = ((helper = (helper = helpers.furigana || (depth0 != null ? depth0.furigana : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"furigana","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); + stack1 = ((helper = (helper = helpers.furigana || (depth0 != null ? depth0.furigana : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"furigana","hash":{},"fn":container.program(25, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.furigana) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { return stack1; } else { return ''; } -},"24":function(container,depth0,helpers,partials,data) { +},"25":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.lambda(depth0, depth0)) != null ? stack1 : ""); -},"26":function(container,depth0,helpers,partials,data) { +},"27":function(container,depth0,helpers,partials,data) { + return "
    \n"; +},"29":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(30, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + " "; +},"30":function(container,depth0,helpers,partials,data) { + var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + + return "" + + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) + + "\n"; +},"32":function(container,depth0,helpers,partials,data) { return "、"; -},"28":function(container,depth0,helpers,partials,data) { +},"34":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    "; - stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"kanjiLinks","hash":{},"fn":container.program(23, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); + stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"kanjiLinks","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper)); if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} if (stack1 != null) { buffer += stack1; } return buffer + "
    \n"; -},"30":function(container,depth0,helpers,partials,data) { +},"36":function(container,depth0,helpers,partials,data) { var stack1; return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.reasons : depth0),{"name":"each","hash":{},"fn":container.program(31, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.reasons : depth0),{"name":"each","hash":{},"fn":container.program(37, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"31":function(container,depth0,helpers,partials,data) { +},"37":function(container,depth0,helpers,partials,data) { var stack1; return " " + container.escapeExpression(container.lambda(depth0, depth0)) + " " - + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(32, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(38, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; -},"32":function(container,depth0,helpers,partials,data) { +},"38":function(container,depth0,helpers,partials,data) { return "«"; -},"34":function(container,depth0,helpers,partials,data) { +},"40":function(container,depth0,helpers,partials,data) { var stack1; return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.frequencies : depth0),{"name":"each","hash":{},"fn":container.program(35, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.frequencies : depth0),{"name":"each","hash":{},"fn":container.program(41, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"35":function(container,depth0,helpers,partials,data) { +},"41":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; return " " @@ -351,61 +375,61 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia + ":" + alias4(((helper = (helper = helpers.frequency || (depth0 != null ? depth0.frequency : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"frequency","hash":{},"data":data}) : helper))) + "\n"; -},"37":function(container,depth0,helpers,partials,data) { +},"43":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(38, data, 0),"inverse":container.program(41, data, 0),"data":data})) != null ? stack1 : ""); -},"38":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(44, data, 0),"inverse":container.program(47, data, 0),"data":data})) != null ? stack1 : ""); +},"44":function(container,depth0,helpers,partials,data) { var stack1; return "
      \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(39, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(45, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"39":function(container,depth0,helpers,partials,data) { +},"45":function(container,depth0,helpers,partials,data) { var stack1; return "
  • " + ((stack1 = container.invokePartial(partials.definition,depth0,{"name":"definition","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + "
  • \n"; -},"41":function(container,depth0,helpers,partials,data) { +},"47":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.invokePartial(partials.definition,((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["0"] : stack1),{"name":"definition","data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"43":function(container,depth0,helpers,partials,data) { +},"49":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(37, data, 0),"inverse":container.program(44, data, 0),"data":data})) != null ? stack1 : ""); -},"44":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(43, data, 0, blockParams, depths),"inverse":container.program(50, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); +},"50":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.invokePartial(partials.definition,depth0,{"name":"definition","data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + " "; -},"46":function(container,depth0,helpers,partials,data) { +},"52":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    ";
    -  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(24, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
    +  stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(25, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
       if (!helpers.dumpObject) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)}
       if (stack1 != null) { buffer += stack1; }
       return buffer + "
    \n"; -},"48":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"54":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(49, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"49":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(55, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"55":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(50, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(56, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" + ((stack1 = container.invokePartial(partials.term,depth0,{"name":"term","hash":{"playback":(depths[1] != null ? depths[1].playback : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1]),"merged":(depths[1] != null ? depths[1].merged : depths[1]),"grouped":(depths[1] != null ? depths[1].grouped : depths[1]),"debug":(depths[1] != null ? depths[1].debug : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"50":function(container,depth0,helpers,partials,data) { +},"56":function(container,depth0,helpers,partials,data) { return "
    "; -},"52":function(container,depth0,helpers,partials,data) { +},"58":function(container,depth0,helpers,partials,data) { return "

    No results found.

    \n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return "\n\n" - + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(48, data, 0, blockParams, depths),"inverse":container.program(52, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); + + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(54, data, 0, blockParams, depths),"inverse":container.program(58, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); },"main_d": function(fn, props, container, depth0, data, blockParams, depths) { var decorators = container.decorators; diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index f959ea35..678bc613 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -84,9 +84,11 @@ class Translator { const expressions = []; for (const expression of result.expressions.keys()) { for (const reading of result.expressions.get(expression).keys()) { + const tags = await this.expandTags(result.expressions.get(expression).get(reading), result.dictionary); expressions.push({ expression: expression, reading: reading, + tags: dictTagsSort(tags), jmdictTermFrequency: (tags => { if (tags.has('P')) { return 'popular'; @@ -109,6 +111,7 @@ class Translator { const strayDefinitions = definitionsBySequence['-1'].filter((definition, index) => !mergedByTermIndices.has(index)); for (const groupedDefinition of dictTermsGroup(strayDefinitions, dictionaries)) { + groupedDefinition.expressions = [{expression: groupedDefinition.expression, reading: groupedDefinition.reading}]; definitionsMerged.push(groupedDefinition); } diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index 04e6a326..147256bf 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -118,20 +118,34 @@ hr { font-size: 24px; } -.expression a { +.expression .kanji-link { border-bottom: 1px #777 dashed; color: #333; text-decoration: none; } -.expression-popular, .expression-popular a { +.expression-popular, .expression-popular .kanji-link { color: #0275d8; } -.expression-rare, .expression-rare a { +.expression-rare, .expression-rare .kanji-link { color: #999; } +.expression .peek-wrapper { + font-size: 14px; + white-space: nowrap; + display: inline-block; + position: relative; + width: 0px; + height: 0px; + visibility: hidden; +} + +.expression:hover .peek-wrapper { + visibility: visible; +} + .reasons { color: #777; display: inline-block; diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 2dd95692..e54c8b18 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -71,8 +71,10 @@ class Display { onAudioPlay(e) { e.preventDefault(); - const index = Display.entryIndexFind($(e.currentTarget)); - this.audioPlay(this.definitions[index]); + const link = $(e.currentTarget); + const definitionIndex = Display.entryIndexFind(link); + const expressionIndex = $(e.currentTarget).closest('.entry').find('.expression .action-play-audio').index(link); + this.audioPlay(this.definitions[definitionIndex], expressionIndex); } onNoteAdd(e) { @@ -380,11 +382,11 @@ class Display { } } - async audioPlay(definition) { + async audioPlay(definition, expressionIndex) { try { this.spinner.show(); - let url = await apiAudioGetUrl(definition, this.options.general.audioSource); + let url = await apiAudioGetUrl(expressionIndex === -1 ? definition : definition.expressions[expressionIndex], this.options.general.audioSource); if (!url) { url = '/mixed/mp3/button.mp3'; } diff --git a/tmpl/terms.html b/tmpl/terms.html index 505ae5d4..40a44c12 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -34,16 +34,30 @@ {{/if}} + {{#unless merged}} {{#if playback}} {{/if}} + {{/unless}}
    {{#if merged}} {{#each expressions}}
    - {{#kanjiLinks}}{{#furigana}}{{{.}}}{{/furigana}}{{/kanjiLinks}}{{#unless @last}}、{{/unless}} + {{#kanjiLinks}}{{#furigana}}{{{.}}}{{/furigana}}{{/kanjiLinks}}
    + {{~#if ../playback~}} +
    + {{/if}} + {{~#if tags~}} + {{~#each tags~}} + {{name}} + {{/each}} + {{/if}}
    {{~#unless @last~}}、{{/unless}}
    {{/each}} {{else}} -- cgit v1.2.3 From 84420e00bfe025c93ed6ef77f81634687ff64764 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Mon, 9 Oct 2017 23:22:12 +0300 Subject: merged mode: fix tags for non-main dictionaries --- ext/bg/js/dictionary.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index b81d1411..25bcaf69 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -228,8 +228,10 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { for (const tag of definition.tags) { if (dictIsJmdictTermTag(tag)) { result.expressions.get(definition.expression).get(definition.reading).add(tag); - } else { + } else if (typeof tag === 'string') { definitionsByGloss[gloss].tags.add(tag); + } else if (tag.category && tag.category !== 'dictionary') { + definitionsByGloss[gloss].tags.add(tag.name); } } } -- cgit v1.2.3 From e1c5d7a401f1f974da1bedd8600524d982c2c7de Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 10 Oct 2017 06:04:49 +0300 Subject: merged mode: make gloss hash more unique MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use ['gloss', 'ary'].concat('DictName') Known collision: 日本国有鉄道 in JMdict and JMnedict --- ext/bg/js/dictionary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 25bcaf69..b6ca6fd5 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -196,7 +196,7 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { } } - const gloss = JSON.stringify(definition.glossary); + const gloss = JSON.stringify(definition.glossary.concat(definition.dictionary)); if (!definitionsByGloss[gloss]) { definitionsByGloss[gloss] = { expression: new Set(), -- cgit v1.2.3 From 4fb983a70cd02c0c0126a26e70d40261d5b2a51d Mon Sep 17 00:00:00 2001 From: siikamiika Date: Wed, 11 Oct 2017 09:18:55 +0300 Subject: add termTags The dictionary tags field can now have a '\t' in it, and it is used to separate tags associated with definitions and terms. --- ext/bg/js/database.js | 8 +++++-- ext/bg/js/dictionary.js | 31 +++++-------------------- ext/bg/js/templates.js | 60 ++++++++++++++++++++++++++++++++----------------- ext/bg/js/translator.js | 2 ++ tmpl/terms.html | 8 +++++++ 5 files changed, 61 insertions(+), 48 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 7a893fc0..1c2194df 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -68,10 +68,12 @@ class Database { const results = []; await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { if (titles.includes(row.dictionary)) { + const tags = row.tags.split('\t'); results.push({ expression: row.expression, reading: row.reading, - tags: dictFieldSplit(row.tags), + tags: dictFieldSplit(tags[0]), + termTags: tags.length > 1 ? dictFieldSplit(tags[1]) : [], rules: dictFieldSplit(row.rules), glossary: row.glossary, score: row.score, @@ -93,10 +95,12 @@ class Database { const results = []; await this.db.terms.where('sequence').equals(sequence).each(row => { if (row.dictionary === mainDictionary) { + const tags = row.tags.split('\t'); results.push({ expression: row.expression, reading: row.reading, - tags: dictFieldSplit(row.tags), + tags: dictFieldSplit(tags[0]), + termTags: tags.length > 1 ? dictFieldSplit(tags[1]) : [], rules: dictFieldSplit(row.rules), glossary: row.glossary, score: row.score, diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index b6ca6fd5..ee4f5946 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -136,6 +136,7 @@ function dictTermsGroup(definitions, dictionaries) { expression: firstDef.expression, reading: firstDef.reading, reasons: firstDef.reasons, + termTags: groupDefs[0].termTags, score: groupDefs.reduce((p, v) => v.score > p ? v.score : p, Number.MIN_SAFE_INTEGER), source: firstDef.source }); @@ -226,14 +227,16 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { } for (const tag of definition.tags) { - if (dictIsJmdictTermTag(tag)) { - result.expressions.get(definition.expression).get(definition.reading).add(tag); - } else if (typeof tag === 'string') { + if (typeof tag === 'string') { definitionsByGloss[gloss].tags.add(tag); } else if (tag.category && tag.category !== 'dictionary') { definitionsByGloss[gloss].tags.add(tag.name); } } + + for (const tag of definition.termTags) { + result.expressions.get(definition.expression).get(definition.reading).add(tag); + } } for (const gloss in definitionsByGloss) { @@ -288,34 +291,12 @@ function dictTagsSort(tags) { }); } -function dictIsJmdictTermTag(tag) { - return [ - 'P', - 'news', - 'ichi', - 'spec', - 'gai', - 'ik', - 'iK', - 'ok', - 'oK', - 'ek', - 'eK', - 'io', - 'oik', - 'ateji', - 'gikun' - ].includes(tag); -} - function dictJmdictTermTagsRare(tags) { const rareTags = [ 'ik', 'iK', 'ok', 'oK', - 'ek', - 'eK', 'io', 'oik' ]; diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 9a576479..19c49337 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -276,11 +276,13 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia + "\n" + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.reasons : depth0),{"name":"if","hash":{},"fn":container.program(39, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.frequencies : depth0),{"name":"if","hash":{},"fn":container.program(43, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.termTags : depth0),{"name":"if","hash":{},"fn":container.program(43, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "\n" + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.frequencies : depth0),{"name":"if","hash":{},"fn":container.program(46, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n
    \n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.grouped : depth0),{"name":"if","hash":{},"fn":container.program(46, data, 0, blockParams, depths),"inverse":container.program(52, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.grouped : depth0),{"name":"if","hash":{},"fn":container.program(49, data, 0, blockParams, depths),"inverse":container.program(55, data, 0, blockParams, depths),"data":data})) != null ? stack1 : "") + "
    \n\n" - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(55, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.debug : depth0),{"name":"if","hash":{},"fn":container.program(58, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n"; },"17":function(container,depth0,helpers,partials,data) { return " \n \n \n"; @@ -381,71 +383,87 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia var stack1; return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.frequencies : depth0),{"name":"each","hash":{},"fn":container.program(44, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.termTags : depth0),{"name":"each","hash":{},"fn":container.program(44, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; },"44":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + return " " + + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) + + "\n"; +},"46":function(container,depth0,helpers,partials,data) { + var stack1; + + return "
    \n" + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.frequencies : depth0),{"name":"each","hash":{},"fn":container.program(47, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "
    \n"; +},"47":function(container,depth0,helpers,partials,data) { + var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; + return " " + alias4(((helper = (helper = helpers.dictionary || (depth0 != null ? depth0.dictionary : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"dictionary","hash":{},"data":data}) : helper))) + ":" + alias4(((helper = (helper = helpers.frequency || (depth0 != null ? depth0.frequency : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"frequency","hash":{},"data":data}) : helper))) + "\n"; -},"46":function(container,depth0,helpers,partials,data) { +},"49":function(container,depth0,helpers,partials,data) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(47, data, 0),"inverse":container.program(50, data, 0),"data":data})) != null ? stack1 : ""); -},"47":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(50, data, 0),"inverse":container.program(53, data, 0),"data":data})) != null ? stack1 : ""); +},"50":function(container,depth0,helpers,partials,data) { var stack1; return "
      \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(48, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(51, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"48":function(container,depth0,helpers,partials,data) { +},"51":function(container,depth0,helpers,partials,data) { var stack1; return "
  • " + ((stack1 = container.invokePartial(partials.definition,depth0,{"name":"definition","data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + "
  • \n"; -},"50":function(container,depth0,helpers,partials,data) { +},"53":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.invokePartial(partials.definition,((stack1 = (depth0 != null ? depth0.definitions : depth0)) != null ? stack1["0"] : stack1),{"name":"definition","data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"52":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"55":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(46, data, 0, blockParams, depths),"inverse":container.program(53, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); -},"53":function(container,depth0,helpers,partials,data) { + return ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.merged : depth0),{"name":"if","hash":{},"fn":container.program(49, data, 0, blockParams, depths),"inverse":container.program(56, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); +},"56":function(container,depth0,helpers,partials,data) { var stack1; return ((stack1 = container.invokePartial(partials.definition,depth0,{"name":"definition","data":data,"indent":" ","helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : "") + " "; -},"55":function(container,depth0,helpers,partials,data) { +},"58":function(container,depth0,helpers,partials,data) { var stack1, helper, options, buffer = "
    ";
       stack1 = ((helper = (helper = helpers.dumpObject || (depth0 != null ? depth0.dumpObject : depth0)) != null ? helper : helpers.helperMissing),(options={"name":"dumpObject","hash":{},"fn":container.program(25, data, 0),"inverse":container.noop,"data":data}),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),options) : helper));
       if (!helpers.dumpObject) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)}
       if (stack1 != null) { buffer += stack1; }
       return buffer + "
    \n"; -},"57":function(container,depth0,helpers,partials,data,blockParams,depths) { +},"60":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(58, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); -},"58":function(container,depth0,helpers,partials,data,blockParams,depths) { + return ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"each","hash":{},"fn":container.program(61, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : ""); +},"61":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; - return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(59, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.first),{"name":"unless","hash":{},"fn":container.program(62, data, 0, blockParams, depths),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "\n" + ((stack1 = container.invokePartial(partials.term,depth0,{"name":"term","hash":{"playback":(depths[1] != null ? depths[1].playback : depths[1]),"addable":(depths[1] != null ? depths[1].addable : depths[1]),"merged":(depths[1] != null ? depths[1].merged : depths[1]),"grouped":(depths[1] != null ? depths[1].grouped : depths[1]),"debug":(depths[1] != null ? depths[1].debug : depths[1])},"data":data,"helpers":helpers,"partials":partials,"decorators":container.decorators})) != null ? stack1 : ""); -},"59":function(container,depth0,helpers,partials,data) { +},"62":function(container,depth0,helpers,partials,data) { return "
    "; -},"61":function(container,depth0,helpers,partials,data) { +},"64":function(container,depth0,helpers,partials,data) { return "

    No results found.

    \n"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1; return "\n\n" - + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(57, data, 0, blockParams, depths),"inverse":container.program(61, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); + + ((stack1 = helpers["if"].call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitions : depth0),{"name":"if","hash":{},"fn":container.program(60, data, 0, blockParams, depths),"inverse":container.program(64, data, 0, blockParams, depths),"data":data})) != null ? stack1 : ""); },"main_d": function(fn, props, container, depth0, data, blockParams, depths) { var decorators = container.decorators; diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 678bc613..737abd08 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -154,6 +154,7 @@ class Translator { for (const definition of deinflection.definitions) { const tags = await this.expandTags(definition.tags, definition.dictionary); tags.push(dictTagBuildSource(definition.dictionary)); + const termTags = await this.expandTags(definition.termTags, definition.dictionary); definitions.push({ source: deinflection.source, @@ -165,6 +166,7 @@ class Translator { reading: definition.reading, glossary: definition.glossary, tags: dictTagsSort(tags), + termTags: dictTagsSort(termTags), sequence: definition.sequence }); } diff --git a/tmpl/terms.html b/tmpl/terms.html index cc0f600e..72bf9824 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -80,6 +80,14 @@ {{/if}} + {{#if termTags}} +
    + {{#each termTags}} + {{name}} + {{/each}} +
    + {{/if}} + {{#if frequencies}}
    {{#each frequencies}} -- cgit v1.2.3 From 8d660e282911ffb5d7b504784a3c09f13b164953 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 12 Oct 2017 09:59:09 +0300 Subject: add compact tags --- ext/bg/js/dictionary.js | 26 ++++++++++++++++++++++++++ ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 2 ++ ext/bg/js/templates.js | 36 ++++++++++++++++++------------------ ext/bg/js/translator.js | 13 +++++++++++++ ext/bg/settings.html | 4 ++++ tmpl/terms.html | 14 +++++++------- 7 files changed, 72 insertions(+), 26 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index ee4f5946..14f90d29 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -110,6 +110,32 @@ function dictTermsUndupe(definitions) { return definitionsUnique; } +function dictTermsCompressTags(definitions) { + let lastDictionary = ''; + let lastPos = ''; + + for (const definition of definitions) { + const dictionary = JSON.stringify(definition.tags.filter(tag => tag.category === 'dictionary').map(tag => tag.name).sort()); + const pos = JSON.stringify(definition.tags.filter(tag => tag.category === 'pos').map(tag => tag.name).sort()); + + const filterOutCategories = []; + + if (lastDictionary === dictionary) { + filterOutCategories.push('dictionary'); + } else { + lastDictionary = dictionary; + } + + if (lastPos === pos) { + filterOutCategories.push('pos'); + } else { + lastPos = pos; + } + + definition.tags = definition.tags.filter(tag => !filterOutCategories.includes(tag.category)); + } +} + function dictTermsGroup(definitions, dictionaries) { const groups = {}; for (const definition of definitions) { diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index b49c32da..eef893c7 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -139,7 +139,8 @@ function optionsSetDefaults(options) { popupWidth: 400, popupHeight: 250, popupOffset: 10, - showGuide: true + showGuide: true, + compactTags: false }, scanning: { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 6016b38a..f59c3ad0 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -22,6 +22,7 @@ async function formRead() { const optionsNew = $.extend(true, {}, optionsOld); optionsNew.general.showGuide = $('#show-usage-guide').prop('checked'); + optionsNew.general.compactTags = $('#compact-tags').prop('checked'); optionsNew.general.resultOutputMode = $('#result-output-mode').val(); optionsNew.general.audioSource = $('#audio-playback-source').val(); optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); @@ -125,6 +126,7 @@ async function onReady() { const options = await optionsLoad(); $('#show-usage-guide').prop('checked', options.general.showGuide); + $('#compact-tags').prop('checked', options.general.compactTags); $('#result-output-mode').val(options.general.resultOutputMode); $('#audio-playback-source').val(options.general.audioSource); $('#audio-playback-volume').val(options.general.audioVolume); diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 19c49337..e8bd43e9 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -206,30 +206,16 @@ templates['model.html'] = template({"1":function(container,depth0,helpers,partia templates['terms.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); - return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.only : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.only : depth0),{"name":"if","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(9, data, 0),"inverse":container.program(13, data, 0),"data":data})) != null ? stack1 : ""); },"2":function(container,depth0,helpers,partials,data) { var stack1; - return "
    \n (" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.only : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + " only)\n
    \n"; -},"3":function(container,depth0,helpers,partials,data) { - var stack1; - - return ((stack1 = container.lambda(depth0, depth0)) != null ? stack1 : "") - + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(4, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") - + "\n"; -},"4":function(container,depth0,helpers,partials,data) { - return ", "; -},"6":function(container,depth0,helpers,partials,data) { - var stack1; - return "
    \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(7, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
    \n"; -},"7":function(container,depth0,helpers,partials,data) { +},"3":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; return " " + alias4(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"name","hash":{},"data":data}) : helper))) + "\n"; +},"5":function(container,depth0,helpers,partials,data) { + var stack1; + + return "
    \n (" + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.only : depth0),{"name":"each","hash":{},"fn":container.program(6, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + " only)\n
    \n"; +},"6":function(container,depth0,helpers,partials,data) { + var stack1; + + return ((stack1 = container.lambda(depth0, depth0)) != null ? stack1 : "") + + ((stack1 = helpers.unless.call(depth0 != null ? depth0 : (container.nullContext || {}),(data && data.last),{"name":"unless","hash":{},"fn":container.program(7, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + "\n"; +},"7":function(container,depth0,helpers,partials,data) { + return ", "; },"9":function(container,depth0,helpers,partials,data) { var stack1; diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 99344b95..e307efc0 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -37,6 +37,7 @@ class Translator { } async findTermsGrouped(text, dictionaries, alphanumeric) { + const options = await apiOptionsGet(); const titles = Object.keys(dictionaries); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); @@ -45,6 +46,12 @@ class Translator { await this.buildTermFrequencies(definition, titles); } + if (options.general.compactTags) { + for (const definition of definitionsGrouped) { + dictTermsCompressTags(definition.definitions); + } + } + return {length, definitions: definitionsGrouped}; } @@ -120,6 +127,12 @@ class Translator { await this.buildTermFrequencies(definition, titles); } + if (options.general.compactTags) { + for (const definition of definitionsMerged) { + dictTermsCompressTags(definition.definitions); + } + } + return {length, definitions: dictTermsSort(definitionsMerged)}; } diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 6274f3cb..d4b71932 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -35,6 +35,10 @@
    +
    + +
    +
    diff --git a/tmpl/terms.html b/tmpl/terms.html index 72bf9824..e037c544 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -1,4 +1,11 @@ {{#*inline "definition"}} +{{#if tags}} +
    + {{#each tags}} + {{name}} + {{/each}} +
    +{{/if}} {{#if only}}
    ( @@ -8,13 +15,6 @@ only)
    {{/if}} -{{#if tags}} -
    - {{#each tags}} - {{name}} - {{/each}} -
    -{{/if}} {{#if glossary.[1]}}
      {{#each glossary}} -- cgit v1.2.3 From c0f56480bb34b8811835b8d545443bd9ea87c997 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 12 Oct 2017 11:28:32 +0300 Subject: compact tags: reset PoS on new dictionary --- ext/bg/js/dictionary.js | 1 + 1 file changed, 1 insertion(+) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 14f90d29..d057ae83 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -124,6 +124,7 @@ function dictTermsCompressTags(definitions) { filterOutCategories.push('dictionary'); } else { lastDictionary = dictionary; + lastPos = ''; } if (lastPos === pos) { -- cgit v1.2.3 From 7ec28bea5422cda4304b660eccdb39d690f74fad Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 13 Oct 2017 02:40:20 +0300 Subject: change db format, rename tags to definitionTags --- ext/bg/js/database.js | 19 +++++++++---------- ext/bg/js/dictionary.js | 14 +++++++------- ext/bg/js/templates.js | 4 ++-- ext/bg/js/translator.js | 10 +++++----- tmpl/terms.html | 4 ++-- 5 files changed, 25 insertions(+), 26 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 1c2194df..8350e214 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -68,12 +68,11 @@ class Database { const results = []; await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { if (titles.includes(row.dictionary)) { - const tags = row.tags.split('\t'); results.push({ expression: row.expression, reading: row.reading, - tags: dictFieldSplit(tags[0]), - termTags: tags.length > 1 ? dictFieldSplit(tags[1]) : [], + definitionTags: dictFieldSplit(row.definitionTags), + termTags: dictFieldSplit(row.termTags || ''), rules: dictFieldSplit(row.rules), glossary: row.glossary, score: row.score, @@ -95,12 +94,11 @@ class Database { const results = []; await this.db.terms.where('sequence').equals(sequence).each(row => { if (row.dictionary === mainDictionary) { - const tags = row.tags.split('\t'); results.push({ expression: row.expression, reading: row.reading, - tags: dictFieldSplit(tags[0]), - termTags: tags.length > 1 ? dictFieldSplit(tags[1]) : [], + definitionTags: dictFieldSplit(row.definitionTags), + termTags: dictFieldSplit(row.termTags || ''), rules: dictFieldSplit(row.rules), glossary: row.glossary, score: row.score, @@ -229,11 +227,11 @@ class Database { const rows = []; if (summary.version === 1) { - for (const [expression, reading, tags, rules, score, ...glossary] of entries) { + for (const [expression, reading, definitionTags, rules, score, ...glossary] of entries) { rows.push({ expression, reading, - tags, + definitionTags, rules, score, glossary, @@ -241,15 +239,16 @@ class Database { }); } } else { - for (const [expression, reading, tags, rules, score, glossary, sequence] of entries) { + for (const [expression, reading, definitionTags, rules, score, glossary, sequence, termTags] of entries) { rows.push({ expression, reading, - tags, + definitionTags, rules, score, glossary, sequence, + termTags, dictionary: summary.title }); } diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index d057ae83..816e96c4 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -115,8 +115,8 @@ function dictTermsCompressTags(definitions) { let lastPos = ''; for (const definition of definitions) { - const dictionary = JSON.stringify(definition.tags.filter(tag => tag.category === 'dictionary').map(tag => tag.name).sort()); - const pos = JSON.stringify(definition.tags.filter(tag => tag.category === 'pos').map(tag => tag.name).sort()); + const dictionary = JSON.stringify(definition.definitionTags.filter(tag => tag.category === 'dictionary').map(tag => tag.name).sort()); + const pos = JSON.stringify(definition.definitionTags.filter(tag => tag.category === 'pos').map(tag => tag.name).sort()); const filterOutCategories = []; @@ -133,7 +133,7 @@ function dictTermsCompressTags(definitions) { lastPos = pos; } - definition.tags = definition.tags.filter(tag => !filterOutCategories.includes(tag.category)); + definition.definitionTags = definition.definitionTags.filter(tag => !filterOutCategories.includes(tag.category)); } } @@ -229,7 +229,7 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { definitionsByGloss[gloss] = { expression: new Set(), reading: new Set(), - tags: new Set(), + definitionTags: new Set(), glossary: definition.glossary, source: result.source, reasons: [], @@ -253,11 +253,11 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { result.expressions.get(definition.expression).set(definition.reading, new Set()); } - for (const tag of definition.tags) { + for (const tag of definition.definitionTags) { if (typeof tag === 'string') { - definitionsByGloss[gloss].tags.add(tag); + definitionsByGloss[gloss].definitionTags.add(tag); } else if (tag.category && tag.category !== 'dictionary') { - definitionsByGloss[gloss].tags.add(tag.name); + definitionsByGloss[gloss].definitionTags.add(tag.name); } } diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index e8bd43e9..b7b4721e 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -206,14 +206,14 @@ templates['model.html'] = template({"1":function(container,depth0,helpers,partia templates['terms.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1, alias1=depth0 != null ? depth0 : (container.nullContext || {}); - return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.tags : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + return ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.definitionTags : depth0),{"name":"if","hash":{},"fn":container.program(2, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.only : depth0),{"name":"if","hash":{},"fn":container.program(5, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,((stack1 = (depth0 != null ? depth0.glossary : depth0)) != null ? stack1["1"] : stack1),{"name":"if","hash":{},"fn":container.program(9, data, 0),"inverse":container.program(13, data, 0),"data":data})) != null ? stack1 : ""); },"2":function(container,depth0,helpers,partials,data) { var stack1; return "
      \n" - + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.tags : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + + ((stack1 = helpers.each.call(depth0 != null ? depth0 : (container.nullContext || {}),(depth0 != null ? depth0.definitionTags : depth0),{"name":"each","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + "
      \n"; },"3":function(container,depth0,helpers,partials,data) { var helper, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index e307efc0..c102f6a8 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -79,9 +79,9 @@ class Translator { for (const gloss in definitionsByGloss) { const definition = definitionsByGloss[gloss]; - const tags = await this.expandTags(definition.tags, definition.dictionary); + const tags = await this.expandTags(definition.definitionTags, definition.dictionary); tags.push(dictTagBuildSource(definition.dictionary)); - definition.tags = dictTagsSort(tags); + definition.definitionTags = dictTagsSort(tags); result.definitions.push(definition); } @@ -166,8 +166,8 @@ class Translator { let definitions = []; for (const deinflection of deinflections) { for (const definition of deinflection.definitions) { - const tags = await this.expandTags(definition.tags, definition.dictionary); - tags.push(dictTagBuildSource(definition.dictionary)); + const definitionTags = await this.expandTags(definition.definitionTags, definition.dictionary); + definitionTags.push(dictTagBuildSource(definition.dictionary)); const termTags = await this.expandTags(definition.termTags, definition.dictionary); definitions.push({ @@ -179,7 +179,7 @@ class Translator { expression: definition.expression, reading: definition.reading, glossary: definition.glossary, - tags: dictTagsSort(tags), + definitionTags: dictTagsSort(definitionTags), termTags: dictTagsSort(termTags), sequence: definition.sequence }); diff --git a/tmpl/terms.html b/tmpl/terms.html index e037c544..f35caed4 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -1,7 +1,7 @@ {{#*inline "definition"}} -{{#if tags}} +{{#if definitionTags}}
      - {{#each tags}} + {{#each definitionTags}} {{name}} {{/each}}
      -- cgit v1.2.3 From 4203fda906a1b4a104187e54d256e5952d8ae55c Mon Sep 17 00:00:00 2001 From: siikamiika Date: Fri, 13 Oct 2017 03:14:06 +0300 Subject: rename pos to partOfSpeech --- ext/bg/js/dictionary.js | 12 ++++++------ ext/mixed/css/display.css | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 816e96c4..843d05f1 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -112,11 +112,11 @@ function dictTermsUndupe(definitions) { function dictTermsCompressTags(definitions) { let lastDictionary = ''; - let lastPos = ''; + let lastPartOfSpeech = ''; for (const definition of definitions) { const dictionary = JSON.stringify(definition.definitionTags.filter(tag => tag.category === 'dictionary').map(tag => tag.name).sort()); - const pos = JSON.stringify(definition.definitionTags.filter(tag => tag.category === 'pos').map(tag => tag.name).sort()); + const partOfSpeech = JSON.stringify(definition.definitionTags.filter(tag => tag.category === 'partOfSpeech').map(tag => tag.name).sort()); const filterOutCategories = []; @@ -124,13 +124,13 @@ function dictTermsCompressTags(definitions) { filterOutCategories.push('dictionary'); } else { lastDictionary = dictionary; - lastPos = ''; + lastPartOfSpeech = ''; } - if (lastPos === pos) { - filterOutCategories.push('pos'); + if (lastPartOfSpeech === partOfSpeech) { + filterOutCategories.push('partOfSpeech'); } else { - lastPos = pos; + lastPartOfSpeech = partOfSpeech; } definition.definitionTags = definition.definitionTags.filter(tag => !filterOutCategories.includes(tag.category)); diff --git a/ext/mixed/css/display.css b/ext/mixed/css/display.css index 65cfd7ac..670930ae 100644 --- a/ext/mixed/css/display.css +++ b/ext/mixed/css/display.css @@ -92,7 +92,7 @@ hr { background-color: #5cb85c; } -.tag-pos { +.tag-partOfSpeech { background-color: #565656; } -- cgit v1.2.3 From b104dfd524c517a1f910503a8bad564121044e27 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 17 Oct 2017 08:29:31 +0300 Subject: merged mode: fix OBOE (sequence 0) --- ext/bg/js/dictionary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 843d05f1..6df77d73 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -175,7 +175,7 @@ function dictTermsGroup(definitions, dictionaries) { function dictTermsMergeBySequence(definitions, mainDictionary) { const definitionsBySequence = {'-1': []}; for (const definition of definitions) { - if (mainDictionary === definition.dictionary && definition.sequence > 0) { + if (mainDictionary === definition.dictionary && definition.sequence >= 0) { if (!definitionsBySequence[definition.sequence]) { definitionsBySequence[definition.sequence] = { reasons: definition.reasons, -- cgit v1.2.3 From 81f0a616519d1cea66ee54ec47c5da868a208ef6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 17 Oct 2017 11:16:38 +0300 Subject: merged mode: Anki support --- ext/bg/js/dictionary.js | 3 +- ext/bg/js/options.js | 81 ++++++++++++++++++++++++++++++++++++++++--------- ext/bg/js/translator.js | 5 +-- 3 files changed, 72 insertions(+), 17 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 6df77d73..97716e65 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -368,7 +368,8 @@ async function dictFieldFormat(field, definition, mode, options) { merge: options.general.resultOutputMode === 'merge', modeTermKanji: mode === 'term-kanji', modeTermKana: mode === 'term-kana', - modeKanji: mode === 'kanji' + modeKanji: mode === 'kanji', + compactGlossaries: options.general.compactGlossaries }; const html = await apiTemplateRender(options.anki.fieldTemplates, data, true); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 86c4e27c..34cb1240 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -21,10 +21,15 @@ function optionsFieldTemplates() { return ` {{#*inline "glossary-single"}} {{~#unless brief~}} - {{~#if tags~}}({{#each tags}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}) {{/if~}} + {{~#if definitionTags~}}({{#each definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each}}) {{/if~}} + {{~#if only~}}({{#each only}}{{{.}}}{{#unless @last}}, {{/unless}}{{/each}} only) {{/if~}} {{~/unless~}} {{~#if glossary.[1]~}} -
        {{#each glossary}}
      • {{#multiLine}}{{.}}{{/multiLine}}
      • {{/each}}
      + {{~#if compactGlossaries~}} + {{#each glossary}}{{#multiLine}}{{.}}{{/multiLine}}{{#unless @last}} | {{/unless}}{{/each}} + {{~else~}} +
        {{#each glossary}}
      • {{#multiLine}}{{.}}{{/multiLine}}
      • {{/each}}
      + {{~/if~}} {{~else~}} {{~#multiLine}}{{glossary.[0]}}{{/multiLine~}} {{~/if~}} @@ -41,23 +46,56 @@ function optionsFieldTemplates() { {{/inline}} {{#*inline "expression"}} - {{~#if modeTermKana~}} - {{~#if definition.reading~}} - {{definition.reading}} + {{~#if merge~}} + {{~#if modeTermKana~}} + {{~#each definition.reading~}} + {{{.}}} + {{~#unless @last}}, {{/unless~}} + {{~else~}} + {{~#each definition.expression~}} + {{{.}}} + {{~#unless @last}}, {{/unless~}} + {{~/each~}} + {{~/each~}} {{~else~}} - {{definition.expression}} + {{~#each definition.expression~}} + {{{.}}} + {{~#unless @last}}, {{/unless~}} + {{~/each~}} {{~/if~}} {{~else~}} - {{definition.expression}} + {{~#if modeTermKana~}} + {{~#if definition.reading~}} + {{definition.reading}} + {{~else~}} + {{definition.expression}} + {{~/if~}} + {{~else~}} + {{definition.expression}} + {{~/if~}} {{~/if~}} {{/inline}} {{#*inline "furigana"}} - {{#furigana}}{{{definition}}}{{/furigana}} + {{~#if merge~}} + {{~#each definition.expressions~}} + {{~#furigana}}{{{.}}}{{/furigana~}} + {{~#unless @last}}, {{/unless~}} + {{~/each~}} + {{~else~}} + {{#furigana}}{{{definition}}}{{/furigana}} + {{~/if~}} {{/inline}} {{#*inline "furigana-plain"}} - {{#furiganaPlain}}{{{definition}}}{{/furiganaPlain}} + {{~#if merge~}} + {{~#each definition.expressions~}} + {{~#furiganaPlain}}{{{.}}}{{/furiganaPlain~}} + {{~#unless @last}}, {{/unless~}} + {{~/each~}} + {{~else~}} + {{#furiganaPlain}}{{{definition}}}{{/furiganaPlain}} + {{~/if~}} {{/inline}} {{#*inline "glossary"}} @@ -71,12 +109,18 @@ function optionsFieldTemplates() { {{~else~}} {{~#if group~}} {{~#if definition.definitions.[1]~}} -
        {{#each definition.definitions}}
      1. {{> glossary-single brief=../brief}}
      2. {{/each}}
      +
        {{#each definition.definitions}}
      1. {{> glossary-single brief=../brief compactGlossaries=../compactGlossaries}}
      2. {{/each}}
      {{~else~}} - {{~> glossary-single definition.definitions.[0] brief=brief~}} + {{~> glossary-single definition.definitions.[0] brief=brief compactGlossaries=compactGlossaries~}} + {{~/if~}} + {{~else if merge~}} + {{~#if definition.definitions.[1]~}} +
        {{#each definition.definitions}}
      1. {{> glossary-single brief=../brief compactGlossaries=../compactGlossaries}}
      2. {{/each}}
      + {{~else~}} + {{~> glossary-single definition.definitions.[0] brief=brief compactGlossaries=compactGlossaries~}} {{~/if~}} {{~else~}} - {{~> glossary-single definition brief=brief~}} + {{~> glossary-single definition brief=brief compactGlossaries=compactGlossaries~}} {{~/if~}} {{~/if~}} @@ -95,7 +139,16 @@ function optionsFieldTemplates() { {{/inline}} {{#*inline "reading"}} - {{~#unless modeTermKana}}{{definition.reading}}{{/unless~}} + {{~#unless modeTermKana~}} + {{~#if merge~}} + {{~#each definition.reading~}} + {{{.}}} + {{~#unless @last}}, {{/unless~}} + {{~/each~}} + {{~else~}} + {{~definition.reading~}} + {{~/if~}} + {{~/unless~}} {{/inline}} {{#*inline "sentence"}} @@ -115,7 +168,7 @@ function optionsFieldTemplates() { {{/inline}} {{#*inline "tags"}} - {{~#each definition.tags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}} + {{~#each definition.definitionTags}}{{name}}{{#unless @last}}, {{/unless}}{{/each~}} {{/inline}} {{#*inline "url"}} diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index bf347343..70b01ce3 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -128,8 +128,9 @@ class Translator { result.expressions = expressions; - // result.expression = Array.from(result.expression).join(', '); - // result.reading = Array.from(result.reading).join(', '); + result.expression = Array.from(result.expression); + result.reading = Array.from(result.reading); + definitionsMerged.push(result); } -- cgit v1.2.3 From f54f909701453bac563c9cb5ec75fde23e087e8e Mon Sep 17 00:00:00 2001 From: siikamiika Date: Thu, 19 Oct 2017 18:35:42 +0300 Subject: merged mode: rewrite term frequency (use score) --- ext/bg/js/dictionary.js | 29 ++++++++++++++++------------- ext/bg/js/templates.js | 2 +- ext/bg/js/translator.js | 8 ++++---- tmpl/terms.html | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 97716e65..ee056273 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -318,21 +318,24 @@ function dictTagsSort(tags) { }); } -function dictJmdictTermTagsRare(tags) { - const rareTags = [ - 'ik', - 'iK', - 'ok', - 'oK', - 'io', - 'oik' - ]; +function dictTermTagScore(tags) { + let score = 0; + + const tagScores = { + 'ik': -5, + 'iK': -5, + 'ok': -5, + 'oK': -5, + 'io': -5, + 'oik': -5, + 'P': 10 + }; + for (const tag of tags) { - if (rareTags.includes(tag)) { - return true; - } + score += tagScores[tag] || 0; } - return false; + + return score; } function dictFieldSplit(field) { diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index d2aa1ee0..e5640394 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -313,7 +313,7 @@ templates['terms.html'] = template({"1":function(container,depth0,helpers,partia },"29":function(container,depth0,helpers,partials,data,blockParams,depths) { var stack1, helper, options, alias1=depth0 != null ? depth0 : (container.nullContext || {}), alias2=helpers.helperMissing, alias3="function", buffer = "
      \n "; stack1 = ((helper = (helper = helpers.kanjiLinks || (depth0 != null ? depth0.kanjiLinks : depth0)) != null ? helper : alias2),(options={"name":"kanjiLinks","hash":{},"fn":container.program(30, data, 0, blockParams, depths),"inverse":container.noop,"data":data}),(typeof helper === alias3 ? helper.call(alias1,options) : helper)); if (!helpers.kanjiLinks) { stack1 = helpers.blockHelperMissing.call(depth0,stack1,options)} diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 4394e172..22c5022f 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -113,15 +113,15 @@ class Translator { expression: expression, reading: reading, termTags: dictTagsSort(tags), - jmdictTermFrequency: (tags => { - if (tags.has('P')) { + termFrequency: (score => { + if (score > 0) { return 'popular'; - } else if (dictJmdictTermTagsRare(tags)) { + } else if (score < 0) { return 'rare'; } else { return 'normal'; } - })(result.expressions.get(expression).get(reading)) + })(dictTermTagScore(result.expressions.get(expression).get(reading))) }); } } diff --git a/tmpl/terms.html b/tmpl/terms.html index c83a4b15..b5b1325d 100644 --- a/tmpl/terms.html +++ b/tmpl/terms.html @@ -45,7 +45,7 @@ {{#if merged}} {{#each expressions}}
      - {{#kanjiLinks}}{{#furigana}}{{{.}}}{{/furigana}}{{/kanjiLinks}}
      {{~#if ../playback~}} -- cgit v1.2.3 From e034ca3ad4f88282e8dfec27df5cb7c55e0a47c6 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 24 Oct 2017 12:38:05 +0300 Subject: add tag meta score --- ext/bg/js/database.js | 5 +++-- ext/bg/js/dictionary.js | 21 +-------------------- ext/bg/js/translator.js | 2 +- 3 files changed, 5 insertions(+), 23 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 15291968..469b1311 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -359,12 +359,13 @@ class Database { } const rows = []; - for (const [name, category, order, notes] of entries) { + for (const [name, category, order, notes, score] of entries) { const row = dictTagSanitize({ name, category, order, notes, + score, dictionary: summary.title }); @@ -449,7 +450,7 @@ class Database { const bank = []; for (const name in index.tagMeta) { const tag = index.tagMeta[name]; - bank.push([name, tag.category, tag.order, tag.notes]); + bank.push([name, tag.category, tag.order, tag.notes, tag.score]); } tagDataLoaded(summary, bank, ++bankTotalCount, bankLoadedCount++); diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index ee056273..50932735 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -293,6 +293,7 @@ function dictTagSanitize(tag) { tag.category = tag.category || 'default'; tag.notes = tag.notes || ''; tag.order = tag.order || 0; + tag.score = tag.score || 0; return tag; } @@ -318,26 +319,6 @@ function dictTagsSort(tags) { }); } -function dictTermTagScore(tags) { - let score = 0; - - const tagScores = { - 'ik': -5, - 'iK': -5, - 'ok': -5, - 'oK': -5, - 'io': -5, - 'oik': -5, - 'P': 10 - }; - - for (const tag of tags) { - score += tagScores[tag] || 0; - } - - return score; -} - function dictFieldSplit(field) { return field.length === 0 ? [] : field.split(' '); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 22c5022f..5fcdea76 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -121,7 +121,7 @@ class Translator { } else { return 'normal'; } - })(dictTermTagScore(result.expressions.get(expression).get(reading))) + })(tags.map(tag => tag.score).reduce((p, v) => p + v, 0)) }); } } -- cgit v1.2.3 From 27a56dc236d8f05508160c28d9115baad26453d2 Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 24 Oct 2017 18:31:18 +0300 Subject: expand tags before dictTermsMergeByGloss --- ext/bg/js/dictionary.js | 8 +++----- ext/bg/js/translator.js | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'ext/bg/js/dictionary.js') diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 50932735..fea5f3e5 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -229,7 +229,7 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { definitionsByGloss[gloss] = { expression: new Set(), reading: new Set(), - definitionTags: new Set(), + definitionTags: [], glossary: definition.glossary, source: result.source, reasons: [], @@ -254,10 +254,8 @@ function dictTermsMergeByGloss(result, definitions, appendTo, mergedIndices) { } for (const tag of definition.definitionTags) { - if (typeof tag === 'string') { - definitionsByGloss[gloss].definitionTags.add(tag); - } else if (tag.category && tag.category !== 'dictionary') { - definitionsByGloss[gloss].definitionTags.add(tag.name); + if (!definitionsByGloss[gloss].definitionTags.find(existingTag => existingTag.name === tag.name)) { + definitionsByGloss[gloss].definitionTags.push(tag); } } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index e6790fbf..238af94d 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -73,6 +73,13 @@ class Translator { const result = definitionsBySequence[sequence]; const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.general.mainDictionary); + + for (const definition of rawDefinitionsBySequence) { + const tags = await this.expandTags(definition.definitionTags, definition.dictionary); + tags.push(dictTagBuildSource(definition.dictionary)); + definition.definitionTags = tags; + } + const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); const secondarySearchResults = []; @@ -84,6 +91,9 @@ class Translator { for (const reading of result.expressions.get(expression).keys()) { for (const definition of await this.database.findTermsExact(expression, reading, secondarySearchTitles)) { + const tags = await this.expandTags(definition.definitionTags, definition.dictionary); + tags.push(dictTagBuildSource(definition.dictionary)); + definition.definitionTags = tags; secondarySearchResults.push(definition); } } @@ -94,11 +104,7 @@ class Translator { for (const gloss in definitionsByGloss) { const definition = definitionsByGloss[gloss]; - - const tags = await this.expandTags(definition.definitionTags, definition.dictionary); - tags.push(dictTagBuildSource(definition.dictionary)); - definition.definitionTags = dictTagsSort(tags); - + dictTagsSort(definition.definitionTags); result.definitions.push(definition); } @@ -289,9 +295,6 @@ class Translator { async expandTags(names, title) { const tags = []; for (const name of names) { - if (typeof name !== 'string') { - continue; - } const base = name.split(':')[0]; const meta = await this.database.findTagForTitle(base, title); -- cgit v1.2.3