Convert from katakana to vowel kana [python]

Overview

I made a function that returns a vowel when a katakana character string is input, such as "Konichiha-> Onia".

Conversion rules

Leave "n" and "tsu" as they are (Example: Ludicolo-> Unaa) Make "-" (long sound) the same as the previous vowel (example: cola-> oa) If the immediately preceding kana is "to" or "do", "u" is regarded as one character and treated in the same way as uppercase letters (example: Ducchi-> Uui, Uaa-> Uaa). "Ya", "e", and "yo" are treated as one character when the immediately preceding kana is in the I stage, and in other cases, they are treated in the same way as uppercase letters. (Example: Catatsu-> Aau, Kee-> E) "U" is regarded as one kana if the immediately preceding kana is "i", "te" and "de", and is treated as a capital letter in other cases (example: tulle-> u, dewar-> dewar). ) If the immediately preceding kana is U-dan, "ヮ", "a", "e", and "o" are treated as one kana together with it, and in other cases, they are treated as uppercase letters. (Example: Way-> Aei) If the previous kana is U-dan and "te" or "de", "i" is regarded as one kana, and in other cases, it is treated as an uppercase letter (eg Lemon Tea-> Eonii, IKEA-> IKEA) Leave the characters other than katakana as they are.

environment

It has been confirmed to run on Google Colaboratory (as of March 27, 2020) and macOS Catalina, Python 3.8.0.

code

def kana2vowel(text):
    #Uppercase and u conversion list
    large_tone = {
        'A' :'A', 'I' :'I', 'C' :'C', 'D' :'D', 'Oh' :'Oh',
        'U': 'C', 'Vu': 'C',
        'Mosquito' :'A', 'Ki' :'I', 'Ku' :'C', 'Ke' :'D', 'Ko' :'Oh',
        'Service' :'A', 'Shi' :'I', 'Su' :'C', 'Se' :'D', 'So' :'Oh',
        'Ta' :'A', 'Ji' :'I', 'Tsu' :'C', 'Te' :'D', 'To' :'Oh',
        'Na' :'A', 'D' :'I', 'Nu' :'C', 'Ne' :'D', 'No' :'Oh',
        'C' :'A', 'Hi' :'I', 'Fu' :'C', 'F' :'D', 'E' :'Oh',
        'Ma' :'A', 'Mi' :'I', 'Mu' :'C', 'Me' :'D', 'Mo' :'Oh',
        'Ya' :'A', 'Yu' :'C', 'Yo' :'Oh',
        'La' :'A', 'Li' :'I', 'Le' :'C', 'Re' :'D', 'B' :'Oh',
        'Wow' :'A', 'Wo' :'Oh', 'Down' :'Down', 'Vu' :'C',
        'Moth' :'A', 'Gi' :'I', 'Gu' :'C', 'Ge' :'D', 'Go' :'Oh',
        'The' :'A', 'The' :'I', 'Zu' :'C', 'Ze' :'D', 'Zo' :'Oh',
        'Da' :'A', 'Di' :'I', 'Zu' :'C', 'De' :'D', 'Do' :'Oh',
        'Ba' :'A', 'Bi' :'I', 'Bu' :'C', 'Be' :'D', 'Bo' :'Oh',
        'Pacific League' :'A', 'Pi' :'I', 'Pu' :'C', 'Pe' :'D', 'Po' :'Oh'
    }

    #To/Do+'U'To c
    for k in 'Steller sea lion':
        while k+'U' in text:
            text = text.replace(k+'U','C')
    #Te/De+I/I/Convert to c
    for k in 'Tedde':
        for k2,v in zip('Ju','Iu'):
            while k+k2 in text:
                text = text.replace(k+k2,v)

    #Convert uppercase letters and u to vowels
    text = list(text)
    for i, v in enumerate(text):
        if v in large_tone:
            text[i] = large_tone[v]
    text = ''.join(text)

    #Convert Wu to Wu
    while 'Wu' in text:
        text = text.replace('Wu','U')

    #C+ヮ/A/I/E/Convert o to vowel
    for k,v in zip('ヮ yeo','Aieo'):
        text = text.replace('C'+k,v)
    
    #E/Good/Convert to good
    for k in 'Yeah':
        while k+'-' in text:
            text = text.replace(k+'-',k+'I')

    #I/I+Turbocharger/Yu/E/Convert yo to vowel
    for k,v in zip('Nyayo','Aueo'):
        text = text.replace('I'+k, v).replace('I'+k, v)
    
    #Convert the remaining lowercase letters to vowels
    for k,v in zip('Eyayo','Aiaioauo'):
        text = text.replace(k,v)
    
    #-Convert (long sound) to vowel
    for k in 'a-I-U-E-O':
        while k+'-' in text:
            text = text.replace(k+'-',k+k)
    
    return text

Recommended Posts

Convert from katakana to vowel kana [python]
Convert from Markdown to HTML in Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Convert kanji to kana
[Python] Convert from DICOM to PNG or CSV
[python] Convert date to string
Post from Python to Slack
[Python] Convert list to Pandas [Pandas]
Cheating from PHP to Python
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Convert python 3.x code to python 2.x
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
Convert markdown to PDF in Python
Create folders from '01' to '12' with python
[Lambda] [Python] Post to Twitter from Lambda!
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Python> list> Convert double list to single list
Convert from pdf to txt 2 [pyocr]
Connect to utf8mb4 database from python
Python (from first time to execution)
Post images from Python to Tumblr
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
How to access wikipedia from python
Python to switch from another language
How to convert from .mgz to .nii.gz
Python> tuple> Convert double tuple to single tuple
Did not change from Python 2 to 3
Update Python on Mac from 2 to 3
[Python] Convert from NIFITI to PNG (COVID-19 [new coronavirus] CT example)
[Python] How to convert db file to csv
[Python] Fluid simulation: From linear to non-linear
From Python to using MeCab (and CaboCha)
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to convert a 2D list to a 1D list
How to update Google Sheets from Python
[Python] Convert csv file delimiters to tab delimiters
Convert from PDF to CSV with pdfplumber
Send a message from Python to Slack
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
I want to use jar from python
Push notification from Python server to Android
Convert FX 1-minute data to 5-minute data with Python
Connecting from python to MySQL on CentOS 6.4
python> Convert tuple to list> aList = list (pi_tuple)
Porting and modifying doublet-solver from python2 to python3.
How to access RDS from Lambda (python)
Convert Python date types to RFC822 format
Python> Output numbers from 1 to 100, 501 to 600> For csv
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
[Amazon Linux] Switching from Python 2 series to Python 3 series
API explanation to touch mastodon from python