型注釈を追加し、関数の戻り値を明示化。コードの可読性と型安全性を向上。

This commit is contained in:
misyaguziya
2025-10-09 17:07:21 +09:00
parent 7255722b67
commit 7d24b3839c
3 changed files with 16 additions and 13 deletions

View File

@@ -1,5 +1,7 @@
# katakana_to_hepburn.py
# カタカナ -> ヘボン式ローマ字(パッケージ不要)
from typing import List
def katakana_to_hepburn(kata: str, use_macron: bool = True) -> str:
"""
@@ -8,7 +10,7 @@ def katakana_to_hepburn(kata: str, use_macron: bool = True) -> str:
use_macron=False のときは単純に連続母音を残す(例: ou, oo
"""
# 基本音の対応(主要なカタカナ)
base = {
base: dict = {
'':'a','':'i','':'u','':'e','':'o',
'':'ka','':'ki','':'ku','':'ke','':'ko',
'':'sa','':'shi','':'su','':'se','':'so',
@@ -31,7 +33,7 @@ def katakana_to_hepburn(kata: str, use_macron: bool = True) -> str:
}
# 拡張:子音 + 小ャユョ の組合せ(主要なもの)
digraphs = {
digraphs: dict = {
('',''):'kya', ('',''):'kyu', ('',''):'kyo',
('',''):'gya', ('',''):'gyu', ('',''):'gyo',
('',''):'sha', ('',''):'shu', ('',''):'sho',
@@ -49,8 +51,8 @@ def katakana_to_hepburn(kata: str, use_macron: bool = True) -> str:
# F-sounds (ファ フィ フェ フォ)
('',''):'fa', ('',''):'fi', ('',''):'fe', ('',''):'fo',
# シェ チェ ティ etc.
('',''):'she', ('',''):'che',
('',''):'ti', ('',''):'tu', ('',''):'du',
('',''):'she', ('',''):'che',
('',''):'ti',
('',''):'wa', ('',''):'wi', ('',''):'we', ('',''):'wo',
# その他外来語によくある組合せ
('',''):'si', ('',''):'zi', ('',''):'tsa', ('',''):'tsi', ('',''):'tse', ('',''):'tso',
@@ -78,7 +80,7 @@ def katakana_to_hepburn(kata: str, use_macron: bool = True) -> str:
return rom # 母音がないなら全部
# 変換メイン
res = []
res: List[str] = []
i = 0
kata = kata.strip()
length = len(kata)