0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

タイトルがいきなり何かの暗号にみえてしまった人がいるかもしれません。

これは ICU の Transliteration(翻字)を使ってカタカナをひらがなに翻字してさらにローマ字に翻字した上でデフラグさんらしく並べ替えた処理を行った後のものです。

ソースコードをペタっと貼っつけておきます。実行するには cmake と ICU が必要です。OSX なら macports で両方共インストール可能です。

project(transliterate)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
find_library(ICU_DATA_LIBRARY icudata)
find_library(ICU_UC_LIBRARY icuuc)
find_library(ICU_I18N_LIBRARY icui18n)
find_library(ICU_IO_LIBRARY icuio)
find_path(ICU_INCLUDE_DIR unicode/unistr.h)
target_link_libraries(${PROJECT_NAME} ${ICU_IO_LIBRARY} ${ICU_I18N_LIBRARY} ${ICU_UC_LIBRARY} ${ICU_DATA_LIBRARY})
include_directories(${ICU_INCLUDE_DIR})

ソースコードちょっと長く、書き捨て前提で書いてあります。std::map がツリー形式で構築されて自動的にソートされる仕様を利用しているので、ソート処理は省いています。

# include <iostream>
# include <map>
# include <unicode/translit.h>
# include <unicode/ustream.h>

int main(int argc, char *argv[])
{
    if (argc > 1) {
        /* construct UnicodeString from first argument and print it */
        UnicodeString string(UnicodeString::fromUTF8(argv[1]));
        std::cerr << string << std::endl;
        /* transliate Katakana to Hiragana */
        UErrorCode status = U_ZERO_ERROR;
        Transliterator *k2h = Transliterator::createInstance("Katakana-Hiragana", UTRANS_FORWARD, status);
        k2h->transliterate(string);
        std::cerr << string << std::endl;
        /* transliate Hiragana to Roman (Latin) */
        status = U_ZERO_ERROR;
        Transliterator *h2r = Transliterator::createInstance("Hiragana-Latin", UTRANS_FORWARD, status);
        h2r->transliterate(string);
        /* split UnicodeString into array of UChar */
        typedef std::map<UChar, int> UCharMap;
        UCharMap chars;
        for (int i = 0, length = string.length(); i < length; i++) {
            UChar c = string.charAt(i);
            UCharMap::iterator it = chars.find(c);
            if (it != chars.end()) {
                it->second++;
            }
            else {
                chars.insert(std::make_pair(c, 1));
            }
        }
        /* sort and make char (downcast from UChar) upper */
        UCharMap::const_iterator it = chars.begin();
        while (it != chars.end()) {
            std::cerr << char(toupper(it->first)) << it->second;
            it++;
        }
        std::cerr << std::endl;
    }
    return 0;
}

無事にビルドに成功すると以下のように第一引数に変換したいカタカナ文字列を渡すことで最終出力がタイトルにあるような形式で出力されます。

./transliterate インターナショナルコンポーネンツフォーユニコード
# インターナショナルコンポーネンツフォーユニコード
# いんたあなしょなるこんぽおねんつふぉおゆにこおど
# A4D1E1F1H1I2K2N7O9P1R1S2T2U3Y1

余談ですがこのソースコードは普段使用している QtCreator 2.7 を使って書いています。C++ は勿論、割と最近 cmake のキーワード補完が効くようになったので便利です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?