# prac1101.rb

class TRYnode
  attr_accessor :data
  attr_accessor :arc

  def initialize
    @data = nil
    @arc = Array.new(256,nil)
  end
end


class TRY
  def initialize
    @root = TRYnode.new
  end

  def entry_word( word, mean )
    n = @root
    word.size.times{|i|
        hlc = word[i]
        if n.arc[hlc] == nil
            n.arc[hlc] = TRYnode.new
        end
        n = n.arc[hlc]
    }
    n.data = mean.dup
  end

  def load_dictionary( filename )
    fp = File::open(filename,'r')
    fp.each{|line|
      if /^([^\s]+)\s+([^\n]+)$/ =~ line
	self.entry_word( $1, $2 )
      end
    }
  end

  def show( n = @root, str = "" )
    if n.data != nil
        print str+","+n.data+"\n"
    else
	for i in 0...256 do
	   if n.arc[i] != nil
	      tmp = str + " "
	      tmp[str.size] = i
	      show( n.arc[i], tmp )
           end
        end
    end
  end
end

# メインルーチン

dic = TRY.new
dic.load_dictionary('dictionary1.dic')
dic.show