开发者

How can I convert all Japanese hiragana to katakana characters in Python?

开发者 https://www.devze.com 2023-02-08 07:51 出处:网络
From hiragana and katakana charts, it looks like it should be possible to \"normalize\" japanese text into hiragana or katakana. It\'s pretty straight-forward to build a table and implement a dictiona

From hiragana and katakana charts, it looks like it should be possible to "normalize" japanese text into hiragana or katakana. It's pretty straight-forward to build a table and implement a dictionary/regex table for search/replace. Does anyone kno开发者_如何学运维w where the work's already been done?


Why would you want to do this though? Katakana is traditionally used for words borrowed from other languages, while hiragana is used for the Japanese native language. By normalizing the japanese text to one form or another you could actually be hindering the reading of it (at least to me it would be harder since I am loosing context by having it normalized).

But in answer to your question, this seems to do what your asking: JCONV


You could do what you want to do very quickly using str.translate.

However it is not readily apparent why you would want to do that.

What I would call normalising in a language written in a Latin-based alphabet would include lowercasing, normalising whitespace, and stripping accents etc so that the result was ASCII. The purpose of doing that would be not for display but for comparing user-entered text in some kind of fuzzy search/match/lookup scenario. The point being that mistakes of accent etc are quite common even with native writers of the languages in question.

Given the role that Hiragana plays in the Japanese writing system (words often have a Kanji stem and Hiragana suffixes) I can't imagine any use for changing Hiragana characters to Katakana ... please enlighten me.


Here is a way without loading an extra package. The range of commonly used hiragana in UTF-8 is from 3041 to 3096. The range for katakana is from 30a1 to 30f6.

Hence do the following:

hira_start = int("3041", 16)
hira_end = int("3096", 16)
kata_start = int("30a1", 16)

hira_to_kata = dict()
for i in range(hira_start, hira_end+1):
    hira_to_kata[chr(i)] = chr(i-hira_start+kata_start)
    print(chr(i), chr(i-hira_start+kata_start))
0

精彩评论

暂无评论...
验证码 换一张
取 消