2019年10月31日木曜日

Python ローマ字 カナ変換

#!/usr/bin/env python36
# coding=utf-8
import json
import requests

def main():
  import io, sys
  import getopt
  from   pykakasi import kakasi
  import mojimoji


  usage =""" usage: ./kakasi.py [options]
  --help            本メッセージ
  --zipcode         郵便番号"""

  try:
    opts, args = getopt.getopt( sys.argv[1:], '-h', ['help','zipcode='])
  except getopt.GetoptError as err:
    print( str(err))
    print( usage )
    sys.exit()

  opt = dict( opts )

  if( '-h' in opt or len(opt)>1 ):
    print( usage )
    sys.exit()
  elif( '--zipcode' in opt ):
    zip= opt['--zipcode']
  else:
    zip = '1510061'

  print( 'REQUEST: ' +  zip)

  resp = requests.get( 'http://zipcloud.ibsnet.co.jp/api/search?zipcode=' + str(zip) + '&limit=1')

  response = json.loads(resp.text)
  address = response["results"][0]

  address1 = address["address1"]
  address2 = address["address2"]
  address3 = address["address3"]
  kana1    = address["kana1"]
  kana2    = address["kana2"]
  kana3    = address["kana3"]
  zipcode  = address["zipcode"]

  print( '---------取得データ---------')
  print( address1 )
  print( address2 )
  print( address3 )
  print( kana1 )
  print( kana2 )
  print( kana3 )
  print( zipcode )


  kakasi = kakasi()  # Generate kakasi instance

  print( '---------ローマ字変換--------')

  kakasi.setMode("H", "a")  # Hiragana to ascii
  kakasi.setMode("K", "a")  # Katakana to ascii
  kakasi.setMode("J", "a")  # Japanese(kanji) to ascii

  kakasi.setMode("r", "Hepburn")  # Use Hepburn romanization

  conv = kakasi.getConverter()
  roma_address1  = conv.do( address1 )
  roma_address2  = conv.do( address2 )
  roma_address3  = conv.do( address3 )
  roma_kana1     = conv.do( kana1 )
  roma_kana2     = conv.do( kana2 )
  roma_kana3     = conv.do( kana3 )
  roma_zipcode   = conv.do( zipcode )
  print( roma_address1 )
  print( roma_address2 )
  print( roma_address3 )
  print( roma_kana1 )
  print( roma_kana2 )
  print( roma_kana3 )
  print( roma_zipcode )

  print( '----------カナ変換---------')
  kakasi.setMode("H", "K")  # Hiragana to KANA
  kakasi.setMode("J", "K")  # Japanese(kanji) to KANA
  kakasi.setMode("a", "K")  # Japanese(kanji) to KANA

  conv = kakasi.getConverter()
  kana_address1  = conv.do( address1 )
  kana_address2  = conv.do( address2 )
  kana_address3  = conv.do( address3 )
  kana_kana1     = conv.do( kana1 )
  kana_kana2     = conv.do( kana2 )
  kana_kana3     = conv.do( kana3 )
  kana_zipcode   = conv.do( zipcode )
  print( kana_address1 )
  print( kana_address2 )
  print( kana_address3 )
  print( kana_kana1 )
  print( kana_kana2 )
  print( kana_kana3 )
  print( kana_zipcode )

  print( '--------全角変換------------')
  print( mojimoji.han_to_zen( kana_kana1) )
  print( mojimoji.han_to_zen( kana_kana2) )
  print( mojimoji.han_to_zen( kana_kana3) )

  print( '--------全角→ローマ字変換------------')

  kakasi.setMode("H", "a")  # Hiragana to ascii
  kakasi.setMode("K", "a")  # Katakana to ascii
  kakasi.setMode("J", "a")  # Japanese(kanji) to ascii

  kakasi.setMode("r", "Hepburn")  # Use Hepburn romanization
  print( conv.do( mojimoji.han_to_zen( kana_kana1) ))
  print( conv.do( mojimoji.han_to_zen( kana_kana2) ))
  print( conv.do( mojimoji.han_to_zen( kana_kana3) ))

if __name__ == "__main__":
    main()

0 件のコメント:

コメントを投稿

zabbix7 amazon linux2023 インストール postgres15

【postgres】 dnf -y install postgresql15-server postgresql15-server-devel postgresql-setup initdb passwd postgres vi `find / -name pg_hba.con...