Class: Stamina::Sample
- Inherits:
-
Object
- Object
- Stamina::Sample
- Includes:
- Enumerable
- Defined in:
- lib/stamina/sample.rb
Overview
A sample as an ordered collection of InputString labeled as positive or negative.
Tips and tricks
-
loading samples from disk is easy thanks to ADL !
Detailed API
Instance Attribute Summary collapse
-
#negative_count ⇒ Object
readonly
Number of negative strings in the sample.
-
#positive_count ⇒ Object
readonly
Number of positive strings in the sample.
-
#size ⇒ Object
readonly
Number of strings in the sample.
Class Method Summary collapse
-
.[](*args) ⇒ Object
Creates an empty sample and appends it with args, by calling Sample#<< on each of them.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Adds a string to the sample.
-
#==(other) ⇒ Object
(also: #eql?)
Compares with another sample other, which is required to be a Sample instance.
-
#correctly_classified_by?(classifier) ⇒ Boolean
Checks if the sample is correctly classified by a given classifier (expected to include the Stamina::Classfier module).
-
#each ⇒ Object
Yields the block with each string.
-
#each_negative ⇒ Object
Yields the block with each negative string.
-
#each_positive ⇒ Object
Yields the block with each positive string.
-
#empty? ⇒ Boolean
Returns true if this sample does not contain any string, false otherwise.
-
#hash ⇒ Object
Computes an hash code for this sample.
-
#include?(str) ⇒ Boolean
Returns true if a given string is included in the sample, false otherwise.
-
#initialize ⇒ Sample
constructor
Creates an empty sample.
-
#negative_enumerator ⇒ Object
Returns an enumerator on negative strings.
-
#positive_enumerator ⇒ Object
Returns an enumerator on positive strings.
-
#signature ⇒ Object
Computes and returns the binary signature of the sample.
-
#to_adl(buffer = "") ⇒ Object
(also: #to_s, #inspect)
Prints an ADL description of this sample on the buffer.
Constructor Details
#initialize ⇒ Sample
Creates an empty sample.
31 32 33 34 |
# File 'lib/stamina/sample.rb', line 31 def initialize() @strings = [] @size, @positive_count, @negative_count = 0, 0, 0 end |
Instance Attribute Details
#negative_count ⇒ Object (readonly)
Number of negative strings in the sample
20 21 22 |
# File 'lib/stamina/sample.rb', line 20 def negative_count @negative_count end |
#positive_count ⇒ Object (readonly)
Number of positive strings in the sample
17 18 19 |
# File 'lib/stamina/sample.rb', line 17 def positive_count @positive_count end |
#size ⇒ Object (readonly)
Number of strings in the sample
14 15 16 |
# File 'lib/stamina/sample.rb', line 14 def size @size end |
Class Method Details
Instance Method Details
#<<(str) ⇒ Object
Adds a string to the sample. The str argument may be an InputString instance, a String (parsed using ADL), a Sample instance (all strings are added) or an Array (recurses on each element).
Raises an InconsistencyError if the same string already exists with the opposite label. Raises an ArgumentError if the str argument is not recognized.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/stamina/sample.rb', line 52 def <<(str) case str when InputString #raise(InconsistencyError, "Inconsistent sample on #{str}", caller) if self.include?(str.negate) @size += 1 str.positive? ? (@positive_count += 1) : (@negative_count += 1) @strings << str when String self << ADL::parse_string(str) when Sample str.each {|s| self << s} when Array str.each {|s| self << s} else raise(ArgumentError, "#{str} is not a valid argument.", caller) end self end |
#==(other) ⇒ Object Also known as: eql?
Compares with another sample other, which is required to be a Sample instance. Returns true if the two samples contains the same strings (including labels), false otherwise.
97 98 99 |
# File 'lib/stamina/sample.rb', line 97 def ==(other) include?(other) and other.include?(self) end |
#correctly_classified_by?(classifier) ⇒ Boolean
Checks if the sample is correctly classified by a given classifier (expected to include the Stamina::Classfier module). Unlabeled strings are simply ignored.
162 163 164 |
# File 'lib/stamina/sample.rb', line 162 def correctly_classified_by?(classifier) classifier.correctly_classify?(self) end |
#each ⇒ Object
Yields the block with each string. This method has no effect if no block is given.
113 114 115 116 |
# File 'lib/stamina/sample.rb', line 113 def each return unless block_given? @strings.each {|str| yield str} end |
#each_negative ⇒ Object
Yields the block with each negative string. This method has no effect if no block is given.
142 143 144 |
# File 'lib/stamina/sample.rb', line 142 def each_negative each {|str| yield str if str.negative?} end |
#each_positive ⇒ Object
Yields the block with each positive string. This method has no effect if no block is given.
122 123 124 125 |
# File 'lib/stamina/sample.rb', line 122 def each_positive return unless block_given? each {|str| yield str if str.positive?} end |
#empty? ⇒ Boolean
Returns true if this sample does not contain any string, false otherwise.
40 41 42 |
# File 'lib/stamina/sample.rb', line 40 def empty?() @size==0 end |
#hash ⇒ Object
Computes an hash code for this sample.
105 106 107 |
# File 'lib/stamina/sample.rb', line 105 def hash self.inject(37){|memo,str| memo + 17*str.hash} end |
#include?(str) ⇒ Boolean
Returns true if a given string is included in the sample, false otherwise. This method allows same flexibility as << for the str argument.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/stamina/sample.rb', line 75 def include?(str) case str when InputString @strings.include?(str) when String include?(ADL::parse_string(str)) when Array str.each {|s| return false unless include?(s)} true when Sample str.each {|s| return false unless include?(s)} true else raise(ArgumentError, "#{str} is not a valid argument.", caller) end end |
#negative_enumerator ⇒ Object
Returns an enumerator on negative strings.
149 150 151 152 153 154 155 |
# File 'lib/stamina/sample.rb', line 149 def negative_enumerator if RUBY_VERSION >= "1.9" Enumerator.new(self, :each_negative) else Enumerable::Enumerator.new(self, :each_negative) end end |
#positive_enumerator ⇒ Object
Returns an enumerator on positive strings.
130 131 132 133 134 135 136 |
# File 'lib/stamina/sample.rb', line 130 def positive_enumerator if RUBY_VERSION >= "1.9" Enumerator.new(self, :each_positive) else Enumerable::Enumerator.new(self, :each_positive) end end |
#signature ⇒ Object
Computes and returns the binary signature of the sample. The signature is a String having one character for each string in the sample. A ‘1’ is used for positive strings, ‘0’ for negative ones and ‘?’ for unlabeled.
171 172 173 174 175 176 177 |
# File 'lib/stamina/sample.rb', line 171 def signature signature = '' each do |str| signature << (str.unlabeled? ? '?' : str.positive? ? '1' : '0') end signature end |
#to_adl(buffer = "") ⇒ Object Also known as: to_s, inspect
Prints an ADL description of this sample on the buffer.
182 183 184 |
# File 'lib/stamina/sample.rb', line 182 def to_adl(buffer="") self.inject(buffer) {|memo,str| memo << "\n" << str.to_adl} end |