Module: LoadData

Included in:
Tagmemics::WordSet
Defined in:
lib/tagmemics/load_data.rb

Overview

Retrieves data from config folder to save to constants.

Class Method Summary collapse

Class Method Details

.config_pathObject



3
4
5
# File 'lib/tagmemics/load_data.rb', line 3

def self.config_path
  File.join(File.dirname(__FILE__), '../../config')
end

.contents_to_a(part_of_speech) ⇒ Object



16
17
18
# File 'lib/tagmemics/load_data.rb', line 16

def self.contents_to_a(part_of_speech)
  list_contents(part_of_speech).split("\n")
end

.list_contents(part_of_speech) ⇒ Object



12
13
14
# File 'lib/tagmemics/load_data.rb', line 12

def self.list_contents(part_of_speech)
  File.new(list_path(part_of_speech), 'r:utf-8').read
end

.list_path(part_of_speech) ⇒ Object

Returns the absolute path to the adjectives list



8
9
10
# File 'lib/tagmemics/load_data.rb', line 8

def self.list_path(part_of_speech)
  File.join(config_path, "#{part_of_speech}.txt")
end

.update_list(part_of_speech, uri, css_selector) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tagmemics/load_data.rb', line 20

def self.update_list(part_of_speech, uri, css_selector)
  require 'mechanize'

  agent = Mechanize.new
  page = agent.get(uri)
  destination = "./config/#{part_of_speech}.txt"
  target = page.search(css_selector)
  regx = /[^\047a-zA-Z\s]/ # \047 is an apostrophe

  arr = []
  target.each do |x|
    x = x.text
    x.gsub(/\r\n\s/, "\n").split("\n").each do |word|
      next if arr.include? word
      next if regx =~ word
      arr << word
    end
  end
  arr.sort!

  puts "Starting list from #{uri}"
  puts "There are #{arr.count} #{part_of_speech} to save."

  File.open(destination, 'w') do |line|
    arr.each do |word|
      line << word + "\n"
    end
  end

  puts 'Save complete.'
end