From ee7e000921c0e16c84fd1463bfee055b60a85dca Mon Sep 17 00:00:00 2001 From: isamu Date: Mon, 30 Nov 2026 12:03:47 +0900 Subject: [PATCH] fix: max-kanji-continuous does not count a run followed by its reading in hiragana MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A run with its whole reading in round brackets right after it (四国八十八箇所巡礼(しこくはちじゅうはちかしょじゅんれい)) is one name the writer gives with its pronunciation, as a name filling 「」 is; it is not counted, and the same name is not counted where the document uses it again. A bracket that holds other words, or reads only part of the run, still counts. Co-Authored-By: Claude Opus 5.5 --- .../chaff/rules/max-kanji-continuous.yaml | 5 +- .../chaff/src/detectors/char-shape.ts | 6 +-- .../chaff/src/detectors/kanji-name-edge.ts | 15 ++++++ text/test/test_kanji_reading_bracket.ts | 52 +++++++++++++++++++ 4 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 text/test/test_kanji_reading_bracket.ts diff --git a/text/packages/chaff/rules/max-kanji-continuous.yaml b/text/packages/chaff/rules/max-kanji-continuous.yaml index 4b20017ec..4bda1a878 100644 --- a/text/packages/chaff/rules/max-kanji-continuous.yaml +++ b/text/packages/chaff/rules/max-kanji-continuous.yaml @@ -7,6 +7,7 @@ how_to_find: kanji-run uses: [pos] # chaff.yaml の names に並べた名前は連なりを切る。鉤括弧の中身がまるごと 1 つの連なりなら数えない(書き手が 1 つの名前と示している)。 # 語のすぐ後ろの丸括弧の中身がまるごと 1 つの連なり(EBITDA+(減価償却他調整前営業利益))も、前の語の言い換えか展開なので数えない。 +# 連なりのすぐ後ろの丸括弧に、連なり全体の読みを平仮名で添えたもの(四国八十八箇所巡礼(しこくはちじゅうはちかしょじゅんれい))も、書き手が示した 1 つの名前なので数えない。 # 住所の先頭にしか来ない単位(都道府県)。地名の後ろに付けば、割られた町名ではなく地名の並び。 # 組織の名前(organisation-form で始まるか地名・固有名詞で始まり organisation-unit で閉じる、organisation-unit で閉じる区切りが続く、 # または固有名詞か地名を含み、名前の後ろに立つ法人格で閉じる)は数えない。 @@ -101,8 +102,8 @@ example: after: 来週、新しい顧客を増やす施策を話し合う会議を開きます。 not_flagged: - ja: 組織の正式名称(国立研究開発法人産業技術総合研究所、経済産業省製造産業局自動車課、東日本青葉自動車販売株式会社)、字の半分以上が地名・組織名・人名でできた名前(東北地方太平洋沖地震、阪神淡路大震災)、題の名前(要綱・試験・事業・法令などで終わり、一行だけで書くか括弧でくくるか見出しにあるか、文書の中で繰り返すもの。大阪市鶴見区役所公募型比較見積実施要綱など)、法令・制度・給付・基金・指定区域の名前(住民基本台帳法施行令、勤労者財産形成貯蓄制度、社会資本整備総合交付金、地域医療介護総合確保基金、特定復興再生拠点区域。文のどこにあっても)、chaff.yaml の names に書いた組織名や製品名、法令の条番号(第二十二条第二項、学校教育法第三十条第二項の「第三十条第二項」)は数えません。空白を挟んで法人格か組織の区切りの後ろに立つ組織の名前(一般社団法人 全国〜センター、〜局 〜課 〜係)と、鉤括弧の中身が漢字の連なりとカタカナや英数字の語だけの名前(「登録業者情報検索入力ページ」)と、語のすぐ後ろの丸括弧を埋める言い換えや展開(EBITDA+(減価償却他調整前営業利益)、修士の学位(大学院修士課程修了程度))も数えません。頭の、前を指す語(上記、当該)と代の序数(第百二代、初代)と漢数字の日付(令和七年六月二十三日、令和元年)、尻の「等」と範囲を示す語(自動車保険契約以外の「以外」、基準以上の「以上」)と、仮名へ続く接続の語の漢字(方針及び基本構想の「及」)は数えません。連なりの中ほどの「等」と範囲を示す語と分けることを示す「別」(高齢運転者等専用駐車区間、関係者以外立入禁止、年齢階級別消費支出額)は語の終わりを、前を指す語(見出しと文が一行に詰まった 苗関連事業当連結会計年度の「当」)は語の頭を見せるので、その前と後ろを別に数えます。文書のどこかで括弧でまるごとくくった名前(「署名用電子証明書確認画面」、[当事者系審判請求人])と、番号や内容の行の上に一行で置いた名前(児童相談所虐待対応ダイヤルの下に 189)は文の中で使っても数えず、法令の題を囲む括弧((〜に関する法律))の中の連なりも数えません。文書が単独で使う法令の名前(政治資金規正法)と法令が名に取った語(愛玩動物看護師法の「愛玩動物看護師」)、職名のすぐ前か後ろの人の姓と名(小泉純一郎内閣総理大臣、内閣総理大臣石破茂)は、連なりを割ります。序数で始まる組織の単位(方式審査室第三担当、東京電力福島第一原子力発電所)と、中黒の後ろで組織の語で閉じる名前の前半(社会保障審議会企業年金・個人年金部会の「社会保障審議会企業年金」)も数えません。頭の地名や固有名詞で始まる組織の名前(国連安全保障理事会改革の「国連安全保障理事会」、東京電力福島第一原子力発電所事故の「東京電力福島第一原子力発電所」)、文書がほかで単独で使う組織や書式の名前(安全保障理事会常任理事国の「安全保障理事会」、提供依頼申出書作成説明会の「提供依頼申出書」)、連なりを閉じる決まった語(日本国内閣総理大臣の「内閣総理大臣」、〜許可事業者、〜検討会委員の「委員」)は、その前後を別の連なりとして数えます。用語集(headword_genres)の、一行が漢字の連なりだけの見出し語は数えません。出席者の並び(区長、副区長、〜委員会事務局長)のように文で終わらない名前の並びの項目は、宛名と同じく、組織の名前の後ろの役職を数えません。注の中の文献の記述(著者. 題. 出版者, 2025, p. 369.)は、引いた文献の題と名前なので数えません。二重鉤括弧でくくった作品や碑の題の中の連なり(『原爆犠牲国民学校教師と子どもの碑』)も、引いた題なので数えません(鉤括弧の中の二重鉤括弧は引用の中の引用なので数えます)。尻の漢数字の数と助数詞(国交正常化六十周年の「六十周年」)と、皇族の敬称(秋篠宮皇嗣同妃両殿下の「両殿下」)は、その前を別に数えます。連なりの中の漢数字の番地(中央通北二丁目地区再開発事業の「二丁目」)は、算用数字と同じく連なりを割り、その前と後ろを別に数えます。文書が文の中で単独で繰り返す語が連なりを閉じるとき(長周期地震動階級を繰り返す文書の 最大長周期地震動階級)は、その前を別に数えます。 - en: A name made mostly of place, organisation and person names (東北地方太平洋沖地震, 阪神淡路大震災) is not counted. Official organisation names (a legal form or a place name followed by a name that ends in a unit such as 研究所 or 協会, or a chain of units such as 省・局・課, or a name with a proper noun closed by a legal form such as 株式会社), titles of named things (a run ending in 要綱, 試験, 事業, 法 and the like that stands on a line of its own, closes a bracket, is followed by a bracketed note, appears in a heading, or recurs in the document), names of statutes, schemes, grants, funds and designated zones wherever they stand (住民基本台帳法施行令, 勤労者財産形成貯蓄制度, 社会資本整備総合交付金, 地域医療介護総合確保基金, 特定復興再生拠点区域), names listed under names in chaff.yaml, and provision numbers in statutes (第二十二条第二項, and 第三十条第二項 after the statute name in 学校教育法第三十条第二項) are not counted. Nor is an organisation name that follows a legal form or an organisation unit across a space (一般社団法人 全国〜センター, 〜局 〜課 〜係), or a name in 「」 made of the kanji run and words with no kanji or hiragana (「登録業者情報検索入力ページ」), or a run that fills the round brackets right after a word, restating or expanding it (EBITDA+(減価償却他調整前営業利益), 修士の学位(大学院修士課程修了程度)). A leading word that points back or ahead (上記, 当該), a leading ordinal (第百二代, 初代), a leading date in kanji numerals (令和七年六月二十三日, 令和元年), a trailing 等 or a word that marks a range (以外 in 自動車保険契約以外, 以上 in 基準以上), and the kanji of a conjunction that continues in kana (及 in 方針及び基本構想) are not counted. A 等, a range word or a 別 that breaks things down in the middle of a run (高齢運転者等専用駐車区間, 関係者以外立入禁止, 年齢階級別消費支出額) shows where a word ends, and a word that points back (当 in 苗関連事業当連結会計年度, a heading run into its sentence) shows where one starts, so what is before and after it is counted apart. A name the document puts whole in brackets somewhere (「署名用電子証明書確認画面」, [当事者系審判請求人]), or writes as a line of its own above a number or other lines (児童相談所虐待対応ダイヤル above 189), is not counted where it is used in a sentence either, nor is a run inside the brackets around a statute's title ((〜に関する法律)). A statute name the document uses on its own (政治資金規正法), the term a statute is named after (愛玩動物看護師 in 愛玩動物看護師法), and a person's full name right before or after an official title (小泉純一郎内閣総理大臣, 内閣総理大臣石破茂) split the run. Nor is an organisation unit that starts with an ordinal (方式審査室第三担当, 東京電力福島第一原子力発電所), or the first half of an organisation name that continues after a ・ and closes in an organisation unit (社会保障審議会企業年金 in 社会保障審議会企業年金・個人年金部会). An organisation name that starts the run and holds a proper noun (国連安全保障理事会 in 国連安全保障理事会改革, 東京電力福島第一原子力発電所 in 東京電力福島第一原子力発電所事故), an organisation or form name the document uses on its own elsewhere (安全保障理事会 in 安全保障理事会常任理事国, 提供依頼申出書 in 提供依頼申出書作成説明会), and a fixed word that closes the run (内閣総理大臣 in 日本国内閣総理大臣, 〜許可事業者, 委員 in 〜検討会委員) split the run, and what is before and after them is counted apart. In a glossary (headword_genres), a line that holds only a run of kanji is the headword and is not counted. In a list of names with no sentence end (an attendee list such as 区長、副区長、〜委員会事務局長), the title after an organisation name in an item is not counted, as in an addressee line. A bibliographic entry in a note (著者. 題. 出版者, 2025, p. 369.) holds the cited work's titles and names, and is not counted. Nor is a run inside 『』, which holds the title of a cited work or monument (『原爆犠牲国民学校教師と子どもの碑』); 『』 inside 「」 is a quotation within a quotation and is counted. A trailing quantity in kanji numerals with its counter (六十周年 in 国交正常化六十周年) and an imperial honorific (両殿下 in 秋篠宮皇嗣同妃両殿下) split the run, and what is before them is counted on its own. A block number in kanji numerals inside a run (二丁目 in 中央通北二丁目地区再開発事業) splits the run as Arabic numerals would, and what is before and after it is counted apart. A term the document repeats on its own in its sentences splits a run it closes (最大長周期地震動階級 in a document that repeats 長周期地震動階級), and what is before it is counted on its own. + ja: 組織の正式名称(国立研究開発法人産業技術総合研究所、経済産業省製造産業局自動車課、東日本青葉自動車販売株式会社)、字の半分以上が地名・組織名・人名でできた名前(東北地方太平洋沖地震、阪神淡路大震災)、題の名前(要綱・試験・事業・法令などで終わり、一行だけで書くか括弧でくくるか見出しにあるか、文書の中で繰り返すもの。大阪市鶴見区役所公募型比較見積実施要綱など)、法令・制度・給付・基金・指定区域の名前(住民基本台帳法施行令、勤労者財産形成貯蓄制度、社会資本整備総合交付金、地域医療介護総合確保基金、特定復興再生拠点区域。文のどこにあっても)、chaff.yaml の names に書いた組織名や製品名、法令の条番号(第二十二条第二項、学校教育法第三十条第二項の「第三十条第二項」)は数えません。空白を挟んで法人格か組織の区切りの後ろに立つ組織の名前(一般社団法人 全国〜センター、〜局 〜課 〜係)と、鉤括弧の中身が漢字の連なりとカタカナや英数字の語だけの名前(「登録業者情報検索入力ページ」)と、語のすぐ後ろの丸括弧を埋める言い換えや展開(EBITDA+(減価償却他調整前営業利益)、修士の学位(大学院修士課程修了程度))と、すぐ後ろの丸括弧に平仮名で読みを添えた連なり(四国八十八箇所巡礼(しこくはちじゅうはちかしょじゅんれい))も数えません。頭の、前を指す語(上記、当該)と代の序数(第百二代、初代)と漢数字の日付(令和七年六月二十三日、令和元年)、尻の「等」と範囲を示す語(自動車保険契約以外の「以外」、基準以上の「以上」)と、仮名へ続く接続の語の漢字(方針及び基本構想の「及」)は数えません。連なりの中ほどの「等」と範囲を示す語と分けることを示す「別」(高齢運転者等専用駐車区間、関係者以外立入禁止、年齢階級別消費支出額)は語の終わりを、前を指す語(見出しと文が一行に詰まった 苗関連事業当連結会計年度の「当」)は語の頭を見せるので、その前と後ろを別に数えます。文書のどこかで括弧でまるごとくくった名前(「署名用電子証明書確認画面」、[当事者系審判請求人])と、番号や内容の行の上に一行で置いた名前(児童相談所虐待対応ダイヤルの下に 189)は文の中で使っても数えず、法令の題を囲む括弧((〜に関する法律))の中の連なりも数えません。文書が単独で使う法令の名前(政治資金規正法)と法令が名に取った語(愛玩動物看護師法の「愛玩動物看護師」)、職名のすぐ前か後ろの人の姓と名(小泉純一郎内閣総理大臣、内閣総理大臣石破茂)は、連なりを割ります。序数で始まる組織の単位(方式審査室第三担当、東京電力福島第一原子力発電所)と、中黒の後ろで組織の語で閉じる名前の前半(社会保障審議会企業年金・個人年金部会の「社会保障審議会企業年金」)も数えません。頭の地名や固有名詞で始まる組織の名前(国連安全保障理事会改革の「国連安全保障理事会」、東京電力福島第一原子力発電所事故の「東京電力福島第一原子力発電所」)、文書がほかで単独で使う組織や書式の名前(安全保障理事会常任理事国の「安全保障理事会」、提供依頼申出書作成説明会の「提供依頼申出書」)、連なりを閉じる決まった語(日本国内閣総理大臣の「内閣総理大臣」、〜許可事業者、〜検討会委員の「委員」)は、その前後を別の連なりとして数えます。用語集(headword_genres)の、一行が漢字の連なりだけの見出し語は数えません。出席者の並び(区長、副区長、〜委員会事務局長)のように文で終わらない名前の並びの項目は、宛名と同じく、組織の名前の後ろの役職を数えません。注の中の文献の記述(著者. 題. 出版者, 2025, p. 369.)は、引いた文献の題と名前なので数えません。二重鉤括弧でくくった作品や碑の題の中の連なり(『原爆犠牲国民学校教師と子どもの碑』)も、引いた題なので数えません(鉤括弧の中の二重鉤括弧は引用の中の引用なので数えます)。尻の漢数字の数と助数詞(国交正常化六十周年の「六十周年」)と、皇族の敬称(秋篠宮皇嗣同妃両殿下の「両殿下」)は、その前を別に数えます。連なりの中の漢数字の番地(中央通北二丁目地区再開発事業の「二丁目」)は、算用数字と同じく連なりを割り、その前と後ろを別に数えます。文書が文の中で単独で繰り返す語が連なりを閉じるとき(長周期地震動階級を繰り返す文書の 最大長周期地震動階級)は、その前を別に数えます。 + en: A name made mostly of place, organisation and person names (東北地方太平洋沖地震, 阪神淡路大震災) is not counted. Official organisation names (a legal form or a place name followed by a name that ends in a unit such as 研究所 or 協会, or a chain of units such as 省・局・課, or a name with a proper noun closed by a legal form such as 株式会社), titles of named things (a run ending in 要綱, 試験, 事業, 法 and the like that stands on a line of its own, closes a bracket, is followed by a bracketed note, appears in a heading, or recurs in the document), names of statutes, schemes, grants, funds and designated zones wherever they stand (住民基本台帳法施行令, 勤労者財産形成貯蓄制度, 社会資本整備総合交付金, 地域医療介護総合確保基金, 特定復興再生拠点区域), names listed under names in chaff.yaml, and provision numbers in statutes (第二十二条第二項, and 第三十条第二項 after the statute name in 学校教育法第三十条第二項) are not counted. Nor is an organisation name that follows a legal form or an organisation unit across a space (一般社団法人 全国〜センター, 〜局 〜課 〜係), or a name in 「」 made of the kanji run and words with no kanji or hiragana (「登録業者情報検索入力ページ」), or a run that fills the round brackets right after a word, restating or expanding it (EBITDA+(減価償却他調整前営業利益), 修士の学位(大学院修士課程修了程度)), or a run followed right away by its whole reading in hiragana in round brackets (四国八十八箇所巡礼(しこくはちじゅうはちかしょじゅんれい)). A leading word that points back or ahead (上記, 当該), a leading ordinal (第百二代, 初代), a leading date in kanji numerals (令和七年六月二十三日, 令和元年), a trailing 等 or a word that marks a range (以外 in 自動車保険契約以外, 以上 in 基準以上), and the kanji of a conjunction that continues in kana (及 in 方針及び基本構想) are not counted. A 等, a range word or a 別 that breaks things down in the middle of a run (高齢運転者等専用駐車区間, 関係者以外立入禁止, 年齢階級別消費支出額) shows where a word ends, and a word that points back (当 in 苗関連事業当連結会計年度, a heading run into its sentence) shows where one starts, so what is before and after it is counted apart. A name the document puts whole in brackets somewhere (「署名用電子証明書確認画面」, [当事者系審判請求人]), or writes as a line of its own above a number or other lines (児童相談所虐待対応ダイヤル above 189), is not counted where it is used in a sentence either, nor is a run inside the brackets around a statute's title ((〜に関する法律)). A statute name the document uses on its own (政治資金規正法), the term a statute is named after (愛玩動物看護師 in 愛玩動物看護師法), and a person's full name right before or after an official title (小泉純一郎内閣総理大臣, 内閣総理大臣石破茂) split the run. Nor is an organisation unit that starts with an ordinal (方式審査室第三担当, 東京電力福島第一原子力発電所), or the first half of an organisation name that continues after a ・ and closes in an organisation unit (社会保障審議会企業年金 in 社会保障審議会企業年金・個人年金部会). An organisation name that starts the run and holds a proper noun (国連安全保障理事会 in 国連安全保障理事会改革, 東京電力福島第一原子力発電所 in 東京電力福島第一原子力発電所事故), an organisation or form name the document uses on its own elsewhere (安全保障理事会 in 安全保障理事会常任理事国, 提供依頼申出書 in 提供依頼申出書作成説明会), and a fixed word that closes the run (内閣総理大臣 in 日本国内閣総理大臣, 〜許可事業者, 委員 in 〜検討会委員) split the run, and what is before and after them is counted apart. In a glossary (headword_genres), a line that holds only a run of kanji is the headword and is not counted. In a list of names with no sentence end (an attendee list such as 区長、副区長、〜委員会事務局長), the title after an organisation name in an item is not counted, as in an addressee line. A bibliographic entry in a note (著者. 題. 出版者, 2025, p. 369.) holds the cited work's titles and names, and is not counted. Nor is a run inside 『』, which holds the title of a cited work or monument (『原爆犠牲国民学校教師と子どもの碑』); 『』 inside 「」 is a quotation within a quotation and is counted. A trailing quantity in kanji numerals with its counter (六十周年 in 国交正常化六十周年) and an imperial honorific (両殿下 in 秋篠宮皇嗣同妃両殿下) split the run, and what is before them is counted on its own. A block number in kanji numerals inside a run (二丁目 in 中央通北二丁目地区再開発事業) splits the run as Arabic numerals would, and what is before and after it is counted apart. A term the document repeats on its own in its sentences splits a run it closes (最大長周期地震動階級 in a document that repeats 長周期地震動階級), and what is before it is counted on its own. level_meaning: ja: 漢字の連続 {limit} 字まで en: up to {limit} kanji in a row diff --git a/text/packages/chaff/src/detectors/char-shape.ts b/text/packages/chaff/src/detectors/char-shape.ts index 5c1c5d212..56422dbef 100644 --- a/text/packages/chaff/src/detectors/char-shape.ts +++ b/text/packages/chaff/src/detectors/char-shape.ts @@ -6,7 +6,7 @@ import { leadingPrefix, trailingTitle, type AddresseeWords } from "./addressee-t import { followsForm, followsUnit, - fillsGloss, + bracketNames, holdsQuotedName, inWorkTitle, isListItem, @@ -82,10 +82,10 @@ const maskedProse = (prose: string, profile: DocumentProfile | undefined, names: /** * 鉤括弧と隅付き括弧の中身が、この範囲(文の先頭からの位置)と、漢字も平仮名も無い語(ページ、センター)だけか(尻の丸括弧の補いは除く)。 - * 括弧でくくった名前は、書き手が 1 つの名前として示したもの(「登録業者情報検索入力ページ」)。 + * 括弧でくくった名前は、書き手が 1 つの名前として示したもの(「登録業者情報検索入力ページ」)。すぐ後ろに読みの括弧を添えた連なりも同じ。 */ const fillsQuote = (sentence: Sentence, start: number, end: number): boolean => - quotedSpans(sentence.text, NAME_MARKS).some((span) => holdsQuotedName(sentence.text, span, start, end)) || fillsGloss(sentence.text, start, end); + quotedSpans(sentence.text, NAME_MARKS).some((span) => holdsQuotedName(sentence.text, span, start, end)) || bracketNames(sentence.text, start, end); const runsOf = (sentence: Sentence, profile: DocumentProfile | undefined, names: readonly string[]): KanjiRun[] => { const prose = proseText(sentence); diff --git a/text/packages/chaff/src/detectors/kanji-name-edge.ts b/text/packages/chaff/src/detectors/kanji-name-edge.ts index 97107d313..4f57bf173 100644 --- a/text/packages/chaff/src/detectors/kanji-name-edge.ts +++ b/text/packages/chaff/src/detectors/kanji-name-edge.ts @@ -149,6 +149,18 @@ const GLOSSED_WORD_END = /[\p{Script=Han}\p{Script=Katakana}\p{Script=Latin}\p{N export const fillsGloss = (text: string, start: number, end: number): boolean => quotedSpans(text, GLOSS_MARKS).some((span) => GLOSSED_WORD_END.test(text.slice(0, span.start - 1)) && holdsName(text, span, start, end)); +/** A reading in kana in the round brackets right after a word (四国八十八箇所巡礼(しこくはちじゅうはちかしょじゅんれい)). */ +const READING = /^[((]([\p{Script=Hiragana}ー・\s]+)[))]/u; + +/** + * 漢字の連なり [start, end) のすぐ後ろの丸括弧が、連なり全体の読み(少なくとも一字に一つの仮名)か。読みを添えた連なりは、 + * 書き手が一つの名前として読み方まで示したもの(四国八十八箇所巡礼(しこくはちじゅうはちかしょじゅんれい))。 + */ +export const givesReading = (text: string, start: number, end: number): boolean => { + const reading = READING.exec(text.slice(end))?.[1]?.replace(/[\s・]/gu, ""); + return reading !== undefined && reading.length >= end - start; +}; + const holds = (span: Span, start: number, end: number): boolean => span.start <= start && end <= span.end; /** @@ -159,3 +171,6 @@ export const inWorkTitle = (text: string, start: number, end: number): boolean = const quotes = quotedSpans(text, { "「": "」" }); return quotedSpans(text, { "『": "』" }).some((title) => holds(title, start, end) && !quotes.some((quote) => holds(quote, title.start, title.end))); }; + +/** 語の補いの括弧を埋めるか(fillsGloss)、すぐ後ろの括弧に読みを添えたか(givesReading)。どちらも書き手が 1 つの名前と示したもの。 */ +export const bracketNames = (text: string, start: number, end: number): boolean => fillsGloss(text, start, end) || givesReading(text, start, end); diff --git a/text/test/test_kanji_reading_bracket.ts b/text/test/test_kanji_reading_bracket.ts new file mode 100644 index 000000000..c984f6d98 --- /dev/null +++ b/text/test/test_kanji_reading_bracket.ts @@ -0,0 +1,52 @@ +import { describe, it, before } from "node:test"; +import assert from "node:assert/strict"; +import { buildDocument } from "../packages/chaff/src/document.ts"; +import { loadRules } from "../packages/chaff/src/rule-load.ts"; +import { runRules } from "../packages/chaff/src/run.ts"; +import { adapter as ja } from "../packages/lang-ja/src/index.ts"; +import { measuredOffOn } from "../scripts/rules-measure-files.ts"; +import { givesReading } from "../packages/chaff/src/detectors/kanji-name-edge.ts"; + +// max-kanji-continuous: 連なりのすぐ後ろの丸括弧に仮名で読みを添えれば、書き手が読み方まで示した 1 つの名前。例文は自作。 + +const RULES = loadRules("ja"); +const KANJI_AT_NORMAL = { "max-kanji-continuous": RULES.find((rule) => rule.id === "max-kanji-continuous")?.levels.normal ?? 0 }; + +const kanjiWords = (source: string, genre = "blog/essay"): string[] => + runRules(buildDocument("t.md", source, ja), RULES, measuredOffOn(genre), true, genre, KANJI_AT_NORMAL) + .findings.filter((finding) => finding.rule === "max-kanji-continuous") + .map((finding) => String(finding.values?.["word"])); + +describe("max-kanji-continuous: 連なりの後ろの読みの括弧", () => { + before(async () => { + await ja.prepare?.({ pos: true }); + }); + + it("valid: 連なり全体の読みを仮名で添えた名前は数えない", () => { + assert.deepEqual(kanjiWords("西国三十三所観音霊場巡礼(さいこくさんじゅうさんしょかんのんれいじょうじゅんれい)は近畿を巡る古い巡礼路です。"), []); + assert.deepEqual(kanjiWords("西国三十三所観音霊場巡礼(さいこく さんじゅうさんしょ かんのん れいじょう じゅんれい)は古い巡礼路です。"), []); + }); + + it("valid: 文書のどこかで読みを添えた名前は、文の中で使っても数えない", () => { + assert.deepEqual( + kanjiWords("西国三十三所観音霊場巡礼(さいこくさんじゅうさんしょかんのんれいじょうじゅんれい)を歩いた。\n\n西国三十三所観音霊場巡礼は一月かかる。\n"), + [], + ); + }); + + it("invalid: 括弧が読みでないか、連なりの一部の読みしかなければ数える", () => { + assert.deepEqual(kanjiWords("西国三十三所観音霊場巡礼(約千キロ)は古い巡礼路です。"), ["西国三十三所観音霊場巡礼"]); + assert.deepEqual(kanjiWords("西国三十三所観音霊場巡礼(じゅんれい)は古い巡礼路です。"), ["西国三十三所観音霊場巡礼"]); + assert.deepEqual(kanjiWords("来週、新規顧客獲得施策検討会議を開きます。"), ["新規顧客獲得施策検討会議"]); + }); + + it("reads the reading in the bracket right after the run only", () => { + assert.equal(givesReading("四国遍路(しこくへんろ)", 0, 4), true); + assert.equal(givesReading("四国遍路(しこく)", 0, 4), false); + assert.equal(givesReading("四国遍路(へんろ)", 0, 4), false); + assert.equal(givesReading("四国遍路 (しこくへんろ)", 0, 4), false); + assert.equal(givesReading("四国遍路(シコクヘンロ)", 0, 4), false); + assert.equal(givesReading("四国遍路", 0, 4), false); + assert.equal(givesReading("四国遍路()", 0, 4), false); + }); +});