Class: Probability::FrequencyDistribution
- Inherits:
-
Hash
- Object
- Hash
- Probability::FrequencyDistribution
- Defined in:
- lib/punkt-segmenter/frequency_distribution.rb
Instance Attribute Summary collapse
-
#N ⇒ Object
readonly
Returns the value of attribute N.
Instance Method Summary collapse
- #<<(sample) ⇒ Object
- #[](sample) ⇒ Object
- #[]=(sample, value) ⇒ Object
- #clear ⇒ Object
- #delete(sample, &block) ⇒ Object
- #delete_if(&block) ⇒ Object
- #each(&block) ⇒ Object
- #each_key(&block) ⇒ Object
- #each_value(&block) ⇒ Object
- #frequency_of(sample) ⇒ Object
- #inc(sample, count = 1) ⇒ Object
-
#initialize ⇒ FrequencyDistribution
constructor
A new instance of FrequencyDistribution.
- #items ⇒ Object
- #keys ⇒ Object
- #max ⇒ Object
- #merge(other_frequency_distribution) ⇒ Object
- #merge!(other_frequency_distribution) ⇒ Object
- #samples ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ FrequencyDistribution
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
#N ⇒ Object (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
57 58 59 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 57 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 |
#clear ⇒ Object
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
66 67 68 69 70 71 72 73 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 66 def delete(sample, &block) result = super if result @cache = {} @N -= result end result end |
#delete_if(&block) ⇒ Object
75 76 77 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 75 def delete_if(&block) raise "Not implemented for Frequency Distributions" end |
#each(&block) ⇒ Object
44 45 46 47 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 44 def each(&block) items = @cache[:ordered_by_frequency_desc] || order_by_frequency_desc items.each { |item| yield(item[0], item[1]) } end |
#each_key(&block) ⇒ Object
49 50 51 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 49 def each_key(&block) keys.each { |item| yield(item) } end |
#each_value(&block) ⇒ Object
53 54 55 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 53 def each_value(&block) values.each { |value| yield(value) } end |
#frequency_of(sample) ⇒ Object
79 80 81 82 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 79 def frequency_of(sample) return 0 if @N == 0 return self[sample].to_f / @N end |
#inc(sample, count = 1) ⇒ Object
61 62 63 64 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 61 def inc(sample, count = 1) return if count == 0 self[sample] = self[sample] + count end |
#items ⇒ Object
40 41 42 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 40 def items @cache[:ordered_by_frequency_desc] || order_by_frequency_desc end |
#keys ⇒ Object
30 31 32 33 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 30 def keys result = @cache[:ordered_by_frequency_desc] || order_by_frequency_desc result.map { |item| item[0] } end |
#max ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 84 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
99 100 101 102 103 104 105 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 99 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
107 108 109 110 111 112 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 107 def merge!(other_frequency_distribution) other_frequency_distribution.each do |sample, value| self.inc(sample, value) end self end |
#samples ⇒ Object
7 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 7 alias_method :samples, :keys |
#values ⇒ Object
35 36 37 38 |
# File 'lib/punkt-segmenter/frequency_distribution.rb', line 35 def values result = @cache[:ordered_by_frequency_desc] || order_by_frequency_desc result.map { |item| item[1] } end |