#!/usr/local/bin/ruby -Ke

# 全角記号の半角化と pdic風レイアウト変換
def full2half(s)
  if s
    # {ひらがな}の除去
    s = s[0..s.index(' : ')] + s[s.index(' : ')..s.length].gsub(/\｛[^｛]*｝/,"") if s.index(' : ')
    s = s.gsub(/＝/, "=")\
    .gsub(/×/, "X")\
    .gsub(/％/, "%")\
    .gsub(/＿/, "_")\
    .gsub(/？/, "?")\
    .gsub(/（/, "(")\
    .gsub(/）/, ")")\
    .gsub(/［/, "[")\
    .gsub(/］/, "]")\
    .gsub(/\s*\/\s*【用例/, "\t【用例")
    if s.index(' : ')
      if s[0..s.index(' : ')] =~ /(\{[^\}]*\})/
	y = $`
	x = $1.gsub(/\{/,"\t【")
	x = x.gsub!(/}/,'】')
	s = y + ' : ' + x + s[(s.index(' : ')+' : '.length)..s.length]
      end
    end
    s.gsub!(/  +/, " ")
    #.gsub!(/#{" \t"}/,"\t")
  end
  return s.strip
end

# 用例の移動
def yourei_idou(s)
  if s =~ /【用例/
    ary = s.split("\t")
    ary1 = []
    ary2 = []
    ary.each{|x| 
      if x =~ /^【用例/
	ary2.push(x)
      else
	ary1.push(x)
      end
    }
    ss = ""
    ary1.each{|x|
      ss << x + "\t"
    } 
    ary2.each{|x| 
      ss << x + "\t"
    } 
    return ss
  else
    return s
  end
end

# 機種依存コードの変換
def kishu_izon(s)
  s.gsub(/\x87\x92/s, CNV_CHR3)\
  .gsub(/\x87\x54/s, "I")\
  .gsub(/\x87\x55/s, "II")\
  .gsub(/\x87\x56/s, "III")\
  .gsub(/\x87\x57/s, "IV")\
  .gsub(/\x87\x58/s, "V")\
  .gsub(/\x87\x59/s, "VI")\
  .gsub(/\x87\x5a/s, "VII")\
  .gsub(/\x87\x5b/s, "VIII")\
  .gsub(/\x87\x5c/s, "IX")\
  .gsub(/\x87\x5d/s, "X")\
  .gsub(/\x87\x70/s, "cm")\
  .gsub(/\xee\x9d/s, CNV_CHR2)\
  .gsub(/#{"[\x84\xbf-\x88\x9d]"}/s, CNV_CHR)
end

require 'kconv'
require "jcode"
require "nkf"
$KCODE = "EUC"

CNV_CHR = Kconv::kconv('〓', Kconv::SJIS, Kconv::EUC)
# とう小平　語彙の先頭あってbinary検索不可
CNV_CHR2 = Kconv::kconv('とう', Kconv::SJIS, Kconv::EUC)
CNV_CHR3 = Kconv::kconv('∫', Kconv::SJIS, Kconv::EUC)

ps = kishu_izon(ARGF.gets)
sold = full2half(NKF.nkf('-S -e -Lu', ps))
sold =~ / : /
key = $`
i = j = 0
while pps = ARGF.gets
  ps = kishu_izon(pps)
  s = full2half(NKF.nkf('-S -e -Lu', ps))
  if s =~ / : /
    midashi = $`
    imi = $'
    if key == midashi 
      j+=1
      sold << imi
    else
      if j == 0
	$stdout.puts(sold.sub(/ : \t/, " : "))
      else
	$stdout.puts(yourei_idou(sold))
      end
      j=0
      key = midashi
      sold = s
    end
  end
end
if j == 0
  $stdout.puts(sold.sub(/ : \t/, " : "))
else
  $stdout.puts(yourei_idou(sold))
end
