merged mode: add secondary searches

This commit is contained in:
siikamiika 2017-10-15 05:19:16 +03:00
parent 4203fda906
commit 03f7ca23e1
5 changed files with 59 additions and 8 deletions

View File

@ -86,6 +86,32 @@ class Database {
return results; return results;
} }
async findTermsExact(term, reading, titles) {
if (!this.db) {
throw 'Database not initialized';
}
const results = [];
await this.db.terms.where('expression').equals(term).each(row => {
if (row.reading === reading && titles.includes(row.dictionary)) {
results.push({
expression: row.expression,
reading: row.reading,
definitionTags: dictFieldSplit(row.definitionTags),
termTags: dictFieldSplit(row.termTags || ''),
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;
}
async findTermsBySequence(sequence, mainDictionary) { async findTermsBySequence(sequence, mainDictionary) {
if (!this.db) { if (!this.db) {
throw 'Database not initialized'; throw 'Database not initialized';

View File

@ -62,7 +62,8 @@ async function formRead() {
const priority = parseInt(dictionary.find('.dict-priority').val(), 10); const priority = parseInt(dictionary.find('.dict-priority').val(), 10);
const enabled = dictionary.find('.dict-enabled').prop('checked'); const enabled = dictionary.find('.dict-enabled').prop('checked');
const main = dictionary.find('.dict-main').prop('checked'); const main = dictionary.find('.dict-main').prop('checked');
optionsNew.dictionaries[title] = {priority, enabled, main}; const allowSecondarySearches = dictionary.find('.dict-allow-secondary-searches').prop('checked');
optionsNew.dictionaries[title] = {priority, enabled, main, allowSecondarySearches};
}); });
return {optionsNew, optionsOld}; return {optionsNew, optionsOld};
@ -262,14 +263,15 @@ async function dictionaryGroupsPopulate(options) {
} }
for (const dictRow of dictRowsSort(dictRows, options)) { for (const dictRow of dictRowsSort(dictRows, options)) {
const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, main: false}; const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, main: false, allowSecondarySearches: false};
const dictHtml = await apiTemplateRender('dictionary.html', { const dictHtml = await apiTemplateRender('dictionary.html', {
title: dictRow.title, title: dictRow.title,
version: dictRow.version, version: dictRow.version,
revision: dictRow.revision, revision: dictRow.revision,
priority: dictOptions.priority, priority: dictOptions.priority,
enabled: dictOptions.enabled, enabled: dictOptions.enabled,
main: dictOptions.main main: dictOptions.main,
allowSecondarySearches: dictOptions.allowSecondarySearches
}); });
dictGroups.append($(dictHtml)); dictGroups.append($(dictHtml));
@ -277,7 +279,7 @@ async function dictionaryGroupsPopulate(options) {
formUpdateVisibility(options); formUpdateVisibility(options);
$('.dict-enabled, .dict-priority').change(e => { $('.dict-enabled, .dict-priority, .dict-allow-secondary-searches').change(e => {
dictionaryGroupsSort(); dictionaryGroupsSort();
onFormOptionsChanged(e); onFormOptionsChanged(e);
}); });
@ -329,7 +331,7 @@ async function onDictionaryImport(e) {
const options = await optionsLoad(); const options = await optionsLoad();
const summary = await utilDatabaseImport(e.target.files[0], updateProgress); const summary = await utilDatabaseImport(e.target.files[0], updateProgress);
options.dictionaries[summary.title] = {enabled: true, priority: 0, main: false}; options.dictionaries[summary.title] = {enabled: true, priority: 0, main: false, allowSecondarySearches: false};
await optionsSave(options); await optionsSave(options);
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);

View File

@ -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))) + 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)))
+ "</small></h4>\n\n <div class=\"checkbox\">\n <label><input type=\"checkbox\" class=\"dict-enabled\" " + "</small></h4>\n\n <div class=\"checkbox\">\n <label><input type=\"checkbox\" class=\"dict-enabled\" "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enabled : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enabled : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Enable search</label>\n <label><input type=\"radio\" class=\"dict-main\" " + "> Enable search</label>\n </div>\n <div class=\"checkbox options-advanced\">\n <label><input type=\"checkbox\" class=\"dict-allow-secondary-searches\" "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.allowSecondarySearches : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Allow secondary searches</label>\n </div>\n <div class=\"radio\">\n <label><input type=\"radio\" class=\"dict-main\" "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.main : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.main : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Set as main dictionary</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-" + "> Set as main dictionary</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-"
+ alias4(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"title","hash":{},"data":data}) : helper))) + alias4(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"title","hash":{},"data":data}) : helper)))

View File

@ -58,6 +58,7 @@ class Translator {
async findTermsMerged(text, dictionaries, alphanumeric) { async findTermsMerged(text, dictionaries, alphanumeric) {
const options = await apiOptionsGet(); const options = await apiOptionsGet();
const mainDictionary = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].main).concat([''])[0]; const mainDictionary = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].main).concat([''])[0];
const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches);
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
@ -74,7 +75,23 @@ class Translator {
const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), mainDictionary); const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), mainDictionary);
const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence);
dictTermsMergeByGloss(result, definitionsBySequence['-1'], definitionsByGloss, mergedByTermIndices);
const secondarySearchResults = [];
if (secondarySearchTitles.length) {
for (const expression of result.expressions.keys()) {
if (expression === text) {
continue;
}
for (const reading of result.expressions.get(expression).keys()) {
for (const definition of await this.database.findTermsExact(expression, reading, secondarySearchTitles)) {
secondarySearchResults.push(definition);
}
}
}
}
dictTermsMergeByGloss(result, definitionsBySequence['-1'].concat(secondarySearchResults), definitionsByGloss, mergedByTermIndices);
for (const gloss in definitionsByGloss) { for (const gloss in definitionsByGloss) {
const definition = definitionsByGloss[gloss]; const definition = definitionsByGloss[gloss];
@ -88,7 +105,6 @@ class Translator {
dictTermsSort(result.definitions, dictionaries); 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 = []; const expressions = [];
for (const expression of result.expressions.keys()) { for (const expression of result.expressions.keys()) {
for (const reading of result.expressions.get(expression).keys()) { for (const reading of result.expressions.get(expression).keys()) {

View File

@ -3,6 +3,11 @@
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" class="dict-enabled" {{#if enabled}}checked{{/if}}> Enable search</label> <label><input type="checkbox" class="dict-enabled" {{#if enabled}}checked{{/if}}> Enable search</label>
</div>
<div class="checkbox options-advanced">
<label><input type="checkbox" class="dict-allow-secondary-searches" {{#if allowSecondarySearches}}checked{{/if}}> Allow secondary searches</label>
</div>
<div class="radio">
<label><input type="radio" class="dict-main" {{#if main}}checked{{/if}}> Set as main dictionary</label> <label><input type="radio" class="dict-main" {{#if main}}checked{{/if}}> Set as main dictionary</label>
</div> </div>
<div class="form-group options-advanced"> <div class="form-group options-advanced">