summaryrefslogtreecommitdiff
path: root/test/test_hg.rb
blob: 420d77f3b48454ebacda1ea6c2987a2ff166d465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env ruby

require_relative '../hg'


def main
  # viterbi
  semiring = ViterbiSemiring.new
  hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/toy/toy.json', semiring, true)
  #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/toy/toy-test.json', semiring, true)
  #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/glue/glue.json', semiring, true)
  #hypergraph, nodes_by_id = HG::read_hypergraph_from_json('../example/3/3.json', semiring, true)
  path, score = HG::viterbi_path hypergraph, nodes_by_id[-1], semiring
  s = HG::derive path, path.last.head, []
  puts "#{s.map { |i| i.word }.join ' '}"
  puts Math.log score
  puts

  # all paths
  hypergraph.reset
  paths = HG::all_paths hypergraph, nodes_by_id[-1]
  paths.each_with_index { |p,i|
    s = HG::derive p, p.last.head, []
    puts "#{i+1}. #{s.map { |x| x.word }.join ' '}"
  }
end


main