Class: Yanbi::WordBag

Inherits:
Object
  • Object
show all
Defined in:
lib/wordbags/wordbag.rb

Direct Known Subclasses

DiadBag, StemmedWordBag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(corpus = nil) ⇒ WordBag



18
19
20
21
22
# File 'lib/wordbags/wordbag.rb', line 18

def initialize(corpus=nil)
  @words = []
  @counts = {}
  standardize(corpus) if corpus
end

Instance Attribute Details

#wordsObject (readonly)

Returns the value of attribute words.



16
17
18
# File 'lib/wordbags/wordbag.rb', line 16

def words
  @words
end

Class Method Details

.load(filename) ⇒ Object



44
45
46
# File 'lib/wordbags/wordbag.rb', line 44

def self.load(filename)
  WordBag.new.load(filename)
end

Instance Method Details

#add_file(filename) ⇒ Object



24
25
26
27
# File 'lib/wordbags/wordbag.rb', line 24

def add_file(filename)
  raw = File.open(filename).read
  standardize(raw)
end

#add_text(text) ⇒ Object



29
30
31
# File 'lib/wordbags/wordbag.rb', line 29

def add_text(text)
  standardize(text)
end

#between_counts(min, max = nil) ⇒ Object



59
60
61
62
63
# File 'lib/wordbags/wordbag.rb', line 59

def between_counts(min, max=nil)
  counts = @counts.select{|key, value| value >= min}
  counts.select! {|key, value| value <= max} unless max.nil?
  @words.select {|word| counts.keys.include? word}
end

#empty?Boolean



69
70
71
# File 'lib/wordbags/wordbag.rb', line 69

def empty?
  @words.empty?
end

#intersection(other) ⇒ Object



65
66
67
# File 'lib/wordbags/wordbag.rb', line 65

def intersection(other)
  self.words & other.words
end

#load(filename) ⇒ Object



39
40
41
42
# File 'lib/wordbags/wordbag.rb', line 39

def load(filename)
  @words = YAML.load_file(filename + ".yml")
  update_counts(@words)
end

#remove(words) ⇒ Object



52
53
54
55
56
57
# File 'lib/wordbags/wordbag.rb', line 52

def remove(words)
  words.each do |word|
    @words.reject! {|x| x == word}
    @counts.delete(word)
  end
end

#save(filename) ⇒ Object



33
34
35
36
37
# File 'lib/wordbags/wordbag.rb', line 33

def save(filename)
  out = File.new(filename + ".yml", "w")
  out.write(@words.to_yaml)
  out.close
end

#word_counts(min = 1) ⇒ Object



48
49
50
# File 'lib/wordbags/wordbag.rb', line 48

def word_counts(min=1)
  @counts.select {|key, value| value >= min}
end