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