From e99ae80f7adc0f0e677381c3cc1549235d3877ab Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 11 Jul 2023 13:01:41 +0200 Subject: small cleanup --- api/word.ts | 72 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 31 deletions(-) (limited to 'api/word.ts') diff --git a/api/word.ts b/api/word.ts index 4c09cff..2e07c98 100644 --- a/api/word.ts +++ b/api/word.ts @@ -1,47 +1,57 @@ import Glossary from "./glossary.ts"; import APIBase from "./base.ts"; import Japanese, { JapaneseFormatter } from "./japanese.ts"; - import "../util/string.ts"; +import "../util/object.ts"; import { Tag, TagGroup, TokenTags } from "../search/tags.ts"; import { SearchWord } from "../search/types.ts"; +import { recursiveValues } from "../util/object.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 "でき"; + 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 "し"; + if ([ + Tag.Inflection.Passive, + Tag.Inflection.Causative, + ].includes(tag as any)) return "さ"; + // wikipedia has できる as the potential form for する, but できる here + // means it's already foobar'd + break; + } 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 "こ"; + 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 + ].includes(tag as any)) return "き"; + if ([ + Tag.Inflection.Negative, + Tag.Inflection.Desirable.Volitional, + Tag.Inflection.Passive, + Tag.Inflection.Causative, + Tag.Inflection.Potential, + Tag.Inflection.Command, + ].includes(tag as any)) return "こ"; + break; + } return "く"; } -- cgit v1.2.3