Skip to content

feat: Support decimal Arabic-kanji numerals - #21

Draft
ymkjp wants to merge 4 commits into
geolonia:masterfrom
announce:fix/20-mixed-arabic-kanji-numerals
Draft

feat: Support decimal Arabic-kanji numerals#21
ymkjp wants to merge 4 commits into
geolonia:masterfrom
announce:fix/20-mixed-arabic-kanji-numerals

Conversation

@ymkjp

@ymkjp ymkjp commented Apr 3, 2026

Copy link
Copy Markdown

Summary

  • Fix findKanjiNumbers and kanji2number to handle decimal Arabic-kanji numerals like 8.5万, 25.24億, 1.2兆
  • These formats are common in Japanese price listings (e.g. 8.5万円 for rent, 1.2兆円 for national budget)

Changes

  • src/index.ts: Add decimal number pattern to the findKanjiNumbers regex so tokens like 8.5万 are captured whole
  • src/utils.ts: Add decimal number handling in kan2n() so 8.5 is correctly parsed as a coefficient
  • test/test.ts: Add test cases covering decimal + 万/億/兆 for both findKanjiNumbers and kanji2number
  • README.md: Add decimal examples

Out of scope

Checklist (optional)

  • Tests added/updated
  • Docs updated

Fixes #20 (comment).

Summary by CodeRabbit

リリースノート

  • 新機能
    • 小数点を含む数字と日本語単位の組み合わせ(例:8.5万、25.24億、1.20兆)を認識・変換できるようになりました。正確な整数結果を要求するケースでは不正な小数はエラーとなります。
  • ドキュメント
    • READMEに小数付き数値の変換例を追加しました。
  • テスト
    • 小数+単位の振る舞いを検証するテストケースを追加しました。

@coderabbitai

coderabbitai Bot commented Apr 3, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 52b23aa0-9327-4c4e-9002-94dea39f24e8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

README、正規表現、ユーティリティ、テストが更新され、小数点を含むアラビア数字と漢字の大位単位(例: 「8.5万」)の検出と変換が追加されました。findKanjiNumbers()は小数を含むトークンを抽出し、kanji2number()は条件付きで小数係数を扱います。

変更内容

Cohort / File(s) Summary
README 更新
README.md
小数を含む漢字単位の例を追加(例: "8.5万", "1.2兆")。
検索パターン拡張
src/index.ts
findKanjiNumbers()の正規表現を拡張して小数(半角・全角ドット、全角数字含む)を許可。matchAllに切替え、抽出後のフィルタも小数対応に変更。
小数係数処理
src/index.ts
splitLargeNumberPartsraw出力から各大位に対する生の係数文字列を受け取り、parseDecimalCoefficient()を追加して小数係数を扱うロジックを実装。小数係数はスケールを用いてBigIntへ変換し、割り切れる場合のみ加算、割り切れない場合はTypeErrorを投げる。
ユーティリティ拡張
src/utils.ts
splitLargeNumberParts(japanese)を新設(numbersrawを返す)。既存splitLargeNumberはこれを利用して戻り値を生成。zen2han()で全角ドット.に正規化。kan2n()がzen2han後の小数形式(例: ^[0-9]+\.[0-9]+$)を認識してNumberを返すように拡張。
テスト追加
test/test.ts
小数+大位単位(8.5万, 全角小数含む等)を検証するケースを追加。正しい整数変換を確認する成功ケースと、非整数結果となる場合のTypeError期待ケースを追加。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 分

🐰 小さな桁にも春が来た
点々光る八・五の舞
万も兆も手をつなぎ
正規表現は穴を塞ぎ
数がふわり、整数へ ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: Support decimal Arabic-kanji numerals' accurately describes the main change across all modified files (README, src/index.ts, src/utils.ts, test/test.ts), focusing on the new decimal number support feature.
Linked Issues check ✅ Passed All linked issue #20 objectives are fully met: findKanjiNumbers now captures mixed Arabic-kanji forms (including decimals) as single tokens, kanji2number correctly converts them, comprehensive test cases are added, and README documentation is updated.
Out of Scope Changes check ✅ Passed All changes directly address issue #20 requirements; no out-of-scope modifications detected. Decimal support, regex updates, utility function enhancements, and test additions all align with the stated PR objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ymkjp
ymkjp force-pushed the fix/20-mixed-arabic-kanji-numerals branch 2 times, most recently from 5787563 to 65bb9c0 Compare April 3, 2026 19:07
@ymkjp
ymkjp marked this pull request as ready for review April 3, 2026 19:14

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 65bb9c054b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/utils.ts
Comment thread src/index.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/index.ts (1)

