Class Cass::Contrast

  1. cass/lib/cass/contrast.rb
Parent: Object

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.

Methods

public class

  1. new
  2. parse

public instance

  1. apply
  2. to_s

Attributes

words [RW]

Public class methods

new (words)
[show source]
# File cass/lib/cass/contrast.rb, line 10
    def initialize(words)
      words = words.split(/,*\s+/) if words.class == String
      @words = words
    end
parse (contrast)

Initialize a contrast from a string representation.

[show source]
# File cass/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

Public instance methods

apply (doc)

Apply the contrast to a document and return result as a string. See to_s method for format.

[show source]
# File cass/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 ()

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.

[show source]
# File cass/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