Class: CorrectHorseBatteryStaple::Word

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/correct_horse_battery_staple/word.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_map = {}) ⇒ Word

Returns a new instance of Word.

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/correct_horse_battery_staple/word.rb', line 40

def initialize(value_map = {})
  raise ArgumentError, "Must supply at least :word" unless value_map[:word] || value_map["word"]

  # phasing this out
  self.index = -1

  case value_map
    when Hash then update_from_hash(value_map)
    when CorrectHorseBatteryStaple::Word then update_from_hash(value_map.to_hash)
    else raise "Can't initialize Word from #{value_map.inspect}"
  end
end

Instance Attribute Details

#dispersionObject

dispersion is Juilland dispersion, the % of texts containing the word. texts is the # of texts containing the word.



14
15
16
# File 'lib/correct_horse_battery_staple/word.rb', line 14

def dispersion
  @dispersion
end

#distanceObject

this word’s frequency’s distance from mean frequency in stddevs; signed.



28
29
30
# File 'lib/correct_horse_battery_staple/word.rb', line 28

def distance
  @distance
end

#distance_probabilityObject

distance_probability is the distance of this word’s probability from the mean in stddev



36
37
38
# File 'lib/correct_horse_battery_staple/word.rb', line 36

def distance_probability
  @distance_probability
end

#frequencyObject

frequency is the total count of the word in corpus



6
7
8
# File 'lib/correct_horse_battery_staple/word.rb', line 6

def frequency
  @frequency
end

#indexObject

rank is the word position when sorted by frequency in entire corpus index is the index of the word in this (sub)corpus



10
11
12
# File 'lib/correct_horse_battery_staple/word.rb', line 10

def index
  @index
end

#percentileObject

in which percentile does the word appear. this can be calculated from the array of words so is somewhat redundant here



24
25
26
# File 'lib/correct_horse_battery_staple/word.rb', line 24

def percentile
  @percentile
end

#probabilityObject

probability is the chance of any given word in a text composed of the sum of (word*frequency) in the corpus being this word.



32
33
34
# File 'lib/correct_horse_battery_staple/word.rb', line 32

def probability
  @probability
end

#rankObject

rank is the word position when sorted by frequency in entire corpus index is the index of the word in this (sub)corpus



10
11
12
# File 'lib/correct_horse_battery_staple/word.rb', line 10

def rank
  @rank
end

#textsObject

texts is the # of texts containing the word. this is not available for many frequency lists.



18
19
20
# File 'lib/correct_horse_battery_staple/word.rb', line 18

def texts
  @texts
end

#wordObject

text of word



3
4
5
# File 'lib/correct_horse_battery_staple/word.rb', line 3

def word
  @word
end

Instance Method Details

#<=>(other) ⇒ Object



53
54
55
# File 'lib/correct_horse_battery_staple/word.rb', line 53

def <=>(other)
  self.frequency <=> other.frequency
end

#==(other) ⇒ Object



99
100
101
# File 'lib/correct_horse_battery_staple/word.rb', line 99

def ==(other)
  self.word == other.word
end

#[](attr) ⇒ Object



85
86
87
# File 'lib/correct_horse_battery_staple/word.rb', line 85

def [](attr)
  send(attr.to_s)
end

#[]=(attr, value) ⇒ Object



89
90
91
# File 'lib/correct_horse_battery_staple/word.rb', line 89

def []=(attr, value)
  send("#{attr}=", value)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/correct_horse_battery_staple/word.rb', line 95

def eql?(other)
  self.word == other.word
end

#inspectObject



65
66
67
# File 'lib/correct_horse_battery_staple/word.rb', line 65

def inspect
  "CHBS::Word(#{self.to_hash.inspect})"
end

#to_hashObject



69
70
71
72
73
74
# File 'lib/correct_horse_battery_staple/word.rb', line 69

def to_hash
  instance_variables.reduce({}) do |hash, key|
    hash[key.to_s[1..-1]] = instance_variable_get(key)
    hash
  end
end

#to_json(*args) ⇒ Object



57
58
59
# File 'lib/correct_horse_battery_staple/word.rb', line 57

def to_json(*args)
  to_hash.to_json(*args)
end

#to_sObject



61
62
63
# File 'lib/correct_horse_battery_staple/word.rb', line 61

def to_s
  self.word
end

#update_from_hash(hash) ⇒ Object



76
77
78
79
80
81
# File 'lib/correct_horse_battery_staple/word.rb', line 76

def update_from_hash(hash)
  hash.each do |key, val|
    self[key] = val unless key.to_s == "wstruct"
  end
  self
end