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.
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)
--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