Class: Yanbi::WordBag
- Inherits:
-
Object
- Object
- Yanbi::WordBag
- Defined in:
- lib/wordbags/wordbag.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Class Method Summary collapse
Instance Method Summary collapse
- #add_file(filename) ⇒ Object
- #add_text(text) ⇒ Object
- #between_counts(min, max = nil) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(corpus = nil) ⇒ WordBag
constructor
A new instance of WordBag.
- #intersection(other) ⇒ Object
- #load(filename) ⇒ Object
- #remove(words) ⇒ Object
- #save(filename) ⇒ Object
- #word_counts(min = 1) ⇒ Object
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
#words ⇒ Object (readonly)
Returns the value of attribute words.
16 17 18 |
# File 'lib/wordbags/wordbag.rb', line 16 def words @words end |
Class Method Details
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 |