diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-07-13 16:39:01 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-07-13 16:39:01 +0200 |
commit | 28fb362147358819afeb0f0a5fa7a4ad136499ac (patch) | |
tree | 4c4224f503f66fcea1e25b464daf0f9659a09bab /api/word.ts | |
parent | 919cdce6bcf12f564901ea536dae4227d025b031 (diff) |
ignore terms
Diffstat (limited to 'api/word.ts')
-rw-r--r-- | api/word.ts | 76 |
1 files changed, 55 insertions, 21 deletions
diff --git a/api/word.ts b/api/word.ts index 2e07c98..b519789 100644 --- a/api/word.ts +++ b/api/word.ts @@ -6,33 +6,33 @@ import "../util/object.ts"; import { Tag, TagGroup, TokenTags } from "../search/tags.ts"; import { SearchWord } from "../search/types.ts"; import { recursiveValues } from "../util/object.ts"; +import Sentence from "./sentence.ts"; + +// TODO: better irregular reading handling (should also work for counter words / 入る) // irregular stems taken from <https://en.wikipedia.org/wiki/Japanese_irregular_verbs#suru_and_kuru> -function irregularSuruStem(tags: TokenTags): string { +function irregularSuru(tags: TokenTags, conjugation: string): string { for (let i = 0, tag = tags[i]; i < tags.length; i++, tag = tags[i]) { if (!recursiveValues(Tag.Inflection).includes(tag)) continue; if (recursiveValues(Tag.Inflection.Reason).includes(tag)) continue; if ([ Tag.Inflection.Polite.Masu, Tag.Inflection.Suffix.Te, - Tag.Inflection.Tense.Past, Tag.Inflection.Desirable.Itai, // part of Wikipedia's -ta form - Tag.Inflection.Negative, - Tag.Inflection.Desirable.Volitional, Tag.Inflection.Command, - ].includes(tag as any)) return "し"; + ].includes(tag as any)) return "し" + conjugation; if ([ Tag.Inflection.Passive, Tag.Inflection.Causative, - ].includes(tag as any)) return "さ"; + ].includes(tag as any)) return "さ" + conjugation; // wikipedia has できる as the potential form for する, but できる here // means it's already foobar'd break; } - return "す"; + return conjugation; } -function irregularKuruStem(tags: TokenTags): string { +function irregularKuru(tags: TokenTags, conjugation: string): string { for (let i = 0, tag = tags[i]; i < tags.length; i++, tag = tags[i]) { if (!recursiveValues(Tag.Inflection).includes(tag)) continue; if (recursiveValues(Tag.Inflection.Reason).includes(tag)) continue; @@ -41,7 +41,7 @@ function irregularKuruStem(tags: TokenTags): string { Tag.Inflection.Suffix.Te, Tag.Inflection.Tense.Past, Tag.Inflection.Desirable.Itai, // part of Wikipedia's -ta form - ].includes(tag as any)) return "き"; + ].includes(tag as any)) return "き" + conjugation; if ([ Tag.Inflection.Negative, Tag.Inflection.Desirable.Volitional, @@ -49,10 +49,10 @@ function irregularKuruStem(tags: TokenTags): string { Tag.Inflection.Causative, Tag.Inflection.Potential, Tag.Inflection.Command, - ].includes(tag as any)) return "こ"; + ].includes(tag as any)) return "こ" + conjugation; break; } - return "く"; + return "く" + conjugation; } export default class Word extends APIBase { @@ -60,11 +60,22 @@ export default class Word extends APIBase { protected base: Japanese; /** @prop word as written in parent sentence */ protected text: Japanese; - /** @prop this.furigana should output kanji with reading */ - protected outputKanji: boolean = true; /** @prop this word represents an unrecognized sentence part between recognized terms */ protected filler: boolean; + private _resolveParent: (sentence: Sentence) => void = _ => {}; + /** @prop parent sentence */ + protected parent = new Promise<Sentence>(res => this._resolveParent = res); + + /** @prop length of word in sentence */ + public length: number; + /** @prop (conjugated) writing of term (*may* contain kanji) */ + public writing: string; + /** @prop (conjugated) reading of term (kana-only) */ + public reading: string; + /** @prop dictionary id for term */ + public id: number = -1; + constructor(input: string | SearchWord) { super(); if (typeof input === "string") { @@ -72,7 +83,6 @@ export default class Word extends APIBase { input = input as string; this.base = new Japanese(input, input); this.text = this.base; - this.outputKanji = false; } else { this.filler = false; input = input as SearchWord; @@ -87,26 +97,50 @@ export default class Word extends APIBase { // special reading for irregular verbs var reading = input.reading; - if (input.writing == '来る') reading = irregularKuruStem(input.tags) + conjugation; - else if (input.writing == '為る') reading = irregularSuruStem(input.tags) + conjugation; + if (input.writing == '来る') reading = irregularKuru(input.tags, conjugation); + else if (input.writing == '為る') reading = irregularSuru(input.tags, conjugation); else reading = reading.replaceLast(base, conjugation); // generate conjugated version of verb with kanji - this.text = new Japanese(input.writing.replaceLast(base, conjugation), reading); + this.text = new Japanese(input.source, reading); } else { this.text = this.base; } - this.outputKanji = input.source.hasKanji(); // only output kanji if input also uses kanji + this.id = input.id; } + this.writing = this.text.writing; + this.length = this.text.writing.length; + this.reading = this.text.reading; } furigana(format: JapaneseFormatter) { - if (!this.outputKanji) return this.text.reading; - else return this.text.furigana(format); + return this.text.furigana(format); } async glossary() { // TODO: output nothing if this.filler == true - return new Glossary().withParent(await this.api); + return new Glossary().withAPI(await this.api); } + + /** @summary check if this word is written as ~ */ + public written(as: string) { + return this.text.writing == as || this.base.writing == as; + } + + /** @summary check if this word is read as ~ */ + public read(as: string) { + return this.text.reading == as || this.base.reading == as; + } + + /** @summary ignore this word for currently logged in user and refresh the sentence */ + async ignore() { + await (await this.api)["setTermPriority"](this.base.writing, this.base.reading, -1); + await (await this.parent).update(); + } + + /** @summary set parent sentence for this word */ + public withParent(parent: Sentence) { + this._resolveParent(parent); + return this; + } } |