From 479836dbf3c7cc6e5940abe698ccc5e1d7b440c7 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 11 Jul 2023 00:46:07 +0200 Subject: fix http client/server and irregular suru/kuru readings for api/word.ts --- api/word.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'api/word.ts') diff --git a/api/word.ts b/api/word.ts index 4dad4a3..4c09cff 100644 --- a/api/word.ts +++ b/api/word.ts @@ -3,9 +3,48 @@ import APIBase from "./base.ts"; import Japanese, { JapaneseFormatter } from "./japanese.ts"; import "../util/string.ts"; -import { TagGroup } from "../search/tags.ts"; +import { Tag, TagGroup, TokenTags } from "../search/tags.ts"; import { SearchWord } from "../search/types.ts"; +// irregular stems taken from +function irregularSuruStem(tags: TokenTags): string { + if (tags.anyOf([ + 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, + ])) return "し"; + if (tags.anyOf([ + Tag.Inflection.Passive, + Tag.Inflection.Causative, + ])) return "さ"; + if (tags.anyOf([ + Tag.Inflection.Potential, + ])) return "でき"; + return "す"; +} + +function irregularKuruStem(tags: TokenTags): string { + if (tags.anyOf([ + Tag.Inflection.Polite.Masu, + Tag.Inflection.Suffix.Te, + Tag.Inflection.Tense.Past, + Tag.Inflection.Desirable.Itai, // part of Wikipedia's -ta form + ])) return "き"; + if (tags.anyOf([ + Tag.Inflection.Negative, + Tag.Inflection.Desirable.Volitional, + Tag.Inflection.Passive, + Tag.Inflection.Causative, + Tag.Inflection.Potential, + Tag.Inflection.Command, + ])) return "こ"; + return "く"; +} + export default class Word extends APIBase { /** @prop dictionary form of verb if this word is a verb */ protected base: Japanese; @@ -29,13 +68,21 @@ export default class Word extends APIBase { input = input as SearchWord; this.base = new Japanese(input.writing, input.reading); if (input.tags.anyOf(TagGroup.Conjugable as string[])) { + // transfer conjugation from input.source to both dictionary reading and writing for furigana 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)); + + // 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; + else reading = reading.replaceLast(base, conjugation); + + // generate conjugated version of verb with kanji + this.text = new Japanese(input.writing.replaceLast(base, conjugation), reading); } else { this.text = this.base; } -- cgit v1.2.3