Class: RakeText::Stoplist

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_text/stoplist.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_path = '') ⇒ Stoplist

Returns a new instance of Stoplist.



4
5
6
7
8
9
10
# File 'lib/rake_text/stoplist.rb', line 4

def initialize(file_path = '')
  unless file_path.empty?
    self.load_file(file_path)
  else
    self.load_default
  end
end

Instance Method Details

#add(word) ⇒ Object



24
25
26
# File 'lib/rake_text/stoplist.rb', line 24

def add(word)
  @words.push(word)
end

#clearObject



20
21
22
# File 'lib/rake_text/stoplist.rb', line 20

def clear
  @words = []
end

#load_defaultObject



12
13
14
# File 'lib/rake_text/stoplist.rb', line 12

def load_default
  self.load_file(File.join( File.dirname(__FILE__), '../../data/stoplists/default.stoplist'))
end

#load_file(file_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rake_text/stoplist.rb', line 28

def load_file(file_path)
  # Reset word array
  @words = []

  # Open the file
  file = File.new(file_path)

  # Loop the files contents
  while (line = file.gets)
    # Check if it's a comment
    if line.strip[0] == '#' then next end

    # Loop the words on the line
    line.split(/ /).each { |word| @words.push(word.strip) }
  end
end

#load_foxObject



16
17
18
# File 'lib/rake_text/stoplist.rb', line 16

def load_fox
  self.load_file(File.join( File.dirname(__FILE__), '../../data/stoplists/fox.stoplist'))
end

#patternObject



49
50
51
52
# File 'lib/rake_text/stoplist.rb', line 49

def pattern
  pattern = @words.map {|word| '\\b' + word + '\\b'}.join('|')
  return Regexp.new(pattern, Regexp::IGNORECASE)
end

#wordsObject



45
46
47
# File 'lib/rake_text/stoplist.rb', line 45

def words
  return @words
end