summaryrefslogtreecommitdiff
path: root/derivation_to_json/rec.rb
blob: 677a02addcbfff3e0cb6c5562f4bf978e47c63b1 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env ruby

require 'json'
require 'zipf'


before = JSON.parse(ReadFile.read('x.json'))
after = JSON.parse(ReadFile.read('y.json'))

alignment = {}
after["align"].each { |i|
  a,b = i.split '-'
  a = a.to_i
  b = b.to_i
  if alignment[a]
    alignment[a] << b
  else
    alignment[a] = [b]
  end
}

srg2idx = {}
before['source_rgroups'].uniq.each { |k|
  srg2idx[k] = []
  before['source_rgroups'].each_with_index { |i,j|
    if i==k
      srg2idx[k] << j
    end
  } 
}

srg2idx.each_pair { |k,v|
  a = []
  tgt = []
  v.each { |i|
    a << after["source"][i]
    tgt << after["target"][alignment[i].first]
  }
  rule_before = before['rules_by_span_id'][k.to_s]
  src_side_before = splitpipe(rule_before)[1]
  x = src_side_before.split
  a.first.insert(0, " [X] ") if x[0] == "[X]"
  a[a.size-1] += " [X] " if x[x.size-1] == "[X]"
  puts rule_before
  puts "#{k} #{a.join " [X] "}"
  puts tgt.to_s
  puts "---"
  puts
}