Class: Cass::Contrast
- Inherits:
-
Object
- Object
- Cass::Contrast
- Defined in:
- lib/cass/contrast.rb
Overview
Defines a single contrast on a document or documents. Currently, only comparisons between two pairs of words (i.e., computation of the interaction term; see Holtzman et al., submitted for details) is implemented.
Instance Attribute Summary collapse
-
#words ⇒ Object
Returns the value of attribute words.
Class Method Summary collapse
-
.parse(contrast) ⇒ Object
Initialize a contrast from a string representation.
Instance Method Summary collapse
-
#apply(doc) ⇒ Object
Apply the contrast to a document and return result as a string.
-
#initialize(words) ⇒ Contrast
constructor
A new instance of Contrast.
-
#to_s ⇒ Object
Returns a string representation of the contrast.
Constructor Details
#initialize(words) ⇒ Contrast
10 11 12 13 |
# File 'lib/cass/contrast.rb', line 10 def initialize(words) words = words.split(/,*\s+/) if words.class == String @words = words end |
Instance Attribute Details
#words ⇒ Object
Returns the value of attribute words.
8 9 10 |
# File 'lib/cass/contrast.rb', line 8 def words @words end |
Class Method Details
.parse(contrast) ⇒ Object
Initialize a contrast from a string representation.
16 17 18 19 20 |
# File 'lib/cass/contrast.rb', line 16 def self.parse(contrast) words = contrast.split(/,*\s+/) words = [words[0,2], words[2,2]] if (words.size == 4) Contrast.new(words) end |
Instance Method Details
#apply(doc) ⇒ Object
Apply the contrast to a document and return result as a string. See to_s method for format.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cass/contrast.rb', line 24 def apply(doc) sim = doc.similarity(@words.flatten) if sim.class == Array puts "Error: #{doc.name} is missing the following operands: #{sim.join(", ")}" return false end # Compute interaction term if @words.flatten.size == 4 s = [doc.name, sim[0,2], sim[0,3], sim[1,2], sim[1,3]] s[1,4] = s[1,4].map { |f| format("%0.4f", f).to_f } s << s[1] - s[2] - s[3] + s[4] s.join("\t") else [doc.name, sim[0,1]].join("\t") end end |
#to_s ⇒ Object
Returns a string representation of the contrast. For interaction terms, returns five columns (the four pairs and the interaction term). For pairwise contrasts, just returns the similarity.
45 46 47 48 49 50 51 52 |
# File 'lib/cass/contrast.rb', line 45 def to_s if @words.flatten.size == 4 o = @words.flatten "document\t#{o[0]}.#{o[2]}\t#{o[0]}.#{o[3]}\t#{o[1]}.#{o[2]}\t#{o[1]}.#{o[3]}\tinteraction" else "document\t#{@words.join(".")}" end end |