Class: SegmentRuby::ProbabilityDistribution
- Inherits:
-
Object
- Object
- SegmentRuby::ProbabilityDistribution
- Defined in:
- lib/segment_ruby.rb
Instance Attribute Summary collapse
-
#log_total ⇒ Object
readonly
Returns the value of attribute log_total.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #files ⇒ Object
- #has_key?(w) ⇒ Boolean
-
#initialize(total_file_name, data_file_name) ⇒ ProbabilityDistribution
constructor
A new instance of ProbabilityDistribution.
- #log_prob(w) ⇒ Object
- #prob(w) ⇒ Object
- #total ⇒ Object
Constructor Details
#initialize(total_file_name, data_file_name) ⇒ ProbabilityDistribution
Returns a new instance of ProbabilityDistribution.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/segment_ruby.rb', line 13 def initialize(total_file_name, data_file_name) @total_file_name = total_file_name @data_file_name = total_file_name @log_total = begin total = File.read(total_file_name).to_i Math.log2(total) rescue Math.log2(10**1000) end @table = Hash.new { |w| -Float::INFINITY } File.open(data_file_name).each_line do |line| data = line.split(/\s/) freq = data[-1].to_i keys = data[0..-2] key = keys.join(' ') log_p = Math.log2(freq) - log_total table[key] = log_p end end |
Instance Attribute Details
#log_total ⇒ Object (readonly)
Returns the value of attribute log_total.
37 38 39 |
# File 'lib/segment_ruby.rb', line 37 def log_total @log_total end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
37 38 39 |
# File 'lib/segment_ruby.rb', line 37 def table @table end |
Instance Method Details
#files ⇒ Object
39 40 41 |
# File 'lib/segment_ruby.rb', line 39 def files [@total_file_name, @data_file_name] end |
#has_key?(w) ⇒ Boolean
55 56 57 |
# File 'lib/segment_ruby.rb', line 55 def has_key?(w) table.has_key?(w) end |
#log_prob(w) ⇒ Object
43 44 45 |
# File 'lib/segment_ruby.rb', line 43 def log_prob(w) table[w] end |
#prob(w) ⇒ Object
47 48 49 |
# File 'lib/segment_ruby.rb', line 47 def prob(w) 2**table[w] end |
#total ⇒ Object
51 52 53 |
# File 'lib/segment_ruby.rb', line 51 def total 2**log_total end |