62-70: ⚠️ Potential issue | 🟠 Major

純粋な小数(例: 8.5)まで抽出対象になっています。

Line 62 の拡張に対し、Line 69 が整数のみ除外のままなので、万/億/兆 を含まない純粋な小数が findKanjiNumbers() の結果に混入します。整数と同様に「純粋なアラビア小数」も除外する条件が必要です。

修正案
-      if ((! item.match(/^[0-90-9]+$/)) && (item.length && '兆' !== item && '億' !== item && '万' !== item && '萬' !== item)) {
+      if ((! item.match(/^[0-90-9]+([.][0-90-9]+)?$/)) && (item.length && '兆' !== item && '億' !== item && '万' !== item && '萬' !== item)) {
         return true
       } else {
         return false
       }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/index.ts` around lines 62 - 70, The filter in findKanjiNumbers currently
only rejects pure Arabic integers (uses /^[0-90-9]+$/) so pure Arabic decimals
like "8.5" slip through; update the filter condition that checks item against
/^[0-90-9]+$/ to also reject pure-decimal forms by using a regex that matches
either integer or decimal (e.g. /^[0-90-9]+(?:[..][0-90-9]+)?$/) when evaluating
the item inside the match.filter callback (the block referencing regex, match
and the anonymous filter function in src/index.ts).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@src/index.ts`:
- Around line 62-70: The filter in findKanjiNumbers currently only rejects pure
Arabic integers (uses /^[0-90-9]+$/) so pure Arabic decimals like "8.5" slip
through; update the filter condition that checks item against /^[0-90-9]+$/ to
also reject pure-decimal forms by using a regex that matches either integer or
decimal (e.g. /^[0-90-9]+(?:[..][0-90-9]+)?$/) when evaluating the item inside
the match.filter callback (the block referencing regex, match and the anonymous
filter function in src/index.ts).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fe76f535-8896-4357-ba35-d0012b0a2ced

📥 Commits

Reviewing files that changed from the base of the PR and between e09ee1e and 65bb9c0.

📒 Files selected for processing (4)
  • README.md
  • src/index.ts
  • src/utils.ts
  • test/test.ts

@ymkjp
ymkjp marked this pull request as draft April 3, 2026 19:26
ymkjp added 2 commits April 4, 2026 04:59
…anji2number

Handle formats like 8.5ä¸where a decimal number precedes
a kanji unit character. Fixes geolonia#20
- Use Math.round after unit multiplication to avoid floating-point
  errors (e.g. 0.29億 producing 28999999.999999996 instead of 29000000)
- Exclude standalone decimals without kanji units from findKanjiNumbers
  results (e.g. "8.5" in "値段は8.5です" should not be extracted)
@ymkjp
ymkjp force-pushed the fix/20-mixed-arabic-kanji-numerals branch from 65bb9c0 to fc9739e Compare April 3, 2026 19:59
@ymkjp ymkjp changed the title fix: Support decimal Arabic-kanji numerals feat: Support decimal Arabic-kanji numerals Apr 3, 2026
@ymkjp
ymkjp marked this pull request as ready for review April 3, 2026 20:09
@ymkjp
ymkjp marked this pull request as draft April 3, 2026 20:15
@ymkjp
ymkjp marked this pull request as draft April 3, 2026 20:15

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fc9739ed48

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/index.ts
Comment on lines +38 to +39
if (unit % decimal.scale !== 0n) {
throw new TypeError('The attribute of kanji2number() must be a Japanese numeral as integer.')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Accept equivalent decimal coefficients with trailing zeros

The divisibility guard rejects valid coefficients that only differ by trailing zeros in the fractional part. For example, kanji2number("1.20000万") should evaluate to the same integer as "1.2万" (12000), but unit % decimal.scale !== 0n throws because scale is not reduced before checking. This makes parsing depend on numeric formatting rather than value and will fail on fixed-precision inputs commonly emitted by external systems.

Useful? React with 👍 / 👎.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/index.ts`:
- Around line 33-45: The code treats a falsy return from
parseDecimalCoefficient() the same as "no coefficient", which causes invalid
decimal coefficients (e.g. NaN or '' interpreted as missing) to be swallowed;
update the branch in kanji2number where it iterates over largeNumbers to use
raw[key] !== '' as the presence check, call parseDecimalCoefficient(raw[key])
only when raw[key] !== '', and if parseDecimalCoefficient returns null/invalid
for a non-empty raw value throw a TypeError (instead of falling back to treating
the unit as absent); reference parseDecimalCoefficient, largeNumbers, numbers,
raw, and the kanji2number loop to locate and fix the logic so integer
coefficients still work but invalid decimal strings raise an error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7a7f8143-1673-4220-aa4f-22af0b328196

📥 Commits

Reviewing files that changed from the base of the PR and between 65bb9c0 and fc9739e.

📒 Files selected for processing (4)
  • README.md
  • src/index.ts
  • src/utils.ts
  • test/test.ts
✅ Files skipped from review due to trivial changes (1)
  • README.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/test.ts

Comment thread src/index.ts
Comment on lines 33 to +45
for (const key in largeNumbers) {
if (numbers[key]) {
const n = largeNumbers[key] * numbers[key]
number = number + n
const decimal = parseDecimalCoefficient(raw[key])
if (decimal) {
const unit = BigInt(largeNumbers[key])
if (unit % decimal.scale !== 0n) {
throw new TypeError('The attribute of kanji2number() must be a Japanese numeral as integer.')
}

number = number + Number(decimal.digits * (unit / decimal.scale))
} else {
number = number + largeNumbers[key] * numbers[key]
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

不正な小数係数が 0 として握りつぶされます。

parseDecimalCoefficient() が解釈できない係数でも、Line 34 の truthy 判定だと NaN が「その単位は存在しない」と同じ扱いになります。1百2.5万findKanjiNumbers() で 1 トークンとして拾えますが、ここでは の係数が無視されて最終結果が 0 になります。raw[key] !== '' を基準に分岐し、整数係数でも純粋な小数係数でもない値は TypeError にしたほうが安全です。

💡 修正案
     for (const key in largeNumbers) {
-      if (numbers[key]) {
+      if (raw[key] !== '') {
         const decimal = parseDecimalCoefficient(raw[key])
         if (decimal) {
           const unit = BigInt(largeNumbers[key])
           if (unit % decimal.scale !== 0n) {
             throw new TypeError('The attribute of kanji2number() must be a Japanese numeral as integer.')
           }

           number = number + Number(decimal.digits * (unit / decimal.scale))
-        } else {
+        } else if (Number.isFinite(numbers[key])) {
           number = number + largeNumbers[key] * numbers[key]
+        } else {
+          throw new TypeError('The attribute of kanji2number() must be a Japanese numeral as integer.')
         }
       }
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/index.ts` around lines 33 - 45, The code treats a falsy return from
parseDecimalCoefficient() the same as "no coefficient", which causes invalid
decimal coefficients (e.g. NaN or '' interpreted as missing) to be swallowed;
update the branch in kanji2number where it iterates over largeNumbers to use
raw[key] !== '' as the presence check, call parseDecimalCoefficient(raw[key])
only when raw[key] !== '', and if parseDecimalCoefficient returns null/invalid
for a non-empty raw value throw a TypeError (instead of falling back to treating
the unit as absent); reference parseDecimalCoefficient, largeNumbers, numbers,
raw, and the kanji2number loop to locate and fix the logic so integer
coefficients still work but invalid decimal strings raise an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

findKanjiNumbers misparses mixed Arabic-kanji numerals (e.g. 14万2000, 1億8990万)

1 participant