From 603755ea417013f7a0693d54ed658584643aa1c5 Mon Sep 17 00:00:00 2001 From: Tom Adams Date: Sun, 1 Jun 2008 15:38:00 +0100 Subject: [PATCH] Fixed a bug for Edge#active?, and using "time flies like an arrow" for e.g.. --- chart_parser.rb | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/chart_parser.rb b/chart_parser.rb index 9b53237..535f8c6 100755 --- a/chart_parser.rb +++ b/chart_parser.rb @@ -35,14 +35,18 @@ class Edge [@i,@j,@category,@contents,@sub].hash end def to_s + "#{active? ? 'A' : 'I'}"+ "<#{@i},#{@j},#{@category},[%s],[%s]>" % [@contents.join(', '), @sub.join(', ')] end def active? - @j-@i != @contents.length + unfound and unfound.length != 0 + end + def unfound + @contents[@sub.length..-1] end def nextWord - @contents[(@j-@i)] + unfound[0] end def to_tree #raise Exception if active? @@ -70,7 +74,7 @@ class Parser @grammar.each do |cat,rule| for r in rule if r == [Terminal.new(word)] - agenda << Edge.new(n,n+1,cat,[Terminal.new(word)]) + agenda << Edge.new(n,n+1,cat,[],[Terminal.new(word)]) end end end @@ -93,6 +97,7 @@ class Parser chart.each do |edge| @log.debug("chart: #{edge}") end + #gets end # Return all spanning parses in Array form @@ -159,23 +164,27 @@ if __FILE__ == $0 log.level = Logger::DEBUG log.formatter = lambda {|severity,time,progname,msg| "#{severity}: #{msg}\n"} - shouldBe = [['S',[ - ['NP',['Tom']], - ['VP',[ - ['V',['is']], - ['NP',['cool']] - ] - ] - ] - ]].to_set - - grammar = {'S'=>[['NP','VP']], - 'VP'=>[['V','NP']], - 'NP'=>[[Terminal.new('Tom')],[Terminal.new('cool')]], - 'V'=>[[Terminal.new('is')]]} + shouldBe = Set.new([["S", [["NP", [["NP", ["time"]], ["NP", ["flies"]]]], + ["VP", [["V", ["like"]], ["NP", [["Det", ["an"]], ["NP", ["arrow"]]]]]]]], + ["S", [["NP", ["time"]], ["VP", [["V", ["flies"]], ["PP", [["PR", ["like"]], + ["NP", [["Det", ["an"]], ["NP", ["arrow"]]]]]]]]]]]) + + grammar = {'S'=>[%w{NP VP}], + 'VP'=>[%w{V NP}, + %w{V PP}], + 'NP'=>[[Terminal.new('time')], + [Terminal.new('flies')], + [Terminal.new('arrow')], + %w{Det NP}, + %w{NP NP}], + 'PP'=>[%w{PR NP}], + 'PR'=>[[Terminal.new('like')]], + 'Det'=>[[Terminal.new('an')]], + 'V'=>[[Terminal.new('like')], + [Terminal.new('flies')]]} p = Parser.new(grammar,log) - q = p.parse(%w{Tom is cool}) + q = p.parse(%w{time flies like an arrow}) if q == shouldBe puts "Test succeeded!" -- 2.11.4.GIT