Sha256: bca924b9568a5215c105dc9e81cf88415cf0cfbaadcc92e0de5c0b753a500676

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require_relative '../lib/story_key'

namespace :lexicon do
  desc 'Build lib/story_key/data.rb from text files'
  task :build do
    output_file = 'lib/story_key/data.rb'
    str =
      <<~TEXT
        # frozen_string_literal: true

        # This was auto-generated by `rake lexicon:build` at #{Time.current}
        # from text files stored in /lexicons

        module StoryKey::Data
          ENTRIES = {
      TEXT
    str += [].tap do |ary|
      StoryKey::GRAMMAR.values.flatten.uniq.each do |part_of_speech|
        substr = "  #{part_of_speech}: {\n"
        substr +=
          [].tap do |ary2|
            entries_for(part_of_speech).each do |group, entries|
              elements = entries.map { |e| "'#{e}'" }.join(', ')
              ary2 << "   #{group}: [#{elements}]"
            end
          end.join(",\n")
        ary << "#{substr}\n  }"
      end
    end.join(",\n")
    str +=
      <<~TEXT

          }.freeze
        end
      TEXT
    File.write(output_file, str)
    puts "#{output_file} written"
  end

  def entries_for(part_of_speech)
    { countable: [], uncountable: [] }.tap do |data|
      Dir.glob("lexicons/#{part_of_speech}s/**/*.txt") do |path|
        group = path.split('/')[-2] == 'countable' ? :countable : :uncountable
        data[group] += txtfile_lines(path)
      end
      data.each { |group, entries| data[group] = entries.sort }
    end
  end

  def txtfile_lines(path)
    File.readlines(path)
        .map(&:strip)
        .reject { |l| l.start_with?('#') || l.blank? }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
story_key-0.3.0 rakelib/lexicon.rake
story_key-0.2.0 rakelib/lexicon.rake