KAKASI | japanese to romaji converter
mouse 430 · person cloud · link
Last update
2017-07-26
2017
07-26
« — »
1
2
3
4
5
6
7
8
9
10
aptitude install kakasi

cat document.txt | \
  kakasi -Ha -Ka -Ja -Ea -ka -ja -ga \  # convert all to ascii/romaji
         -s              \  # insert space between words
         -c              \  # skip special chars (TAB,CR,LF,BLANK)
         -i utf8 -o utf8 \  # input/output charset
         -rhepburn       \  # hepburn translation method
         -p              \  # display all possibile readings
         -f                 # show original kanji+translation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
gem 'kakasi' # add line to Gemfile

# make a useful string method
module Utils::String
  KAKASI_OPTS = %w{ -Ha -Ka -Ja -Ea -ka -ja -ga -s -c -rhepburn }

  def to_romaji(kakasi_opts = [])
    enc_opts = {invalid: :replace, undef: :replace, replace: '_'}

    Kakasi.kakasi(
      (KAKASI_OPTS+kakasi_opts).join(' '),
      self.encode(Encoding::CP932, enc_opts) # fix UTF-8 to Windows-31J (Encoding::UndefinedConversionError)
    ).encode(Encoding::UTF_8, enc_opts) # reencode to UTF8 for further processing
  end # to_romaji --------------------------------------------------------------
end

String.send(:include, ::Utils::String)

Source: KAKASI Hp, manpage, ruby gem