From b06312dffe8daf4ed7c518736217da95945ad509 Mon Sep 17 00:00:00 2001 From: isamu Date: Mon, 30 Nov 2026 12:41:16 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20the=20Japanese=20adapter=20reads=20?= =?UTF-8?q?=E5=AD=A6=E3=81=B3=E3=82=84=20before=20a=20noun=20as=20?= =?UTF-8?q?=E5=AD=A6=E3=81=B3=20and=20the=20coordinating=20=E3=82=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IPADIC holds 学びや as one noun (学び舎), so in 学びや成長の機会 the や vanished and no-doubled-joshi counted one の chain across the coordination. Co-Authored-By: Claude Opus 5.5 --- .../lang-ja/lexicons/fused-ya-noun.yaml | 6 ++ text/packages/lang-ja/src/fused-ya.ts | 43 +++++++++++++ text/packages/lang-ja/src/pos.ts | 10 ++- text/test/test_fused_ya.ts | 64 +++++++++++++++++++ 4 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 text/packages/lang-ja/lexicons/fused-ya-noun.yaml create mode 100644 text/packages/lang-ja/src/fused-ya.ts create mode 100644 text/test/test_fused_ya.ts diff --git a/text/packages/lang-ja/lexicons/fused-ya-noun.yaml b/text/packages/lang-ja/lexicons/fused-ya-noun.yaml new file mode 100644 index 000000000..4e8ea2366 --- /dev/null +++ b/text/packages/lang-ja/lexicons/fused-ya-noun.yaml @@ -0,0 +1,6 @@ +# 並立の「や」で終わる形を、解析器(IPADIC)が一語の名詞として持っているもの。「学びや」は辞書では学び舎だが、 +# 「学びや成長の機会」では「学び」と並立の「や」。後ろに名詞か連用形の動詞が助詞を挟まずに続くときだけ、二語に直す(fused-ya.ts)。 +id: fused-ya-noun +language: ja +entries: + - pattern: 学びや diff --git a/text/packages/lang-ja/src/fused-ya.ts b/text/packages/lang-ja/src/fused-ya.ts new file mode 100644 index 000000000..e5173ed7b --- /dev/null +++ b/text/packages/lang-ja/src/fused-ya.ts @@ -0,0 +1,43 @@ +import type { Lexicon } from "chaffjs/plugin"; +import type { Morpheme } from "./counter-tsu.ts"; + +/** + * 並立の「や」で終わる名詞を、辞書が一語の名詞として持っていることがある(学びや = 学び舎)。「学びや成長の機会」は + * 「学び や 成長」の並びなのに、一語に読むと「や」が消え、の の連なりが切れない。語彙表 fused-ya-noun に並べた語が + * 助詞を挟まずに名詞か連用形の動詞(気づき)の前に立つときだけ、語幹の名詞と並立助詞の「や」の二語に直す。 + * 名詞の「学び舎」は後ろに助詞(学びやで・学びやを)か句読点が来るので、そのまま。 + */ +export type FusedYaVocabulary = ReadonlySet; + +export const fusedYaVocabulary = (lexicons: Readonly>): FusedYaVocabulary => + new Set((lexicons["fused-ya-noun"] ?? []).map((entry) => entry.pattern)); + +const YA = "や"; + +const YA_READING = "ヤ"; + +const isContentNoun = (morpheme: Morpheme): boolean => morpheme.pos === "名詞" && !["非自立", "接尾"].includes(morpheme.pos_detail_1); + +const isContinuativeVerb = (morpheme: Morpheme): boolean => + morpheme.pos === "動詞" && morpheme.pos_detail_1 === "自立" && morpheme.conjugated_form === "連用形"; + +const startsCoordinate = (morpheme: Morpheme | undefined): boolean => morpheme !== undefined && (isContentNoun(morpheme) || isContinuativeVerb(morpheme)); + +const stemReading = (reading: string | undefined): { reading?: string } => + reading?.endsWith(YA_READING) === true ? { reading: reading.slice(0, -YA_READING.length) } : {}; + +const stemNoun = (morpheme: Morpheme): Morpheme => { + const stem = morpheme.surface_form.slice(0, -YA.length); + return { surface_form: stem, pos: "名詞", pos_detail_1: "一般", pos_detail_2: "*", basic_form: stem, ...stemReading(morpheme.reading) }; +}; + +const coordinatingYa = (): Morpheme => ({ surface_form: YA, pos: "助詞", pos_detail_1: "並立助詞", pos_detail_2: "*", basic_form: YA, reading: YA_READING }); + +const isFusedYa = (morpheme: Morpheme, vocabulary: FusedYaVocabulary): boolean => + morpheme.pos === "名詞" && morpheme.surface_form.endsWith(YA) && vocabulary.has(morpheme.surface_form); + +/** 形態素の並びを受け取り、語彙表の「や」で終わる名詞が並びの前に立つものを、語幹の名詞と並立の「や」にした並びを返す。ほかの語はそのまま。 */ +export const readFusedYa = (morphemes: readonly Morpheme[], vocabulary: FusedYaVocabulary): Morpheme[] => + morphemes.flatMap((morpheme, index) => + isFusedYa(morpheme, vocabulary) && startsCoordinate(morphemes[index + 1]) ? [stemNoun(morpheme), coordinatingYa()] : [morpheme], + ); diff --git a/text/packages/lang-ja/src/pos.ts b/text/packages/lang-ja/src/pos.ts index ba66af077..ef9a608f6 100644 --- a/text/packages/lang-ja/src/pos.ts +++ b/text/packages/lang-ja/src/pos.ts @@ -9,6 +9,7 @@ import { predicateFrameAfter } from "./predicate-frame.ts"; import { distributiveVocabulary, isEchoAt, type Inflection } from "./reduplication.ts"; import { isRaDroppedAt, raDroppedVocabulary } from "./ra-dropped.ts"; import { isSaInsertedAt } from "./sa-inserted.ts"; +import { fusedYaVocabulary, readFusedYa } from "./fused-ya.ts"; import type { Token } from "chaffjs/plugin"; import { kuromojiDictionaryPath, kuromojiModule } from "./kuromoji-module.ts"; @@ -122,6 +123,8 @@ const PREDICATE_FRAMES = (LEXICONS["passive-predicate-frame"] ?? []).map((entry) const REDUPLICATING_NOUNS = distributiveVocabulary(LEXICONS).nouns; +const FUSED_YA_VOCABULARY = fusedYaVocabulary(LEXICONS); + /** * 非自立名詞(の・こと・もの・ため・はず)。品詞は名詞だが、単独では何も指さない。 * 「回るのか。」の「の」を体言止めと読むと、疑問文が全部ひっかかる。 @@ -347,8 +350,11 @@ const tokensOf = (read: readonly Placed[]): Token[] => { const PIECE_LIMIT = 1000; const analyse = (tokenizer: Tokenizer, text: string): Morpheme[] => - readCompoundNounEnding( - readCounterTsu(analyserPieces(text, PIECE_LIMIT).flatMap((piece) => toArray(callMethod(tokenizer, "tokenize", [piece])).flatMap(toMorpheme))), + readFusedYa( + readCompoundNounEnding( + readCounterTsu(analyserPieces(text, PIECE_LIMIT).flatMap((piece) => toArray(callMethod(tokenizer, "tokenize", [piece])).flatMap(toMorpheme))), + ), + FUSED_YA_VOCABULARY, ); type Placed = { readonly morpheme: Morpheme; readonly start: number }; diff --git a/text/test/test_fused_ya.ts b/text/test/test_fused_ya.ts new file mode 100644 index 000000000..4ff7a46b2 --- /dev/null +++ b/text/test/test_fused_ya.ts @@ -0,0 +1,64 @@ +import { before, describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { firedRules } from "./rule-run.ts"; +import { readFusedYa } from "../packages/lang-ja/src/fused-ya.ts"; +import type { Morpheme } from "../packages/lang-ja/src/counter-tsu.ts"; +import { adapter as ja } from "../packages/lang-ja/src/index.ts"; + +// 辞書が一語の名詞(学び舎)と読む「学びや」を、並びの前では「学び」と並立の「や」に直す。例文は自作。 + +const morpheme = (surface: string, pos: string, detail1 = "一般", form?: string): Morpheme => ({ + surface_form: surface, + pos, + pos_detail_1: detail1, + pos_detail_2: "*", + basic_form: surface, + ...(form === undefined ? {} : { conjugated_form: form }), +}); + +const VOCABULARY = new Set(["学びや"]); + +const MANABIYA: Morpheme = { ...morpheme("学びや", "名詞"), reading: "マナビヤ" }; + +const shape = (morphemes: readonly Morpheme[]): string[] => morphemes.map((item) => `${item.surface_form}/${item.pos},${item.pos_detail_1}`); + +const doubledJoshi = (source: string): number => firedRules(ja, source).filter((id) => id === "no-doubled-joshi").length; + +describe("readFusedYa", () => { + it("splits the noun before a noun or a continuative verb into the stem and the coordinating や", () => { + const beforeNoun = readFusedYa([MANABIYA, morpheme("成長", "名詞", "サ変接続")], VOCABULARY); + assert.deepEqual(shape(beforeNoun), ["学び/名詞,一般", "や/助詞,並立助詞", "成長/名詞,サ変接続"]); + assert.equal(beforeNoun[0]?.reading, "マナビ"); + const beforeVerb = readFusedYa([MANABIYA, morpheme("気づき", "動詞", "自立", "連用形")], VOCABULARY); + assert.deepEqual(shape(beforeVerb), ["学び/名詞,一般", "や/助詞,並立助詞", "気づき/動詞,自立"]); + }); + + it("leaves the noun before a particle, a mark, a suffix or at the end, and a word not in the list", () => { + const cases: readonly (readonly Morpheme[])[] = [ + [MANABIYA, morpheme("で", "助詞", "格助詞")], + [MANABIYA, morpheme("、", "記号", "読点")], + [MANABIYA, morpheme("たち", "名詞", "接尾")], + [MANABIYA], + [morpheme("部屋", "名詞"), morpheme("成長", "名詞", "サ変接続")], + ]; + cases.forEach((sequence) => assert.deepEqual(readFusedYa(sequence, VOCABULARY), sequence)); + assert.deepEqual(readFusedYa([MANABIYA, morpheme("成長", "名詞")], new Set()), [MANABIYA, morpheme("成長", "名詞")]); + assert.deepEqual(readFusedYa([], VOCABULARY), []); + }); +}); + +describe("no-doubled-joshi and 学びや", () => { + before(async () => { + await ja.prepare?.({ pos: true }); + }); + + it("valid: や joining 学び and the next noun ends the の chain", () => { + assert.equal(doubledJoshi("今月の自分自身の学びや成長の機会を振り返る。"), 0); + assert.equal(doubledJoshi("日々の自分の学びや気づきの記録を残す。"), 0); + }); + + it("invalid: a chain with no や in it, or 学びや as a noun, is still reported", () => { + assert.equal(doubledJoshi("今月の自分自身の学びの成長の機会を振り返る。"), 1); + assert.equal(doubledJoshi("本社の営業部の学びやの建物の改修を進める。"), 1); + }); +});