Class: Highscore::Wordlist

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/highscore/wordlist.rb

Overview

a basic list of words

Direct Known Subclasses

Blacklist, Bonuslist, Whitelist

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words = [], use_bloom_filter = true) ⇒ Wordlist

Returns a new instance of Wordlist.

Parameters:

  • words (defaults to: [])

    Array



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

#wordsObject (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

Parameters:

  • data

    String Array

Returns:

  • Highscore::Wordlist



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

Parameters:

  • file_path

    String

Returns:

  • Highscore::Wordlist



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

Parameters:

  • word

    String



80
81
82
83
# File 'lib/highscore/wordlist.rb', line 80

def <<(word)
  @words << word
  @bloom_filter << word unless @bloom_filter.nil?
end

#eachObject

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?

Parameters:

  • keyword

    String

Returns:

  • (Boolean)

    true/false



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

#lengthObject

count of ignored words

Returns:

  • Fixnum



54
55
56
# File 'lib/highscore/wordlist.rb', line 54

def length
  @words.length
end

#to_aObject

get an array of blacklisted words

Returns:

  • Array



61
62
63
# File 'lib/highscore/wordlist.rb', line 61

def to_a
  @words.to_a
end