Add support for prefix wildcards
This commit is contained in:
parent
7401408c39
commit
1659340898
@ -28,7 +28,7 @@ class Database {
|
||||
}
|
||||
|
||||
try {
|
||||
this.db = await Database.open('dict', 4, (db, transaction, oldVersion) => {
|
||||
this.db = await Database.open('dict', 5, (db, transaction, oldVersion) => {
|
||||
Database.upgrade(db, transaction, oldVersion, [
|
||||
{
|
||||
version: 2,
|
||||
@ -76,6 +76,15 @@ class Database {
|
||||
indices: ['dictionary', 'expression', 'reading', 'sequence']
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
version: 5,
|
||||
stores: {
|
||||
terms: {
|
||||
primaryKey: {keyPath: 'id', autoIncrement: true},
|
||||
indices: ['dictionary', 'expression', 'reading', 'sequence', 'expressionReverse', 'readingReverse']
|
||||
}
|
||||
}
|
||||
}
|
||||
]);
|
||||
});
|
||||
@ -143,14 +152,17 @@ class Database {
|
||||
}
|
||||
};
|
||||
|
||||
const useWildcard = !!wildcard;
|
||||
const prefixWildcard = wildcard === 'prefix';
|
||||
|
||||
const dbTransaction = this.db.transaction(['terms'], 'readonly');
|
||||
const dbTerms = dbTransaction.objectStore('terms');
|
||||
const dbIndex1 = dbTerms.index('expression');
|
||||
const dbIndex2 = dbTerms.index('reading');
|
||||
const dbIndex1 = dbTerms.index(prefixWildcard ? 'expressionReverse' : 'expression');
|
||||
const dbIndex2 = dbTerms.index(prefixWildcard ? 'readingReverse' : 'reading');
|
||||
|
||||
for (let i = 0; i < termList.length; ++i) {
|
||||
const term = termList[i];
|
||||
const query = wildcard ? IDBKeyRange.bound(term, `${term}\uffff`, false, false) : IDBKeyRange.only(term);
|
||||
const term = prefixWildcard ? stringReverse(termList[i]) : termList[i];
|
||||
const query = useWildcard ? IDBKeyRange.bound(term, `${term}\uffff`, false, false) : IDBKeyRange.only(term);
|
||||
promises.push(
|
||||
Database.getAll(dbIndex1, query, i, processRow),
|
||||
Database.getAll(dbIndex2, query, i, processRow)
|
||||
@ -377,7 +389,9 @@ class Database {
|
||||
rules,
|
||||
score,
|
||||
glossary,
|
||||
dictionary: summary.title
|
||||
dictionary: summary.title,
|
||||
expressionReverse: stringReverse(expression),
|
||||
readingReverse: stringReverse(reading)
|
||||
});
|
||||
}
|
||||
} else {
|
||||
@ -391,7 +405,9 @@ class Database {
|
||||
glossary,
|
||||
sequence,
|
||||
termTags,
|
||||
dictionary: summary.title
|
||||
dictionary: summary.title,
|
||||
expressionReverse: stringReverse(expression),
|
||||
readingReverse: stringReverse(reading)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -207,10 +207,14 @@ class DisplaySearch extends Display {
|
||||
async onSearchQueryUpdated(query, animate) {
|
||||
try {
|
||||
const details = {};
|
||||
const match = /[*\uff0a]+$/.exec(query);
|
||||
const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(query);
|
||||
if (match !== null) {
|
||||
details.wildcard = true;
|
||||
query = query.substring(0, query.length - match[0].length);
|
||||
if (match[1]) {
|
||||
details.wildcard = 'prefix';
|
||||
} else if (match[3]) {
|
||||
details.wildcard = 'suffix';
|
||||
}
|
||||
query = match[2];
|
||||
}
|
||||
|
||||
const valid = (query.length > 0);
|
||||
|
@ -230,7 +230,7 @@ class Translator {
|
||||
const titles = Object.keys(dictionaries);
|
||||
const deinflections = (
|
||||
details.wildcard ?
|
||||
await this.findTermWildcard(text, titles) :
|
||||
await this.findTermWildcard(text, titles, details.wildcard) :
|
||||
await this.findTermDeinflections(text, titles)
|
||||
);
|
||||
|
||||
@ -268,8 +268,8 @@ class Translator {
|
||||
return [definitions, length];
|
||||
}
|
||||
|
||||
async findTermWildcard(text, titles) {
|
||||
const definitions = await this.database.findTermsBulk([text], titles, true);
|
||||
async findTermWildcard(text, titles, wildcard) {
|
||||
const definitions = await this.database.findTermsBulk([text], titles, wildcard);
|
||||
if (definitions.length === 0) {
|
||||
return [];
|
||||
}
|
||||
@ -308,7 +308,7 @@ class Translator {
|
||||
deinflectionArray.push(deinflection);
|
||||
}
|
||||
|
||||
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, false);
|
||||
const definitions = await this.database.findTermsBulk(uniqueDeinflectionTerms, titles, null);
|
||||
|
||||
for (const definition of definitions) {
|
||||
const definitionRules = Deinflector.rulesToRuleFlags(definition.rules);
|
||||
|
@ -118,6 +118,10 @@ function toIterable(value) {
|
||||
throw new Error('Could not convert to iterable');
|
||||
}
|
||||
|
||||
function stringReverse(string) {
|
||||
return string.split('').reverse().join('').replace(/([\uDC00-\uDFFF])([\uD800-\uDBFF])/g, '$2$1');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Async utilities
|
||||
|
Loading…
Reference in New Issue
Block a user