summaryrefslogtreecommitdiff
path: root/grammar.rb
blob: a674a7bce4cc2fa66a65829ee77eb7891970e746 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require 'nlp_ruby'


class T
  attr_accessor :word

  def initialize word
    @word = word
  end

  def to_s
    "T<#{@word}>"
  end
end

class NT
  attr_accessor :symbol, :index, :span

  def initialize symbol, index=0
    @symbol = symbol
    @index = index
    @span = Span.new
  end

  def to_s
    "NT(#{@span.left},#{@span.right})<#{@symbol},#{@index}>"
  end
end



class Rule
  attr_accessor :lhs, :rhs

  def initialize lhs=nil, rhs=[]
    @lhs = lhs
    @rhs = rhs
  end

  def to_s
    "#{lhs} -> #{rhs.map{ |i| i.to_s }.join ' '} [arity=#{arity}]"
  end

  def arity
    rhs.select { |i| i.class == NT }.size
  end

  def from_s s
    _ = splitpipe s, 3
    @lhs = NT.new _[0].strip.gsub!(/(\[|\])/, "")
    _[1].split.each { |x|
      x.strip!
      if x[0]=='[' && x[x.size-1] == ']'
        @rhs << NT.new(x.gsub!(/(\[|\])/, "").split(',')[0])
      else
        @rhs << T.new(x)
      end
    }
  end

  def self.from_s s
    r = self.new
    r.from_s s
    return r
  end
end

class Grammar
  attr_accessor :rules, :startn, :startt, :flat

  def initialize fn
    @rules = []; @startn = []; @startt = [] ;@flat = []
    ReadFile.readlines_strip(fn).each_with_index { |s,i|
      STDERR.write '.'; STDERR.write " #{i+1}\n" if (i+1)%80==0
      @rules << Rule.from_s(s)
      if @rules.last.rhs.first.class == NT
        @startn << @rules.last
      else
        if rules.last.arity == 0
          @flat << @rules.last
        else
          @startt << @rules.last
        end
      end
    }
    STDERR.write "\n"
  end

  def to_s
    s = ''
    @rules.each { |r| s += r.to_s+"\n" }
    return s
  end

  def add_glue_rules
    @rules.map { |r| r.lhs.symbol }.select { |s| s != 'S' }.uniq.each { |symbol|
      @rules << Rule.new(NT.new('S'), [NT.new(symbol)])
      @startn << @rules.last
      @rules << Rule.new(NT.new('S'), [NT.new('S'), NT.new('X')])
      @startn << @rules.last
    }
  end

  def add_pass_through_rules s
    s.each { |word|
      @rules << Rule.new(NT.new('X'), [T.new(word)])
      @flat << @rules.last
    }
  end
end