【Python】シェル上で出力した文字列で濁点が分かれてしまった文字(結合文字)を濁音に直すスクリプト

598 語
3 分
【Python】シェル上で出力した文字列で濁点が分かれてしまった文字(結合文字)を濁音に直すスクリプト

はじまり#

リサちゃん avatar
リサちゃん
ただいマジLOVE1000%~。
135ml avatar
135ml
おかえListen to my heart!!
リサちゃん avatar
リサちゃん
今日さ~、道を歩いてたら側溝のドブに落ちちゃってさ~
135ml avatar
135ml
あれ? なんか、君のセリフの中の濁音が1文字として認識されていないような・・・?
リサちゃん avatar
リサちゃん
え? 本当に? ビビデバビデブー・・・。本当だ! 全部濁音が変だ! (コピペしてみよう!)
135ml avatar
135ml
よし、直すか。
ペンギン avatar
ペンギン
しかし、この記事を移植する過程で、濁音が分かれてしまう現象が再現しなくなってしまった。そんな不具合が再現しなくなった反面、物悲しさも感じた、うp主なのであった。

ツールのソース#

こちらがツールのソースになります。

targetに濁点が別の文字となってしまっている文字列を列挙して、replaceにその文字と同じ順序に修正後の文字列を列挙しています。

class ReplaceCharacter:
def MakeVoicedsound(self, text):
"""
text : String of target text.
"""
target = 'がぎぐげござじずぜぞだぢづでどばびぶべぼぱぴぷぺぽガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポ'
replace = 'がぎぐげござじずぜぞだぢづてどばびぶべぼぱぴぷぺぽガギグゲゴザジズゼゾダヂヅデドバビブベボパピプペポ'
list_target = []
list_replace = []
for i in range(0, len(target), 2):
list_target.append(target[i : i+2])
for i in range(0, len(replace), 1):
list_replace.append(replace[i : i+1])
for i in range(0,len(list_target)):
text = text.replace(list_target[i], list_replace[i])
return text

テストクラスのソース#

今回初めて、テストコードを作って、pytestモジュールでテストさせてみました。

pytestがテストコードを認識できるように、test/__<strong>init__</strong>.pyを作るのを忘れずに。

from TextReplacer import ReplaceCharacter
class Test_ReplaceCharacter:
# Unicode : \u3099
def test_MakeVoicedsound_1_1(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('がぎぐげござじずぜぞ') == 'がぎぐげござじずぜぞ'
def test_MakeVoicedsound_1_2(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('だぢづでどばびぶべぼ') == 'だぢづてどばびぶべぼ'
# Unicode : \u309A
def test_MakeVoicedsound_1_3(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('ぱぴぷぺぽ') == 'ぱぴぷぺぽ'
# Unicode : \u3099
def test_MakeVoicedsound_1_4(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('ガギグゲゴザジズゼゾ') == 'ガギグゲゴザジズゼゾ'
def test_MakeVoicedsound_1_5(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('ダヂヅデドバビブベボ') == 'ダヂヅデドバビブベボ'
# Unicode : \u309A
def test_MakeVoicedsound_1_6(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('パピプペポ') == 'パピプペポ'
# dakuon
def test_MakeVoicedsound_2_1(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('がぎぐげござじずぜぞ') == 'がぎぐげござじずぜぞ'
def test_MakeVoicedsound_2_2(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('だぢづてどばびぶべぼ') == 'だぢづてどばびぶべぼ'
#handakuon
def test_MakeVoicedsound_2_3(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('ぱぴぷぺぽ') == 'ぱぴぷぺぽ'
# dakuon
def test_MakeVoicedsound_2_4(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('ガギグゲゴザジズゼゾ') == 'ガギグゲゴザジズゼゾ'
def test_MakeVoicedsound_2_5(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('ダヂヅデドバビブベボ') == 'ダヂヅデドバビブベボ'
# handakuon
def test_MakeVoicedsound_2_6(self):
replaceCharacter = ReplaceCharacter()
assert replaceCharacter.MakeVoicedsound('パピプペポ') == 'パピプペポ'

おしまい#

135ml avatar
135ml
どうだい? 直ったかね?
リサちゃん avatar
リサちゃん
ビビデバビデブー!
ペンギン avatar
ペンギン
しかし、この記事を移植する過程で、濁音が分かれてしまう現象が再現しなくなってしまった。本記事の中には、既に濁音が分かれてしまう文字は存在しない。そういう不具合が再現しなくなった反面、物悲しさも感じた、うp主なのであった。

以上になります!

記事を共有

この記事が役に立ったなら、ぜひ他の人と共有してください!

【Python】シェル上で出力した文字列で濁点が分かれてしまった文字(結合文字)を濁音に直すスクリプト
https://endorphinbath.com/posts/make-voiced-sound/
著者
kinkinbeer135ml
公開日
2021-10-29
ライセンス
CC BY-NC-SA 4.0
関連記事 スマート
1
【Python】複数の区切り文字を指定して文字列を配列に分割する
Code Pythonで文字列を配列に分割するスクリプトを掲載します。分割文字は配列で指定するように作っています。
2
【Python】文字列の先頭と末尾にあるスペース、空白文字を削除する
Code Pythonで文字列の先頭と末尾にスペース(空白文字)が混じっていることがあります。そのスペースを削除するスクリプトを掲載します。
3
【Python】inputを使った処理をpytestでunittestしたい(monkeypatchでmockする)
Code Pythonスクリプトをpytestする時に、input()のようなビルトイン関数が入っている時にmockする方法を紹介します。monkeypatchを使用します。
4
【Python】pytestで同じディレクトリのモジュールをimportして、"ModuleNotFoundError: No module named"を出さなくする
Code Pythonスクリプトをpytestするとき、"ModuleNotFoundError: No module named"が表示されてしまった場合、この記事の方法でそのエラーが解決するかも。
5
【Python】Pydanticのvalidatorが非推奨だからfield_validatorを使って2段階バリデーションを実装する
Code @validatorはdeprecatedになってるし、@field_validatorにはpreとかalwaysフラグが無いから、from pydantic.functional_validators import field_validatorでインポートしたfield_validatorを使用する方法と、BeforeValidatorとAfterValidatorをAnnotateの中に入れる実装方法を試した。
ランダム記事 ランダム
Profile Image of the Author
kinkinbeer135ml
SIerをやめて、プログラミングを勉強しています。※Amazonアソシエイトに参加しています。
お知らせ
私のブログへようこそ!これはサンプルのお知らせです。
音楽
カバー

音楽

再生中なし

0:00 0:00
歌詞なし
カテゴリ
タグ
サイト統計
記事
287
カテゴリー
8
タグ
93
総文字数
486,174
運用日数
0
最終活動
0 日前

目次