Convert Hiragana to Romaji with Python (Beta)

At first

There is a Japanese to Romaji conversion module called PyKakasi, so if you are busy, we recommend you to install it.

――I want to write it myself! --Not enough to use modules ――I want to customize it later

It is an article for those who say.

Development environment

code

chars_dict.py


hiragana = {

    # vowel
    'Ah': 'a',
    'I': 'i',
    'U': 'u',
    'e': 'e',
    'O': 'o',

    # consonant K
    'Or': 'ka',
    'Ki': 'ki',
    'Ku': 'ku',
    'Ke': 'ke',
    'This': 'ko',

    # consonant K"
    'But': 'ga',
    'Gi': 'gi',
    'Gu': 'gu',
    'Ge': 'ge',
    'Go': 'go',
    
    ...

}

main.py


from .chars_dict import hiragana as dict


def hiragana_to_romaji(hiragana):
    romaji = ''
    for char in hiragana:
        if char in dict:
            romaji += dict[char] + '-'
        else:
            romaji = 'ERROR_'
            break
    romaji = romaji[:-1]

    return romaji


hiragana = input()
romaji = hiragana_to_romaji(hiragana)
print(romaji)

run


Hello# hiragana = input()
ko-n-ni-chi-ha  # print(romaji)

Hello World# hiragana = input()
ERROR  # print(romaji)

Points to be improved

--Improved because it is necessary to write symmetric data of hiragana and romaji in dictionary type in advance. --I haven't written the processing when lowercase letters (tsu, ya, etc.) are included (a little troublesome) --It does not support katakana (solved by adding dictionary data)

Recommended Posts

Convert Hiragana to Romaji with Python (Beta)
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
Convert Chinese numerals to Arabic numerals 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
How to convert JSON file to CSV file with Python Pandas
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