Class: SegmentRuby::ProbabilityDistribution

Inherits:
Object
  • Object
show all
Defined in:
lib/segment_ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(total_file_name, data_file_name) ⇒ ProbabilityDistribution



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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
  begin
    total = File.read(total_file_name).to_i
    @log_total= Math.log2(total)
  rescue
    @log_total= 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
  true
end

Instance Method Details

#filesObject



39
40
41
# File 'lib/segment_ruby.rb', line 39

def files
  [@total_file_name, @data_file_name]
end

#has_key?(w) ⇒ Boolean



59
60
61
# File 'lib/segment_ruby.rb', line 59

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

#log_totalObject



51
52
53
# File 'lib/segment_ruby.rb', line 51

def log_total
  @log_total
end

#prob(w) ⇒ Object



47
48
49
# File 'lib/segment_ruby.rb', line 47

def prob(w)
  2**@table[w]
end

#tableObject



35
36
37
# File 'lib/segment_ruby.rb', line 35

def table
  @table
end

#totalObject



55
56
57
# File 'lib/segment_ruby.rb', line 55

def total
  2**@log_total
end