Class: Stamina::Sample

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSample

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_countObject (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_countObject (readonly)

Number of positive strings in the sample



17
18
19
# File 'lib/stamina/sample.rb', line 17

def positive_count
  @positive_count
end

#sizeObject (readonly)

Number of strings in the sample



14
15
16
# File 'lib/stamina/sample.rb', line 14

def size
  @size
end

Class Method Details

.[](*args) ⇒ Object

Creates an empty sample and appends it with args, by calling Sample#<< on each of them.



26
# File 'lib/stamina/sample.rb', line 26

def self.[](*args) Sample.new << args end

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.

Returns:

  • (Boolean)


162
163
164
# File 'lib/stamina/sample.rb', line 162

def correctly_classified_by?(classifier)
  classifier.correctly_classify?(self)
end

#eachObject

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_negativeObject

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_positiveObject

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.

Returns:

  • (Boolean)


40
41
42
# File 'lib/stamina/sample.rb', line 40

def empty?() 
  @size==0 
end

#hashObject

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.

Returns:

  • (Boolean)


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_enumeratorObject

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_enumeratorObject

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

#signatureObject

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