From ce9e0788317b25e5d297ed38d9fed0754a341288 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 1 Jul 2023 16:37:50 +0200 Subject: WIP sentence API --- api/sentence-word.ts | 3 ++- api/sentence.ts | 38 ++++++++++++++++++++++++++++++++++++-- api/word.ts | 1 + api/yomikun.ts | 5 ++++- 4 files changed, 43 insertions(+), 4 deletions(-) (limited to 'api') diff --git a/api/sentence-word.ts b/api/sentence-word.ts index a5bb9ca..6b2ef22 100644 --- a/api/sentence-word.ts +++ b/api/sentence-word.ts @@ -1,10 +1,11 @@ +import { ParseToken } from "../language/types.ts"; import Word from "./word.ts"; export default class SentenceWord extends Word { public indexStart: number = 0; public indexEnd: number = 0; - constructor() { + constructor(source: string | ParseToken) { super(); } } diff --git a/api/sentence.ts b/api/sentence.ts index 6f67844..276a6c5 100644 --- a/api/sentence.ts +++ b/api/sentence.ts @@ -1,16 +1,50 @@ +import { ParseResult } from "../language/types.ts"; import APIBase from "./base.ts"; import SentenceWord from "./sentence-word.ts"; import Word from "./word.ts"; -import Yomikun from "./yomikun.ts"; export default class Sentence extends APIBase { public words: Array = []; + protected query?: ParseResult; + protected original: string = ""; - constructor() { + public ready: Promise; + private _resolveReady: () => void = () => {}; + + constructor(input: string) { super(); + this.ready = new Promise(res => this._resolveReady = res); + this.fetch(input); } first(searchValue: RegExp | string): Word | undefined { return this.words[0]; } + + private async fetch(input: string) { + this.original = input; + this.query = await (await this.api)["core"].parseSentence(input); + await this.updateWords(); + this._resolveReady(); + } + + private async updateWords() { + this.words.clear(); + let token = 0; + let i = 0; + while (i < this.original.length) { + this.words.push(new SentenceWord(this.query!.tokens[token]).withParent(await this.api)); + + i += this.query!.tokens[token].source.length; + if (i == this.original.length) break; + token++; + + // continue if there are no unrecognized gaps between tokens + if (this.query!.tokens[token]?.start == i) continue; + var remainder = this.original.substring(i, this.query!.tokens[token]?.start); + + this.words.push(new SentenceWord(remainder).withParent(await this.api)); + i += remainder.length; + } + } } diff --git a/api/word.ts b/api/word.ts index 3e8fce6..63dce10 100644 --- a/api/word.ts +++ b/api/word.ts @@ -1,5 +1,6 @@ import Glossary from "./glossary.ts"; import APIBase from "./base.ts"; +import { ParseToken } from "../language/types.ts"; export default class Word extends APIBase { public writing = "TODO"; diff --git a/api/yomikun.ts b/api/yomikun.ts index 4971567..60d49ba 100644 --- a/api/yomikun.ts +++ b/api/yomikun.ts @@ -1,5 +1,6 @@ import Core from "../core/api.ts"; import RemoteCoreClient from "../core/http/client.ts"; +import { ParseResult } from "../language/types.ts"; import Sentence from "./sentence.ts"; export default class Yomikun { @@ -10,7 +11,9 @@ export default class Yomikun { } async sentence(input: string): Promise { - return new Sentence().withParent(this); + var sentence = new Sentence(input).withParent(this); + await sentence.ready; + return sentence; } } -- cgit v1.2.3