Class: Highscore::Wordlist
- Inherits:
-
Object
- Object
- Highscore::Wordlist
- Includes:
- Enumerable
- Defined in:
- lib/highscore/wordlist.rb
Overview
a basic list of words
Instance Attribute Summary collapse
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Class Method Summary collapse
-
.load(data, use_bloom_filter = true) ⇒ Object
load a file or array of words.
-
.load_file(file_path, use_bloom_filter = true) ⇒ Object
load a file of keywords.
Instance Method Summary collapse
-
#<<(word) ⇒ Object
add a new word to the blacklist.
-
#each ⇒ Object
iterate over words.
-
#include?(keyword) ⇒ Boolean
does the blacklist contain this keyword?.
-
#initialize(words = [], use_bloom_filter = true) ⇒ Wordlist
constructor
A new instance of Wordlist.
-
#length ⇒ Object
count of ignored words.
-
#to_a ⇒ Object
get an array of blacklisted words.
Constructor Details
#initialize(words = [], use_bloom_filter = true) ⇒ Wordlist
Returns a new instance of Wordlist.
37 38 39 40 41 42 43 |
# File 'lib/highscore/wordlist.rb', line 37 def initialize(words = [], use_bloom_filter = true) @words = words @bloom_filter = nil @use_bloom_filter = use_bloom_filter init_bloom_filter end |
Instance Attribute Details
#words ⇒ Object (readonly)
Returns the value of attribute words.
34 35 36 |
# File 'lib/highscore/wordlist.rb', line 34 def words @words end |
Class Method Details
.load(data, use_bloom_filter = true) ⇒ Object
load a file or array of words
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/highscore/wordlist.rb', line 20 def self.load(data, use_bloom_filter = true) if data.instance_of?(String) words = data.split(' ') elsif data.instance_of? Array words = data else raise ArgumentError, "don't know how to handle a %s class" % data.class end words.map! {|x| x.gsub(/[\!\.\:\,\;\-\+]/, '') } self.new(words, use_bloom_filter) end |
.load_file(file_path, use_bloom_filter = true) ⇒ Object
load a file of keywords
11 12 13 14 |
# File 'lib/highscore/wordlist.rb', line 11 def self.load_file(file_path, use_bloom_filter = true) words = File.read(file_path).split(' ') self.load(words, use_bloom_filter) end |
Instance Method Details
#<<(word) ⇒ Object
add a new word to the blacklist
80 81 82 83 |
# File 'lib/highscore/wordlist.rb', line 80 def <<(word) @words << word @bloom_filter << word unless @bloom_filter.nil? end |
#each ⇒ Object
iterate over words
47 48 49 |
# File 'lib/highscore/wordlist.rb', line 47 def each @words.each {|word| yield word } end |
#include?(keyword) ⇒ Boolean
does the blacklist contain this keyword?
69 70 71 72 73 74 75 |
# File 'lib/highscore/wordlist.rb', line 69 def include?(keyword) unless @bloom_filter.nil? @bloom_filter.include? keyword else @words.include? keyword end end |
#length ⇒ Object
count of ignored words
54 55 56 |
# File 'lib/highscore/wordlist.rb', line 54 def length @words.length end |
#to_a ⇒ Object
get an array of blacklisted words
61 62 63 |
# File 'lib/highscore/wordlist.rb', line 61 def to_a @words.to_a end |