表題のことをPythonで行おうとあれこれ検索しましたが、なかなかこれだという方法が見つかっていません。
今のところ、Is there a way to know whether a Unicode string contains any Chinese/Japanese character in Python? - Stack Overflowで紹介されているunicodedata.name()
という関数を使って、以下のような判定関数を書いて使っています。
def is_japanese(string): for ch in string: name = unicodedata.name(ch) if "CJK UNIFIED" in name \ or "HIRAGANA" in name \ or "KATAKANA" in name: return True return False
この関数は、文字列stringに1文字でも「ひらがな」「カタカナ」「漢字」のどれかが含まれていればTrueを返します。欠点として、stringが中国でしか使われていない漢字のみからなる文字列であってもTrueになってしまいます。
これは、unicodedata.name()
が、以下のような文字列を返す関数であることを利用しています。
>>> import unicodedata >>> unicodedata.name('你') 'CJK UNIFIED IDEOGRAPH-4F60' >>> unicodedata.name('桜') 'CJK UNIFIED IDEOGRAPH-685C' >>> unicodedata.name('あ') 'HIRAGANA LETTER A' >>> unicodedata.name('ア') 'KATAKANA LETTER A' >>> unicodedata.name('a') 'LATIN SMALL LETTER A'