diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-07-12 00:31:12 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-07-12 00:31:12 +0200 |
commit | 413501fde6bac909f31ad399781626caa16c8d04 (patch) | |
tree | 7514b0f551a7c41f618d58a50fa56972e6b06170 /search | |
parent | e99ae80f7adc0f0e677381c3cc1549235d3877ab (diff) |
implement parser breaks
Diffstat (limited to 'search')
-rw-r--r-- | search/search.ts | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/search/search.ts b/search/search.ts index 0a50773..57bd0e2 100644 --- a/search/search.ts +++ b/search/search.ts @@ -4,15 +4,18 @@ import DB from "../db/db.ts"; import "../util/array.ts"; import "../util/set.ts"; import { DeepPartial } from "../util/types.ts"; +import { min } from "../util/number.ts"; /** @summary main Search class */ export default class Search { db: DB; ready: Promise<void>; - constructor() { - this.db = new DB(); + /** @constant guaranteed lookahead delimiters */ + private SCAN_DELIMITERS = [" ", " ", "。", "、", "「", "」"]; + constructor(db: DB) { + this.db = db; this.ready = new Promise<void>(async resolve => { await this.db.ready; resolve(); @@ -89,7 +92,11 @@ export default class Search { }; for (let start = 0; start < sentence.length; start++) { - var lookahead = props.lookahead; // TODO: stop at next delimiter (optimization) + var lookahead = min( + props.lookahead, + (props.breaks.filter(b => b > start)[0] - start) ?? props.lookahead, // lookahead as fallback instead of NaN + // sentence.first(this.SCAN_DELIMITERS, start), + ); var term = sentence.substring(start, start + lookahead); var results = (await this.terms(term)).map(term => { var word = term as SearchWord; |