I want to separate dakuten with Ruby

Question: Question:

I would like to separate the dakuten from hiragana in Ruby, but could you please tell me what kind of implementation is possible?


a = remove_dakuten('だ')
puts a
\# た゛

Answer: Answer:

If you want to do Unicode-like processing, you can separate the voiced sound by NFD normalization.

"だ".unicode_normalize(:nfd) #=> 「た」+U+3099

However, this separated dakuten character (U + 3099) is a special character for combining with the previous character, so if you want to make it a normal dakuten character (U + 309B), you can convert it as follows. increase.

"だ".unicode_normalize(:nfd).tr("\u3099", "\u309b") #=> 「た」+「゛」

To do the same for the semi-voiced sound mark, look like this.

"ぱ".unicode_normalize(:nfd).tr("\u3099\u309a", "\u309b\u309c") #=> 「は」+「゜」
Scroll to Top