Skip to content
Merged
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
8 changes: 5 additions & 3 deletions text/packages/chaff/rules/max-kanji-continuous.yaml

Large diffs are not rendered by default.

23 changes: 10 additions & 13 deletions text/packages/chaff/src/detectors/char-shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { proseText } from "../measure.ts";
import { maskSpans } from "../mask.ts";
import { quotedSpans } from "../quoted-span.ts";
import { nameSpans } from "../team-names.ts";
import { isIn } from "../lexicon-genres.ts";
import { type BreakMarks, holdsKnown, isBreakMark, isTitledPerson, standaloneNames, surfaceWithin, unknownPieces } from "./kanji-run-pieces.ts";
import { isHeadwordGenre } from "./headword-genre.ts";
import { type BreakMarks, holdsKnown, isBreakMark, isTitledPerson, repeatedTerms, standaloneNames, surfaceWithin, unknownPieces } from "./kanji-run-pieces.ts";
import { isBibliographyEntry } from "./bibliography-entry.ts";
import { isNamedByContext } from "./named-by-context.ts";

Expand Down Expand Up @@ -171,6 +171,8 @@ type RunContext = {
readonly repeated: ReadonlySet<string>;
/** 文書がほかで単独の連なりとして使う組織や題の名前(standaloneNames: 安全保障理事会、提供依頼申出書)と法令の名前とその名の元の語(statuteTerms)。 */
readonly terms: ReadonlySet<string>;
/** 文書が文の中で単独で繰り返す語(repeatedTerms: 長周期地震動階級)。連なりを閉じるときだけ、その前で割る。 */
readonly closingTerms: ReadonlySet<string>;
/** 文書のどこかで、括弧の中身がまるごとその連なりである名前(「署名用電子証明書確認画面」)。 */
readonly presented: ReadonlySet<string>;
/** 見出し語の行(文がその連なりだけ)を数えない文書か(用語集)。 */
Expand Down Expand Up @@ -286,13 +288,13 @@ const longestUnknown = (tokens: readonly Token[], rest: Span, keptHead: string,
const leadingName = leadingNamedOrganisation(runTokens, context.maxKnownWords, context.words.organisation);
const marks: BreakMarks = { suffixes: [...context.words.listSuffixes, ...context.words.references], blockUnits: context.words.blockUnits, leadingName };
// A run as long as the text holds no known word; reading it word by word would take a pass per word.
if (!holdsKnown(runTokens, words, [...context.terms, ...context.words.knownWords], marks)) return keptHead + words.join("");
if (!holdsKnown(runTokens, words, [...context.terms, ...context.closingTerms, ...context.words.knownWords], marks)) return keptHead + words.join("");
const isKnown = (start: number, end: number): boolean => {
const piece = words.slice(start, end).join("");
return (
isBreakMark(runTokens, words, { start, end }, marks) ||
context.terms.has(piece) ||
(end === words.length && context.words.knownWords.has(piece)) ||
(end === words.length && (context.words.knownWords.has(piece) || context.closingTerms.has(piece))) ||
isTitledPerson(runTokens, words, { start, end }, context.words.knownWords, context.maxKnownWords)
);
};
Expand Down Expand Up @@ -347,13 +349,6 @@ const statuteTerms = (runs: readonly DocumentRun[], fixedName: TitleWords, max:
const labelNames = (runs: readonly DocumentRun[]): string[] =>
runs.flatMap(({ run }) => (run.label && [...run.text].length >= MIN_TERM_LENGTH ? [run.text] : []));

/** 文書の genre が、rule の headword_genres(見出し語の行を数えない genre: 用語集)にあるか。 */
const isHeadwordGenre = (options: DetectorOptions): boolean => {
const genres = options.settings?.["headword_genres"];
const genre = options.genre;
return genre !== undefined && Array.isArray(genres) && genres.some((target: string) => isIn(genre, target));
};

const nameWordsOf = (doc: ProseDocument): NameWords => {
const patterns = (id: string): string[] => (doc.lexicons[id] ?? []).map((entry) => entry.pattern);
const organisation: OrganisationWords = {
Expand All @@ -370,7 +365,7 @@ const nameWordsOf = (doc: ProseDocument): NameWords => {
addressee: patterns("addressee-suffix"),
addressees: { prefixes: patterns("addressee-prefix"), titles: patterns("addressee-title"), endings: organisation.units, forms: organisation.closingForms },
references: patterns("reference-prefix"),
listSuffixes: [...patterns("list-suffix"), ...patterns("range-suffix")],
listSuffixes: [...patterns("list-suffix"), ...patterns("range-suffix"), ...patterns("breakdown-suffix")],
blockUnits: patterns("address-block-unit"),
knownWords: new Set(patterns("kanji-known-word")),
designations: patterns("designation-phrase"),
Expand All @@ -383,17 +378,19 @@ const contextOf = (doc: ProseDocument, options: DetectorOptions): RunContext =>
const names = doc.names ?? [];
const runs = documentRuns(doc, doc.profile, names);
const inSentences = runs.map(({ run }) => run.text);
const repeated = repeatedRuns(inSentences);
return {
profile: doc.profile,
names,
words,
titled: titledNames(doc, runs),
repeated: repeatedRuns(inSentences),
repeated,
terms: new Set([
...standaloneNames([...inSentences, ...headingRuns(doc)], MIN_TERM_LENGTH, options.limit, [...words.organisation.units, ...words.title.endings]),
...statuteTerms(runs, words.fixedName, options.limit),
...labelNames(runs),
]),
closingTerms: repeatedTerms(repeated, MIN_TERM_LENGTH, options.limit),
presented: new Set(runs.flatMap(({ run }) => (run.quoted || run.label ? [run.text] : []))),
headwords: isHeadwordGenre(options),
maxKnownWords: Math.max(options.limit, ...[...words.knownWords].map((word) => [...word].length)),
Expand Down
9 changes: 9 additions & 0 deletions text/packages/chaff/src/detectors/headword-genre.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { DetectorOptions } from "../plugin.ts";
import { isIn } from "../lexicon-genres.ts";

/** 文書の genre が、rule の headword_genres(見出し語の行を数えない genre: 用語集)にあるか。 */
export const isHeadwordGenre = (options: DetectorOptions): boolean => {
const genres = options.settings?.["headword_genres"];
const genre = options.genre;
return genre !== undefined && Array.isArray(genres) && genres.some((target: string) => isIn(genre, target));
};
8 changes: 8 additions & 0 deletions text/packages/chaff/src/detectors/kanji-run-pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ export const standaloneNames = (runs: readonly string[], min: number, max: numbe
}),
);

/**
* 文書が文の中で単独の連なりとして繰り返す語(repeated)のうち、字数が min から max までのもの(長周期地震動階級)。
* 読み手は文書の中でその語を何度も読んでいるので、その語が長い連なりを閉じれば(最大長周期地震動階級)、前の語との切れ目が見える。
* 頭に立つだけ(電子化手数料納付案内)なら、後ろは普通の語の続きなので割らない(呼び出し側が、連なりを閉じるときだけ使う)。
*/
export const repeatedTerms = (repeated: ReadonlySet<string>, min: number, max: number): Set<string> =>
new Set([...repeated].filter((run) => [...run].length >= min && [...run].length <= max));

/** 語の、範囲(span)の中の字。連なりの頭や尻で外した語にまたがる語は、範囲の中だけ。 */
export const surfaceWithin = (token: Token, span: Span): string =>
token.surface.slice(Math.max(0, span.start - token.span.start), Math.max(0, span.end - token.span.start));
Expand Down
7 changes: 7 additions & 0 deletions text/packages/lang-ja/lexicons/breakdown-suffix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 名詞の後ろに付き、その名詞ごとに分けることを示す語(年齢階級別、都道府県別)。
# max-kanji-continuous は、等(list-suffix)と同じに読む。漢字の連なりの尻にあり、解析が語の切れ目で分けたときは数えず、
# 連なりの中ほどにあれば(年齢階級別消費支出額)、語の終わりを見せるのでそこで割る。特別、区別のように一語の中の「別」は、解析が分けないので割らない。
id: breakdown-suffix
language: ja
entries:
- pattern: 別
16 changes: 16 additions & 0 deletions text/test/test_kanji_inner_suffix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ describe("max-kanji-continuous: 範囲の端や除くものを示す語(以外
assert.deepEqual(kanjiWords("内閣総理大臣就任以来、取り組んできた。\n"), ["内閣総理大臣就任以来"]);
});
});

describe("max-kanji-continuous: 分けることを示す語(別)", () => {
before(async () => {
await ja.prepare?.({ pos: true });
});

it("valid: 連なりの中ほどの「別」は語の終わりを見せるので、前後を別に数える", () => {
assert.deepEqual(kanjiWords("世帯主の年齢階級別消費支出額を加重平均した。\n"), []);
assert.deepEqual(kanjiWords("都道府県別市区町村別人口推計値を示す。\n"), []);
});

it("invalid: 「別」の後ろが上限を超えるものと、一語の中の「別」(特別)は数える", () => {
assert.deepEqual(kanjiWords("地域別新規顧客獲得施策検討会議を開いた。\n"), ["新規顧客獲得施策検討会議"]);
assert.deepEqual(kanjiWords("特別地方消費税交付金研究会議を開いた。\n"), ["特別地方消費税交付金研究会議"]);
});
});
16 changes: 16 additions & 0 deletions text/test/test_kanji_run_pieces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ describe("max-kanji-continuous: 知っている語で割る", () => {
assert.deepEqual(kanjiWords("料金は電子化手数料と呼ぶ。電子化手数料納付案内を送る。\n", "docs/faq"), ["電子化手数料納付案内"]);
});

it("valid: 文書が文の中で単独で繰り返す語を含む連なり(長周期地震動階級)", () => {
const source = "長周期地震動階級を予想します。長周期地震動階級が大きい地域です。最大長周期地震動階級が3以上の場合です。\n";
assert.deepEqual(kanjiWords(source, "docs/faq"), []);
});

it("invalid: 繰り返す語が連なりの頭に立つだけなら割らない", () => {
const source = "料金は電子化手数料と呼ぶ。電子化手数料が要る。電子化手数料納付案内を送る。\n";
assert.deepEqual(kanjiWords(source, "docs/faq"), ["電子化手数料納付案内"]);
});

it("invalid: 単独で一度しか使わない語と、上限を超える繰り返しは割らない", () => {
assert.deepEqual(kanjiWords("長周期地震動階級を予想します。最大長周期地震動階級が3以上の場合です。\n", "docs/faq"), ["最大長周期地震動階級"]);
const repeatedLong = "新規顧客獲得施策検討会議を開く。新規顧客獲得施策検討会議に出る。\n";
assert.deepEqual(kanjiWords(repeatedLong, "docs/faq"), ["新規顧客獲得施策検討会議", "新規顧客獲得施策検討会議"]);
});

it("valid: 漢数字と丁目の番地は、算用数字と同じく連なりを割る", () => {
assert.deepEqual(kanjiWords("市が中央通北二丁目地区再開発事業に着手した。\n", "business/report"), []);
});
Expand Down
Loading