From a10db22ce00bd004682a00322b4d177b694082b7 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 4 Jun 2014 20:26:06 +0200 Subject: refactoring, e.g. edges have scores, not weights --- grammar.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'grammar.rb') diff --git a/grammar.rb b/grammar.rb index f4ffe90..7bc6c9b 100644 --- a/grammar.rb +++ b/grammar.rb @@ -25,9 +25,11 @@ class NT def from_s s s.delete! '[]' @symbol, meta = s.split '@' - span, index = meta.split ',' - @left, @right = span.split(':').map { |x| x.to_i } - @index = index.to_i if index + if meta + span, index = meta.split ',' + @left, @right = span.split(':').map { |x| x.to_i } + @index = index.to_i + end end def self.from_s s @@ -44,11 +46,9 @@ end class Rule attr_accessor :lhs, :rhs, :target, :map - def initialize lhs=nil, rhs=[], left=nil, right=nil, target=[] + def initialize lhs=nil, rhs=[], target=[] @lhs = lhs @rhs = rhs - @lhs.left = left if lhs - @lhs.right = right if lhs @target = target @arity_ = nil end @@ -59,20 +59,20 @@ class Rule def arity return @arity_ if @arity_ - return rhs.select { |i| i.class == NT }.size + rhs.select { |i| i.class == NT }.size end def read_right_ s - a = [] + _ = [] s.split.each { |x| x.strip! if x[0]=='[' && x[x.size-1] == ']' - a << NT.from_s(x) + _ << NT.from_s(x) else - a << T.new(x) + _ << T.new(x) end } - return a + return _ end def from_s s @@ -93,7 +93,7 @@ class Grammar attr_accessor :rules, :startn, :startt, :flat def initialize fn - @rules = []; @startn = []; @startt = [] ;@flat = [] + @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) -- cgit v1.2.3