From 94fef2d3aac6d7380e92771c46cc0e4655afaea4 Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Sun, 1 Jun 2014 23:23:14 +0200
Subject: add grammar.rb, hg.rb: viterbi_path(), all_paths()
---
README.md | 1 +
lib/nlp_ruby.rb | 3 ++-
lib/nlp_ruby/hg.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
nlp_ruby.gemspec | 2 +-
4 files changed, 52 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 338109c..cc8d3ef 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ The .gem can be found here: https://rubygems.org/gems/nlp_ruby
bleu.rb : BLEU implementation, also per-sentence-BLEU
dag.rb : implementation of a directed acyclic graph and various algorithms
fileutil.rb : file utilities
+ grammar.rb : classes to model grammars (mostly for translation grammars)
hg.rb : data structures and algorithms (viterbi currently) for hypergraphs
misc.rb : misc. stuff (e.g. monkey patches for Array and String)
semirings.rb : semirings (used in dags.rb)
diff --git a/lib/nlp_ruby.rb b/lib/nlp_ruby.rb
index f57a302..0e26e97 100755
--- a/lib/nlp_ruby.rb
+++ b/lib/nlp_ruby.rb
@@ -10,8 +10,9 @@ require 'nlp_ruby/semirings'
require 'nlp_ruby/bleu'
require 'nlp_ruby/misc'
require 'nlp_ruby/hg'
+require 'nlp_ruby/grammar'
-STDIN.set_encoding 'utf-8'
+STDIN.set_encoding 'utf-8'
STDOUT.set_encoding 'utf-8'
STDERR.set_encoding 'utf-8'
diff --git a/lib/nlp_ruby/hg.rb b/lib/nlp_ruby/hg.rb
index b49cc6e..66e3b13 100644
--- a/lib/nlp_ruby/hg.rb
+++ b/lib/nlp_ruby/hg.rb
@@ -3,6 +3,7 @@
require 'nlp_ruby'
require 'json'
+
module HG
@@ -40,14 +41,15 @@ class HG::Hypergraph
end
class HG::Hyperedge
- attr_accessor :head, :tails, :weight, :f, :mark
+ attr_accessor :head, :tails, :weight, :f, :mark, :rule, :left, :right
- def initialize head=nil, tails=[], weight=0.0, f={}
+ def initialize head=nil, tails=[], weight=0.0, f={}, rule=nil, left=nil, right=nil
@head = head
@tails = tails
@weight = weight
@f = f
@mark = 0
+ @rule = Grammar::Rule.from_s rule if rule
end
def arity
@@ -59,7 +61,7 @@ class HG::Hyperedge
end
def to_s
- "Hyperedge