Can sorting Japanese kanji words be done programmatically?

后端 未结 4 1475
春和景丽
春和景丽 2020-12-14 09:19

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

4条回答
  •  天命终不由人
    2020-12-14 09:19

    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!

提交回复
热议问题