class RandomWords::Source

A class representing a source of words for the RandomWords library.

def bad_init

@!visibility private
Bad init method for testing purposes
def bad_init
  from_file('nouns-noent.txt')
end

def from_file(filename)

Returns:
  • (Array) - An array of words loaded from the file

Parameters:
  • filename (String) -- The name of the file to load
def from_file(filename)
  filename = "#{filename.sub(/\.txt$/, '')}.txt"
  path = File.join(@path, filename)
  File.read(path).strip.split("\n").map(&:strip) # Changed from split_lines to split("\n")
rescue Errno::ENOENT
  warn "File not found: #{path}"
  []
end

def from_files

Returns:
  • (Hash) - A hash containing arrays of words for each part of speech
def from_files
  {
    nouns: from_file('nouns-singular'),
    plural_nouns: from_file('nouns-plural'),
    verbs: from_file('verbs-singular'),
    plural_verbs: from_file('verbs-plural'),
    passive_verbs: from_file('verbs-passive'),
    adverbs: from_file('adverbs'),
    adjectives: from_file('adjectives'),
    articles: from_file('articles-singular'),
    plural_articles: from_file('articles-plural'),
    prepositions: from_file('prepositions'),
    clauses: from_file('clauses'),
    coordinating_conjunctions: from_file('conjunctions-coordinating'),
    subordinate_conjunctions: from_file('conjunctions-subordinate'),
    numbers: from_yaml('numbers'),
    phrases: from_file('phrases')
  }
end

def from_yaml(filename)

Returns:
  • (Hash) - A hash of words loaded from the YAML file

Parameters:
  • filename (String) -- The name of the YAML file to load
def from_yaml(filename)
  path = File.join(@path, "#{filename}.yml")
  return {} unless File.exist?(path)
  begin
    nums = YAML.safe_load(File.read(path), aliases: true).symbolize_keys
    nums.keys.each do |key|
      nums[key] = nums[key].split(' ').map(&:strip) if nums[key].is_a?(String)
    end
    nums
  rescue Psych::SyntaxError => e
    warn "YAML syntax error in #{path}: #{e.message}"
    {}
  end
end

def initialize(name, path)

Parameters:
  • path (String) -- The path to the source directory
  • name (Symbol) -- The name of the source
def initialize(name, path)
  @name = name.to_sym
  @path = path
  config = IO.read(File.join(path, 'config.yml'))
  @config = YAML.safe_load(config, aliases: true)
  @names = @config['triggers'] || [name]
  @description = @config['description'] || 'No description available'
  @dictionary = from_files
  @dictionary[:all_words] = @dictionary.values.flatten.uniq
  @dictionary[:terminators], @dictionary[:extended_punctuation] = from_file('terminators').split_terminators
  @dictionary[:first_names], @dictionary[:last_names], @dictionary[:full_names] = from_file('names').split_names
rescue StandardError
  @name = name.to_sym
  @config = {}
  @names = [name]
  @description = 'No description available'
  @dictionary = from_files
  @dictionary[:all_words] = @dictionary.values.flatten.uniq
  @dictionary[:terminators], @dictionary[:extended_punctuation] = from_file('terminators').split_terminators
  @dictionary[:first_names], @dictionary[:last_names], @dictionary[:full_names] = from_file('names').split_names
end