1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OCRが「令和」を「今和」と読む問題を、暦で確かめてから直す小さな道具を作った

1
Posted at

困りごと

日本語のPDFや帳票をOCRに通してAIに読ませると、日付が崩れることがあります。

  • 元号の「令」が「今」や「合」になる(今和6年3月15日)
  • 「日」が「曰」や「目」になる、数字の0や1が O や l になる(2O24年l月5日)

ある文書解析の道具を日本語PDFで測った記事では、OCRに回ったページで「令→今」の誤りがほぼ全部に出ていました(https://dev.to/elvisyao007/does-a-chinese-document-parser-actually-work-on-japanese-pdfs-i-measured-it-and-the-answer-is-2f5 )。
AIがこれを受け取ると、推測で直すか、存在しない日付を作ります。

作ったもの

JP OCR Date Fix(MIT・Python標準ライブラリのみ・外部通信なし)。
https://github.com/ayatrader/jp-ocr-date-fix

やることは3つだけです。

  1. 「元号+年+月(+日)」または「西暦+年+月(+日)」の形をした所だけを候補にする
  2. 決まった見間違えの表(今→令、戒→成、照→昭、曰→日、O→0、l→1 など)で置きかえる
  3. 置きかえた日付が暦に実在し、元号の期間内にあるときだけ直す。確かめられないものは直さずに知らせる
from jpocrdate import check_date
check_date("平戒31年4月30日")   # fixed → 平成31年4月30日 (2019-04-30)
check_date("平成31年5月1日")    # invalid(平成は4月30日まで。推測で直さない)

AIエージェントからは MCP サーバー(python -m jpocrdate.server)として呼べます。道具は jp_ocr_fix_dates と jp_ocr_check_date の2つです。

できないこと

  • R6.3.15 のような略記はまだ扱いません
  • 見間違えの表は手で作ったもので、すべての誤りは拾えません
  • 会社名や金額は対象外です

テストは18件。間違いや足りない見間違えがあれば Issue で教えてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?