Skip to content

Instantly share code, notes, and snippets.

@wflanagan
Created March 21, 2019 00:18
Show Gist options
  • Save wflanagan/e34d74a0586c0bd71a46106abd6341c8 to your computer and use it in GitHub Desktop.
Save wflanagan/e34d74a0586c0bd71a46106abd6341c8 to your computer and use it in GitHub Desktop.
module Data
class Names
def self.invalid_characters
['|']
end
@fixture_directory = File.expand_path(File.join(File.dirname(__FILE__), ".", "store"))
@file_path = '/names/'
Dir["#{@fixture_directory}#{@file_path}*.txt"].each do |file|
method_base_name = file.split("/").last.gsub('.txt', '')
define_singleton_method(method_base_name) do
open(file).readlines.map do |line|
filtered_line = invalid_characters.reduce(line) do |text, character|
text.gsub(character, '')
end
filtered_line.strip
end
end
define_singleton_method("#{method_base_name}_trie") do
return send("#{method_base_name}_trie") if send("#{method_base_name}_trie").present?
puts "Rebuilding"
trie = Trie.new
send(method_base_name).each { |name| trie.add(name) }
self.class_variable_set("@@#{method_basee_name}_trie", trie)
end
define_singleton_method("#{method_base_name}_regex") do
joined_string = send(method_base_name).join("|")
Regexp.new("\\b(#{joined_string})\\b", Regexp::IGNORECASE)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment