diff options
author | Patrick Simianer <p@simianer.de> | 2014-01-29 19:22:56 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-01-29 19:22:56 +0100 |
commit | d9d72e06db07087aa54401fae8b259f0c4ccd649 (patch) | |
tree | 97f0444314c40d2894ac0892d5559101eda01acf /lib/nlp_ruby/semirings.rb | |
parent | 22644ed1365e566c8bf806bfff4ecd43c46ce089 (diff) |
first usable version, name change => nlp_ruby
Diffstat (limited to 'lib/nlp_ruby/semirings.rb')
-rw-r--r-- | lib/nlp_ruby/semirings.rb | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/nlp_ruby/semirings.rb b/lib/nlp_ruby/semirings.rb new file mode 100644 index 0000000..a06f151 --- /dev/null +++ b/lib/nlp_ruby/semirings.rb @@ -0,0 +1,68 @@ +# semirings for graphs as described in +# 'Dynamic Programming Algorithms in +# Semiring and Hypergraph Frameworks' (Liang Huang) +class Semiring + attr_accessor :add, :multiply, :one, :null, :convert +end + +class BooleanSemiring < Semiring + def initialize + @add = Proc.new { |a,b| a||b } + @multiply = Proc.new { |a,b| a&&b } + @one = true + @null = false + @convert = Proc.new { |v| true && v!=0 } + end +end + +class ViterbiSemiring < Semiring + def initialize + @add = Proc.new { |a,b| [a,b].max } + @multiply = Proc.new { |a,b| a*b } + @one = 1.0 + @null = 0.0 + @convert = Proc.new { |v| v } + end +end + +class InsideSemiring < Semiring + def initialize + @add = Proc.new { |a,b| a+b } + @multiply = Proc.new { |a,b| a*b } + @one = 1.0 + @null = 0.0 + @convert = Proc.new { |v| v } + end +end + +class RealSemiring < Semiring + def initialize + @add = Proc.new { |a,b| [a,b].min } + @multiply = Proc.new { |a,b| a+b } + @one = 0.0 + @null = 1.0/0.0 + @convert = Proc.new { |v| v } + end +end + +# for longest/worst paths +class RealxSemiring < Semiring + def initialize + @add = Proc.new { |a,b| [a,b].max } + @multiply = Proc.new { |a,b| a+b } + @one = -1.0/0.0 + @null = 0.0 + @convert = Proc.new { |v| v } + end +end + +class CountingSemiring < Semiring + def initialize + @add = Proc.new { |a,b| a+b } + @multiply = Proc.new { |a,b| a*b } + @one = 1.0 + @null = 0.0 + @convert = Proc.new { |v| if v!=0 then 1 else 0 end } + end +end + |