aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2023-07-01 16:37:50 +0200
committerlonkaars <loek@pipeframe.xyz>2023-07-01 16:37:50 +0200
commitce9e0788317b25e5d297ed38d9fed0754a341288 (patch)
tree29563a39c73ded16cd93eb7b5c5664d1ece944ac /api
parent8ff39cbe6300ca479584fe7d85ff03a1f65bc9b0 (diff)
WIP sentence API
Diffstat (limited to 'api')
-rw-r--r--api/sentence-word.ts3
-rw-r--r--api/sentence.ts38
-rw-r--r--api/word.ts1
-rw-r--r--api/yomikun.ts5
4 files changed, 43 insertions, 4 deletions
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<SentenceWord> = [];
+ protected query?: ParseResult;
+ protected original: string = "";
- constructor() {
+ public ready: Promise<void>;
+ 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<Sentence> {
- return new Sentence().withParent(this);
+ var sentence = new Sentence(input).withParent(this);
+ await sentence.ready;
+ return sentence;
}
}