import Glossary from "./glossary.ts"; import APIBase from "./base.ts"; import { ParseToken } from "../language/types.ts"; import Japanese, { JapaneseFormatter } from "./japanese.ts"; import "../util/string.ts"; import { Tag } from "../language/tags.ts"; export default class Word extends APIBase { /** @prop dictionary form of verb if this word is a verb */ 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; constructor(input: string | ParseToken) { super(); if (typeof input === "string") { this.filler = true; input = input as string; this.base = new Japanese(input, input); this.text = this.base; this.outputKanji = false; } else { this.filler = false; input = input as ParseToken; this.base = new Japanese(input.writing, input.reading); if (input.tags.anyOf(Object.values(Tag.Class.Verb))) { var writingCommon = input.writing.cmpLen(input.source); var readingCommon = input.reading.cmpLen(input.source); var stemLength = Math.max(writingCommon, readingCommon); var base = input[writingCommon > readingCommon ? "writing" : "reading"].substring(stemLength); var conjugation = input.source.substring(stemLength); this.text = new Japanese(input.writing.replaceLast(base, conjugation), input.reading.replaceLast(base, conjugation)); } else { this.text = this.base; } this.outputKanji = !input.source.kanaOnly(); // only output kanji if input also uses kanji } } furigana(format: JapaneseFormatter) { if (!this.outputKanji) return this.text.reading; else return this.text.furigana(format); } async glossary() { // TODO: output nothing if this.filler == true return new Glossary().withParent(await this.api); } }