summaryrefslogtreecommitdiff
path: root/grammar.rb
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-06-04 20:26:06 +0200
committerPatrick Simianer <p@simianer.de>2014-06-04 20:26:06 +0200
commita10db22ce00bd004682a00322b4d177b694082b7 (patch)
tree1a97ab371137cd459418f12db00237f0039fada1 /grammar.rb
parentdbc15de7a63b939d7c3c51c39b34286aed56739f (diff)
refactoring, e.g. edges have scores, not weights
Diffstat (limited to 'grammar.rb')
-rw-r--r--grammar.rb24
1 files changed, 12 insertions, 12 deletions
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)