Mason
Mason

Reputation: 5339

Can sorting Japanese kanji words be done programmatically?

I've recently discovered, to my astonishment (having never really thought about it before), machine-sorting Japanese proper nouns is apparently not possible.

I work on an application that must allow the user to select a hospital from a 3-menu interface. The first menu is Prefecture, the second is City Name, and the third is Hospital. Each menu should be sorted, as you might expect, so the user can find what they want in the menu.

Let me outline what I have found, as preamble to my question:

  1. The expected sort order for Japanese words is based on their pronunciation. Kanji do not have an inherent order (there are tens of thousands of Kanji in use), but the Japanese phonetic syllabaries do have an order: あ、い、う、え、お、か、き、く、け、こ... and on for the fifty traditional distinct sounds (a few of which are obsolete in modern Japanese). This sort order is called 五十音順 (gojuu on jun , or '50-sound order').

  2. Therefore, Kanji words should be sorted in the same order as they would be if they were written in hiragana. (You can represent any kanji word in phonetic hiragana in Japanese.)

  3. The kicker: there is no canonical way to determine the pronunciation of a given word written in kanji. You never know. Some kanji have ten or more different pronunciations, depending on the word. Many common words are in the dictionary, and I could probably hack together a way to look them up from one of the free dictionary databases, but proper nouns (e.g. hospital names) are not in the dictionary.

So, in my application, I have a list of every prefecture, city, and hospital in Japan. In order to sort these lists, which is a requirement, I need a matching list of each of these names in phonetic form (kana).

I can't come up with anything other than paying somebody fluent in Japanese (I'm only so-so) to manually transcribe them. Before I do so though:

宜しくお願いします。

Upvotes: 25

Views: 13779

Answers (4)

YOU
YOU

Reputation: 123821

For Data, dig Google's Japanese IME (Mozc) data files here.

There is lots of interesting data there, including IPA dictionaries.

Edit:

And you may also try Mecab, it can use IPA dictionary and can convert kanjis to katakana for most of the words

and there is ruby bindings for that too.

and here is somebody tested, ruby with mecab with tagger -Oyomi

Upvotes: 11

Mason
Mason

Reputation: 5339

just a quick followup to explain the eventual actual solution we used. Thanks to all who recommended mecab--this appears to have done the trick.

We have a mostly-Rails backend, but in our circumstance we didn't need to solve this problem on the backend. For user-entered data, e.g. creating new entities with Japanese names, we modified the UI to require the user to enter the phonetic yomigana in addition to the kanji name. Users seem accustomed to this. The problem was the large corpus of data that is built into the app--hospital, company, and place names, mainly.

So, what we did is:

  1. We converted all the source data (a list of 4000 hospitals with name, address, etc) into .csv format (encoded as UTF-8, of course).
  2. Then, for developer use, we wrote a ruby script that:
    1. Uses mecab to translate the contents of that file into Japanese phonetic readings (the precise command used was mecab -Oyomi -o seed_hospitals.converted.csv seed_hospitals.csv, which outputs a new file with the kanji replaced by the phonetic equivalent, expressed in full-width katakana).
    2. Standardizes all yomikata into hiragana (because users tend to enter hiragana when manually entering yomikata, and hiragana and katakana sort differently). Ruby makes this easy once you find it: NKF.nkf("-h1 -w", katakana_str) # -h1 means to hiragana, -w means output utf8
    3. Using the awesomely conveninent new Ruby 1.9.2 version of CSV, combine the input file with the mecab-translated file, so that the resulting file now has extra columns inserted, a la NAME, NAME_YOMIGANA, ADDRESS, ADDRESS_YOMIGANA, and so on.
  3. Use the data from the resulting .csv file to seed our rails app with its built-in values.

From time to time the client updates the source data, so we will need to do this whenever that happens.

As far as I can tell, this output is good. My Japanese isn't good enough to be 100% sure, but a few of my Japanese coworkers skimmed it and said it looks all right. I put a slightly obfuscated sample of the converted addresses in this gist so that anybody who cared to read this far can see for themselves.

UPDATE: The results are in... it's pretty good, but not perfect. Still, it looks like it correctly phoneticized 95%+ of the quasi-random addresses in my list.

Many thanks to all who helped me!

Upvotes: 9

t_motooka
t_motooka

Reputation: 565

I'm not familiar with MeCab, but I think using MeCab is good idea.

Then, I'll introduce another method. If your app is written in Microsoft VBA, you can call "GetPhonetic" function. It's easy to use.

see : http://msdn.microsoft.com/en-us/library/aa195745(v=office.11).aspx


Sorting prefectures by its pronunciation is not common. Most Japanese are used to prefectures sorted by 「都道府県コード」. e.g. 01:北海道, 02:青森県, …, 13:東京都, …, 27:大阪府, …, 47:沖縄県 These codes are defined in "JIS X 0401" or "ISO-3166-2 JP". see (Wikipedia Japanese) : http://ja.wikipedia.org/wiki/%E5%85%A8%E5%9B%BD%E5%9C%B0%E6%96%B9%E5%85%AC%E5%85%B1%E5%9B%A3%E4%BD%93%E3%82%B3%E3%83%BC%E3%83%89

Upvotes: 4

mpenkov
mpenkov

Reputation: 21906

Nice to hear people are working with Japanese.

I think you're spot on with your assessment of the problem difficulty. I just asked one of the Japanese guys in my lab, and the way to do it seems to be as you describe:

  1. Take a list of Kanji
  2. Infer (guess) the yomigana
  3. Sort yomigana by gojuon.

The hard part is obviously step two. I have two guys in my lab: 高橋 and 高谷. Naturally, when sorting reports etc. by name they appear nowhere near each other.

EDIT

If you're fluent in Japanese, have a look here: http://mecab.sourceforge.net/

It's a pretty popular tool, so you should be able to find English documentation too (the man page for mecab has English info).

Upvotes: 5

Related Questions