summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2014-09-27 15:17:43 +0100
committerPatrick Simianer <p@simianer.de>2014-09-27 15:17:43 +0100
commitf65e428784cfd2264f3fdfddd574c37acd38f54f (patch)
tree6a98f82964cbce52957a2d62f8b2c5b7ff8be095
parentf28694f9bdd5059050e420cca2ca2694cbb0dda4 (diff)
cosmetic changes, bump to 1.1
-rwxr-xr-xlib/zipf.rb3
-rw-r--r--lib/zipf/SparseVector.rb1
-rw-r--r--lib/zipf/bleu.rb2
-rw-r--r--lib/zipf/dag.rb2
-rw-r--r--lib/zipf/fileutil.rb1
-rw-r--r--lib/zipf/grammar.rb123
-rw-r--r--lib/zipf/hypergraph.rb (renamed from lib/zipf/hg.rb)3
-rw-r--r--lib/zipf/misc.rb1
-rw-r--r--lib/zipf/tfidf.rb1
-rw-r--r--test/hypergraph/hg-toy.json (renamed from test/hg/hg-toy.json)0
-rw-r--r--test/hypergraph/hg.json (renamed from test/hg/hg.json)0
-rwxr-xr-xtest/test_dag.rb1
-rwxr-xr-xtest/test_hypergraph.rb (renamed from test/test_hg.rb)5
13 files changed, 3 insertions, 140 deletions
diff --git a/lib/zipf.rb b/lib/zipf.rb
index 2f59ccc..681e2cd 100755
--- a/lib/zipf.rb
+++ b/lib/zipf.rb
@@ -9,8 +9,7 @@ require 'zipf/dag'
require 'zipf/semirings'
require 'zipf/bleu'
require 'zipf/misc'
-require 'zipf/hg'
-require 'zipf/grammar'
+require 'zipf/hypergraph'
STDIN.set_encoding 'utf-8'
STDOUT.set_encoding 'utf-8'
diff --git a/lib/zipf/SparseVector.rb b/lib/zipf/SparseVector.rb
index 3f950c4..e9af799 100644
--- a/lib/zipf/SparseVector.rb
+++ b/lib/zipf/SparseVector.rb
@@ -1,6 +1,5 @@
require 'json'
-
class SparseVector < Hash
def initialize arg=nil
diff --git a/lib/zipf/bleu.rb b/lib/zipf/bleu.rb
index 69de00b..c07315e 100644
--- a/lib/zipf/bleu.rb
+++ b/lib/zipf/bleu.rb
@@ -1,6 +1,5 @@
module BLEU
-
class BLEU::NgramCounts
attr_accessor :sum, :clipped, :ref_len, :hyp_len, :n
@@ -125,6 +124,5 @@ def BLEU::per_sentence_bleu hypothesis, reference, n=4, smooth=0.0
return Math.exp logbleu
end
-
end #module
diff --git a/lib/zipf/dag.rb b/lib/zipf/dag.rb
index 45ede20..a0edde7 100644
--- a/lib/zipf/dag.rb
+++ b/lib/zipf/dag.rb
@@ -2,7 +2,6 @@ module DAG
require 'json'
-
class DAG::Node
attr_accessor :label, :outgoing, :incoming, :score, :mark
@@ -200,6 +199,5 @@ def DAG::read_graph_from_json fn, semiring=RealSemiring.new
return graph, nodes_by_label
end
-
end #module
diff --git a/lib/zipf/fileutil.rb b/lib/zipf/fileutil.rb
index eb69136..ac701da 100644
--- a/lib/zipf/fileutil.rb
+++ b/lib/zipf/fileutil.rb
@@ -1,6 +1,5 @@
require 'zlib'
-
class ReadFile
def initialize fn, encoding='utf-8'
diff --git a/lib/zipf/grammar.rb b/lib/zipf/grammar.rb
deleted file mode 100644
index 568b9fc..0000000
--- a/lib/zipf/grammar.rb
+++ /dev/null
@@ -1,123 +0,0 @@
-module Grammar
-
-
-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, :e
-
- def initialize lhs=nil, rhs=[], e=''
- @lhs = lhs
- @rhs = rhs
- @e = e
- end
-
- def to_s
- "#{lhs} -> #{rhs.map{ |i| i.to_s }.join ' '} [arity=#{arity}] ||| #{@e}"
- 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
- }
- @e = _[2]
- end
-
- def self.from_s s
- r = self.new
- r.from_s s
- return r
- end
-end
-
-class Span
- attr_accessor :left, :right
-
- def initialize left=nil, right=nil
- @left = left
- @right = right
- 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
-
-
-end #module
-
diff --git a/lib/zipf/hg.rb b/lib/zipf/hypergraph.rb
index f86bf60..6666062 100644
--- a/lib/zipf/hg.rb
+++ b/lib/zipf/hypergraph.rb
@@ -3,10 +3,8 @@
require_relative 'semirings'
require 'json'
-
module HG
-
class HG::Node
attr_accessor :label, :cat, :outgoing, :incoming, :score
@@ -168,6 +166,5 @@ def HG::all_paths hypergraph, root, semiring=ViterbiSemiring.new
return paths
end
-
end #module
diff --git a/lib/zipf/misc.rb b/lib/zipf/misc.rb
index 0319a5f..4d29a06 100644
--- a/lib/zipf/misc.rb
+++ b/lib/zipf/misc.rb
@@ -1,6 +1,5 @@
require 'timeout'
-
class Array
def max_index
self.index(self.max)
diff --git a/lib/zipf/tfidf.rb b/lib/zipf/tfidf.rb
index 13a40a3..7fb92c9 100644
--- a/lib/zipf/tfidf.rb
+++ b/lib/zipf/tfidf.rb
@@ -1,6 +1,5 @@
module TFIDF
-
# returns key='raw frequency' for an
# array-like object
def TFIDF::tf array, stopwords=[]
diff --git a/test/hg/hg-toy.json b/test/hypergraph/hg-toy.json
index ab7eefc..ab7eefc 100644
--- a/test/hg/hg-toy.json
+++ b/test/hypergraph/hg-toy.json
diff --git a/test/hg/hg.json b/test/hypergraph/hg.json
index 1c093bd..1c093bd 100644
--- a/test/hg/hg.json
+++ b/test/hypergraph/hg.json
diff --git a/test/test_dag.rb b/test/test_dag.rb
index 5c3938b..3007657 100755
--- a/test/test_dag.rb
+++ b/test/test_dag.rb
@@ -3,7 +3,6 @@
require_relative '../lib/zipf'
require 'test/unit'
-
class TestDAG < Test::Unit::TestCase
def test_viterbi
diff --git a/test/test_hg.rb b/test/test_hypergraph.rb
index 667a3b8..c5add9f 100755
--- a/test/test_hg.rb
+++ b/test/test_hypergraph.rb
@@ -3,18 +3,17 @@
require_relative '../lib/zipf'
require 'test/unit'
-
class TestHG < Test::Unit::TestCase
def test_viterbi
semiring = ViterbiSemiring.new
- hypergraph, nodes_by_label, nodes_by_index = HG::read_hypergraph_from_json('test/hg/hg.json', semiring, true)
+ hypergraph, nodes_by_label, nodes_by_index = HG::read_hypergraph_from_json('test/hypergraph/hg.json', semiring, true)
HG::viterbi hypergraph, nodes_by_label['root'], semiring
assert_equal('Goal', nodes_by_index.last.cat)
assert_equal(228.95, Math.log(nodes_by_index.last.score).round(2))
# do all operations in log space
semiring = ViterbiLogSemiring.new
- hypergraph, nodes_by_label, nodes_by_index = HG::read_hypergraph_from_json('test/hg/hg.json', semiring)
+ hypergraph, nodes_by_label, nodes_by_index = HG::read_hypergraph_from_json('test/hypergraph/hg.json', semiring)
HG::viterbi hypergraph, nodes_by_label['root'], semiring
assert_equal('Goal', nodes_by_index.last.cat)
assert_equal(228.95, nodes_by_index.last.score.round(2))