Convert Chinese numerals to Arabic numerals with Python

When I googled it, there weren't many samples and it was just old (Python2 version), so I made it. (Fixed because it was a bug)

python


import re

tt_ksuji = str.maketrans('1 2 3 4 5 6 7 8 90 Ichi 2', '1234567890123')

re_suji = re.compile(r'[10 million billion trillion\d]+')
re_kunit = re.compile(r'[Hundred thousand]|\d+')
re_manshin = re.compile(r'[100 billion trillion]|[^100 billion trillion]+')

TRANSUNIT = {'Ten': 10,
             'Pick up': 10,
             'hundred': 100,
             'thousand': 1000}
TRANSMANS = {'Ten thousand': 10000,
             'Billion': 100000000,
             'Trillion': 1000000000000}


def kansuji2arabic(kstring: str, sep=False):
    """Convert Chinese numerals to Arabic numerals"""

    def _transvalue(sj: str, re_obj=re_kunit, transdic=TRANSUNIT):
        unit = 1
        result = 0
        for piece in reversed(re_obj.findall(sj)):
            if piece in transdic:
                if unit > 1:
                    result += unit
                unit = transdic[piece]
            else:
                val = int(piece) if piece.isdecimal() else _transvalue(piece)
                result += val * unit
                unit = 1

        if unit > 1:
            result += unit

        return result

    transuji = kstring.translate(tt_ksuji)
    for suji in sorted(set(re_suji.findall(transuji)), key=lambda s: len(s),
                           reverse=True):
        if not suji.isdecimal():
            arabic = _transvalue(suji, re_manshin, TRANSMANS)
            arabic = '{:,}'.format(arabic) if sep else str(arabic)
            transuji = transuji.replace(suji, arabic)
        elif sep and len(suji) > 3:
            transuji = transuji.replace(suji, '{:,}'.format(int(suji)))

    return transuji

In fact, any Chinese or Arabic numeral is now normalized to a half-width Arabic numeral.

   kansuji2arabic('Budget amount for fiscal year 2007: 135 million, 164 yen', True)
   '2015 budget amount 1,035,001,164 yen'

Recommended Posts

Convert Chinese numerals to Arabic numerals with Python
Convert list to DataFrame with python
Convert memo at once with Python 2to3
Convert Excel data to JSON with python
Convert FX 1-minute data to 5-minute data with Python
Convert HEIC files to PNG files with Python
Sample to convert image to Wavelet with Python
Convert 202003 to 2020-03 with pandas
Convert PDF to image (JPEG / PNG) with Python
Convert PDFs to images in bulk with Python
Convert Windows epoch values to date with python
Convert strings to character-by-character list format with python
Connect to BigQuery with Python
How to convert / restore a string with [] in python
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Connect to Wikipedia with Python
Post to slack with Python 3
Convert the image in .zip to PDF with Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
PyInstaller memorandum Convert Python [.py] to [.exe] with 2 lines
Switch python to 2.7 with alternatives
Write to csv with Python
Convert python 3.x code to python 2.x
How to convert an array to a dictionary with Python [Application]
Convert images to sepia toning with PIL (Python Imaging Library)
Convert video to black and white with ffmpeg + python + opencv
Convert .ipynb to .html (with BatchFile)
Link to get started with python
Convert markdown to PDF in Python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Workflow to convert formula (image) to python
MP3 to WAV conversion with Python
Convert sentences to vectors with gensim
To do tail recursion with Python2
How to get started with Python
Python> list> Convert double list to single list
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
[Python] Convert natural numbers to ordinal numbers
How to calculate date with python
Convert decimal numbers to n-ary numbers [python]
Convert PDF to image with ImageMagick
Easily post to twitter with Python 3
I want to debug with Python
Python> tuple> Convert double tuple to single tuple
Convert to a string while outputting standard output with Python subprocess
[Python] Convert CSV file uploaded to S3 to JSON file with AWS Lambda
Made it possible to convert PNG to JPG with Pillow of Python
Convert files written in python etc. to pdf with syntax highlighting
[Python] How to convert db file to csv
Try to reproduce color film with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
HTML email with image to send with python
Convert Python> two value sequence to dictionary
Python to remember only with hello, worlds