diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/word.ts | 53 |
1 files changed, 50 insertions, 3 deletions
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 <https://en.wikipedia.org/wiki/Japanese_irregular_verbs#suru_and_kuru> +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; } |