Class: Probability::FrequencyDistribution

Inherits:
Hash
  • Object
show all
Defined in:
lib/punkt-segmenter/frequency_distribution.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFrequencyDistribution

Returns a new instance of FrequencyDistribution.



9
10
11
12
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 9

def initialize
  super
  clear
end

Instance Attribute Details

#NObject (readonly)

Returns the value of attribute N.



4
5
6
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 4

def N
  @N
end

Instance Method Details

#<<(sample) ⇒ Object



54
55
56
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 54

def <<(sample)
  self.inc(sample)
end

#[](sample) ⇒ Object



20
21
22
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 20

def [](sample)
  super || 0
end

#[]=(sample, value) ⇒ Object



24
25
26
27
28
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 24

def []=(sample, value)
  @N += (value - self[sample])
  super
  @cache = {}
end

#clearObject



14
15
16
17
18
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 14

def clear
  super
  @N = 0
  @cache = {}
end

#delete(sample, &block) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 63

def delete(sample, &block)
  result = super
  if result
    @cache = {}
    @N -= result
  end
  result
end

#delete_if(&block) ⇒ Object



72
73
74
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 72

def delete_if(&block)
  raise "Not implemented for Frequency Distributions"
end

#each(&block) ⇒ Object



42
43
44
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 42

def each(&block)
  items.each { |item| yield(item[0], item[1]) }
end

#each_key(&block) ⇒ Object



46
47
48
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 46

def each_key(&block)
  keys.each { |item| yield(item) }
end

#each_value(&block) ⇒ Object



50
51
52
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 50

def each_value(&block)
  values.each { |value| yield(value) }
end

#frequency_of(sample) ⇒ Object



76
77
78
79
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 76

def frequency_of(sample)
  return 0 if @N == 0
  return self[sample].to_f / @N
end

#inc(sample, count = 1) ⇒ Object



58
59
60
61
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 58

def inc(sample, count = 1)
  return if count == 0
  self[sample] = self[sample] + count
end

#itemsObject



38
39
40
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 38

def items
  @cache[:ordered_by_frequency_desc] ||= self.to_a.sort {|x,y| y[1] <=> x[1] }
end

#keysObject



30
31
32
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 30

def keys
  items.map { |item| item[0] }
end

#maxObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 81

def max
  unless @cache[:max]
    max_sample = nil
    max_count  = -1
    self.keys.each do |sample|
      if self[sample] > max_count
        max_sample = sample
        max_count  = self[sample]
      end
    end
    @cache[:max] = max_sample
  end
  return @cache[:max]
end

#merge(other_frequency_distribution) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 96

def merge(other_frequency_distribution)
  temp = self.dup
  other_frequency_distribution.each do |sample, value|
    temp.inc(sample, value)
  end
  return temp
end

#merge!(other_frequency_distribution) ⇒ Object



104
105
106
107
108
109
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 104

def merge!(other_frequency_distribution)
  other_frequency_distribution.each do |sample, value|
    self.inc(sample, value)
  end
  self
end

#samplesObject



7
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 7

alias_method :samples, :keys

#valuesObject



34
35
36
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 34

def values
  items.map { |item| item[1] }
end