Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions text/packages/lang-ja/lexicons/fused-ya-noun.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 並立の「や」で終わる形を、解析器(IPADIC)が一語の名詞として持っているもの。「学びや」は辞書では学び舎だが、
# 「学びや成長の機会」では「学び」と並立の「や」。後ろに名詞か連用形の動詞が助詞を挟まずに続くときだけ、二語に直す(fused-ya.ts)。
id: fused-ya-noun
language: ja
entries:
- pattern: 学びや
43 changes: 43 additions & 0 deletions text/packages/lang-ja/src/fused-ya.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Lexicon } from "chaffjs/plugin";
import type { Morpheme } from "./counter-tsu.ts";

/**
* 並立の「や」で終わる名詞を、辞書が一語の名詞として持っていることがある(学びや = 学び舎)。「学びや成長の機会」は
* 「学び や 成長」の並びなのに、一語に読むと「や」が消え、の の連なりが切れない。語彙表 fused-ya-noun に並べた語が
* 助詞を挟まずに名詞か連用形の動詞(気づき)の前に立つときだけ、語幹の名詞と並立助詞の「や」の二語に直す。
* 名詞の「学び舎」は後ろに助詞(学びやで・学びやを)か句読点が来るので、そのまま。
*/
export type FusedYaVocabulary = ReadonlySet<string>;

export const fusedYaVocabulary = (lexicons: Readonly<Record<string, Lexicon>>): 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],
);
10 changes: 8 additions & 2 deletions text/packages/lang-ja/src/pos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);

/**
* 非自立名詞(の・こと・もの・ため・はず)。品詞は名詞だが、単独では何も指さない。
* 「回るのか。」の「の」を体言止めと読むと、疑問文が全部ひっかかる。
Expand Down Expand Up @@ -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 };
Expand Down
64 changes: 64 additions & 0 deletions text/test/test_fused_ya.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Loading