How to determine if a character is a Chinese character using ruby?
Asked
Active
Viewed 6,072 times
10
-
They usually have more strokes than katakana or hiragana. And you're generally only supposed to use ruby on the more complex kanji ... wait a moment, is this Japanese.SE or stack overflow? – Andrew Grimm Mar 29 '12 at 21:52
2 Answers
18
Ruby 1.9
#encoding: utf-8
"漢" =~ /\p{Han}/

steenslag
- 79,051
- 16
- 138
- 171
-
I use this code,but it's can't work。 This is error info:invalid character property name {Han}: /\p{Han}/ – HelloWorld Apr 28 '10 at 09:03
-
1@HelloWorld: Update your version of Ruby. All characters classes are documented now: http://github.com/ruby/ruby/blob/trunk/doc/re.rdoc (cool nick, BTW) – Marc-André Lafortune Apr 28 '10 at 12:32
-
The link above is broken, but you can find all information in the ruby docs for regexp: http://www.ruby-doc.org/core-2.0.0/Regexp.html#label-Character+Properties – deRailed Sep 21 '13 at 10:58
-
If you're getting "invalid character property name {Han}", you can sometimes solve this by adding /u: `"漢" =~ /\p{Han}/u` – Sprachprofi Dec 17 '16 at 11:18
7
An interesting article on encodings in Ruby: http://blog.grayproductions.net/articles/bytes_and_characters_in_ruby_18 (it's part of a series - check the table of contents at the start of the article also)
I haven't used chinese characters before but this seems to be the list supported by unicode: http://en.wikipedia.org/wiki/List_of_CJK_Unified_Ideographs . Also take note that it's a unified system including Japanese and Korean characters (some characters are shared between them) - not sure if you can distinguish which are Chinese only.
I think you can check if it's a CJK character by calling this on string str and character with index n:
def check_char(str, n)
list_of_chars = str.unpack("U*")
char = list_of_chars[n]
#main blocks
if char >= 0x4E00 && char <= 0x9FFF
return true
end
#extended block A
if char >= 0x3400 && char <= 0x4DBF
return true
end
#extended block B
if char >= 0x20000 && char <= 0x2A6DF
return true
end
#extended block C
if char >= 0x2A700 && char <= 0x2B73F
return true
end
return false
end

Andrei Fierbinteanu
- 7,656
- 3
- 31
- 45
-
@sam they are the CJK ranges. These are the Chinese, Japanese, and Korean characters (assuming the ranges are correct, which I believe they are) – Michael Lowman Jan 06 '11 at 20:33
-
@Michael Lowman, they returned false for a few characters I tested using Korean, Japanese and all 1..9 a..z not to mention they do return true for Chinese. How could I go about checking if the character is traditional or simplified. – thenengah Jan 06 '11 at 20:39
-
-
1On the mentioned wikipedia page each of the blocks has a list of charts with the characters it contains. I used those ranges. – Andrei Fierbinteanu Jan 07 '11 at 09:10
-
is it possible to distinguish between traditional and simplified forms? – thenengah Jan 07 '11 at 21:02
-
Not very easily, but this library does this trick. Ruby 1.9+ only. https://github.com/jpatokal/script_detector – lambshaanxy Jun 04 '12 at 01:30