diff options
-rw-r--r-- | api/yomikun.ts | 6 | ||||
-rw-r--r-- | examples/furigana-html.ts | 4 | ||||
-rw-r--r-- | examples/reading-correction-break.ts | 6 | ||||
-rw-r--r-- | examples/sentence-word-lookup.ts | 18 | ||||
-rw-r--r-- | examples/user-ignore.ts | 36 | ||||
-rw-r--r-- | test/base.ts | 8 | ||||
-rw-r--r-- | test/deinflection/test.ts | 4 | ||||
-rw-r--r-- | test/reading/test.ts | 9 |
8 files changed, 66 insertions, 25 deletions
diff --git a/api/yomikun.ts b/api/yomikun.ts index 60d49ba..a7f214e 100644 --- a/api/yomikun.ts +++ b/api/yomikun.ts @@ -5,9 +5,15 @@ import Sentence from "./sentence.ts"; export default class Yomikun { protected core: Core; + public ready: Promise<void>; constructor(core?: Core) { this.core = core ?? new RemoteCoreClient(); + + this.ready = new Promise(async resolve => { + await this.core.ready; + resolve(); + }) } async sentence(input: string): Promise<Sentence> { diff --git a/examples/furigana-html.ts b/examples/furigana-html.ts index f0ff067..fa599ed 100644 --- a/examples/furigana-html.ts +++ b/examples/furigana-html.ts @@ -4,8 +4,8 @@ import DirectCoreClient from "../core/direct/client.ts"; // Create a direct (local) API instance var api = new Yomikun(new DirectCoreClient()); -// Excplicitly wait until everything is ready -// await api.ready; +// Explicitly wait until everything is ready (not required) +await api.ready; // This sentence does not contain all information until it is explicitly // fetched by the user. Each subclass instantiated from an API instance keeps a diff --git a/examples/reading-correction-break.ts b/examples/reading-correction-break.ts index 3f0359b..6761165 100644 --- a/examples/reading-correction-break.ts +++ b/examples/reading-correction-break.ts @@ -1,11 +1,13 @@ import Yomikun from "../api/yomikun.ts"; import DirectCoreClient from "../core/direct/client.ts"; +// WIP + // Create a direct (local) API instance var api = new Yomikun(new DirectCoreClient()); -// Excplicitly wait until everything is ready -// await api.ready; +// Explicitly wait until everything is ready (not required) +await api.ready; // index sentence (generates wrong readings) var sentence = await api.sentence("日本に来て一番驚いたことは自動販売機の多さだ。"); diff --git a/examples/sentence-word-lookup.ts b/examples/sentence-word-lookup.ts index d60ffbf..9ff9bdf 100644 --- a/examples/sentence-word-lookup.ts +++ b/examples/sentence-word-lookup.ts @@ -1,12 +1,11 @@ import Yomikun from "../api/yomikun.ts"; import DirectCoreClient from "../core/direct/client.ts"; -// import "../util/string.ts"; // Create a direct (local) API instance var api = new Yomikun(new DirectCoreClient()); -// Excplicitly wait until everything is ready -// await api.ready; +// Explicitly wait until everything is ready (not required) +await api.ready; // This sentence does not contain all information until it is explicitly // fetched by the user. Each subclass instantiated from an API instance keeps a @@ -14,15 +13,14 @@ var api = new Yomikun(new DirectCoreClient()); var sentence = await api.sentence("この紅茶は甘すぎる"); // Pick the word 紅茶 from the sentence in some different ways: -var word = sentence.words.find(w => w.writing == "紅茶"); // filter terms by writing (matches first only) -// var word = sentence.first("紅茶"); // reference substring (matches first only) -// var word = sentence.words[1]; // reference word index (depends on correct deconjugations/parsing) -console.log(word); +var word = sentence.words.find(w => w.written("紅茶"))!; // filter terms by writing (matches first only) +var word = sentence.first("紅茶")!; // reference substring (matches first only) +var word = sentence.words[1]; // reference word index (depends on correct deconjugations/parsing) // Fetch definitions for word -var glossary = await word?.glossary(); +var glossary = await word.glossary(); // Show some definitions -console.log(glossary?.dict("jmdict_eng")[0]); // print first definition from JMdict -// console.log(glossary.all()); // print all definitions +console.log(glossary.dict("jmdict_eng")[0]); // print first definition from JMdict +console.log(glossary.all()); // print all definitions diff --git a/examples/user-ignore.ts b/examples/user-ignore.ts new file mode 100644 index 0000000..d0abbe3 --- /dev/null +++ b/examples/user-ignore.ts @@ -0,0 +1,36 @@ +import Yomikun from "../api/yomikun.ts"; +import DirectCoreClient from "../core/direct/client.ts"; + +// Create a direct (local) API instance +var api = new Yomikun(new DirectCoreClient()); + +// Explicitly wait until everything is ready (not required) +await api.ready; + +// Substitute user to gert (from default login root) +api.su("gert"); + +// Lookup sentence +var sentence = await api.sentence("浮上したハイラル城の下にてゼルダ様達の捜索を行うこととなった"); + +// Freeze disables automatic updating of words after database mutations. It's +// used here because there are multiple sequential updates to the database. +sentence.freeze(); + +// Ignore some expressions in JMdict (ignore applies to current user = gert in +// this case) +sentence.words.find(w => w.written("達") && w.read("だち"))?.ignore(); // wrong reading for this case +sentence.at("の下に").ignore(); // expression のもとに +sentence.at("下に").ignore(); // expression したに + +// TODO: 達(だち) should not have to be ignored, but scored lower following +// rendaku rules. <https://en.wikipedia.org/wiki/Rendaku> + +// Unfreeze allows updates again and implicitly calls .update() +sentence.unfreeze(); + +// Get new reading for sentence +var furigana = sentence.furigana(); + +console.log(furigana); + diff --git a/test/base.ts b/test/base.ts index 0390169..79c39ce 100644 --- a/test/base.ts +++ b/test/base.ts @@ -1,2 +1,10 @@ export { assertEquals } from "https://deno.land/std@0.193.0/testing/asserts.ts"; +import Yomikun from "../api/yomikun.ts"; +import DirectCoreClient from '../core/direct/client.ts'; + +export const core = new DirectCoreClient(); +export const api = new Yomikun(core); + +await api.ready; + diff --git a/test/deinflection/test.ts b/test/deinflection/test.ts index 3dc702b..5a123ba 100644 --- a/test/deinflection/test.ts +++ b/test/deinflection/test.ts @@ -1,8 +1,6 @@ import DirectCoreClient from '../../core/direct/client.ts'; import cases from "./cases.ts"; - -var core = new DirectCoreClient(); -await core.ready; +import { core } from '../base.ts'; cases.forEach(({ input, tags }) => { Deno.test(`deinflection - ${input}`, async () => { diff --git a/test/reading/test.ts b/test/reading/test.ts index 6a82b3f..a2524de 100644 --- a/test/reading/test.ts +++ b/test/reading/test.ts @@ -1,13 +1,6 @@ -import { assertEquals } from "../base.ts"; +import { api, assertEquals } from "../base.ts"; import cases from "./cases.ts"; -import Yomikun from "../../api/yomikun.ts"; -import DirectCoreClient from '../../core/direct/client.ts'; - -var core = new DirectCoreClient(); -await core.ready; -var api = new Yomikun(core); - cases.forEach(({input, output}) => { Deno.test(`Sentence reading - ${input}`, async () => { // TODO: use sentence reading and tags |