Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating Kana Input

I am working on an application that allows users to input Japanese language characters. I am trying to come up with a way to determine whether the user's input is a Japanese kana (hiragana, katakana, or kanji).

There are certain fields in the application where entering Latin text would be inappropriate and I need a way to limit certain fields to kanji-only, or katakana-only, etc.

The project uses UTF-8 encoding. I don't expect to accept JIS or Shift-JIS input.

Ideas?

like image 729
Zack The Human Avatar asked Dec 10 '22 22:12

Zack The Human


2 Answers

It sounds like you basically need to just check whether each Unicode character is within a particular range. The Unicode code charts should be a good starting point.

If you're using .NET, my MiscUtil library has some Unicode range support - it's primitive, but it should do the job. I don't have the source to hand right now, but will update this post with an example later if it would be helpful.

like image 146
Jon Skeet Avatar answered Feb 01 '23 05:02

Jon Skeet


Not sure of a perfect answer, but there is a Unicode range for katakana and hiragana listed on Wikipedia. (Which I would expect are also available from unicode.org as well.)

  • Hiragana: Unicode: 3040-309F
  • Katakana: Unicode: 30A0–30FF

Checking those ranges against the input should work as a validation for hiragana or katakana for Unicode in a language-agnostic manner.

For kanji, I would expect it to be a little more complicated, as I expect that the Chinese characters used in Chinese and Japanese are both included in the same range, but then again, I may be wrong here. (I can't expect that Simplified Chinese and Traditional Chinese to be included in the same range...)

like image 27
coobird Avatar answered Feb 01 '23 06:02

coobird