class LitIpsum::Base
Public Class Methods
get_text(filename)
click to toggle source
# File lib/lit_ipsum.rb, line 13 def get_text(filename) File.open(filename, 'r') do |file| return file.map(&:chomp) .reject { |line| line.empty? || line.start_with?('#') } .map { |line| line.delete('_') } .join(' ') .scan(SENTENCE_PATTERN) .uniq end end
sentences(count, max_sentence:, filename:, repeats:)
click to toggle source
# File lib/lit_ipsum.rb, line 24 def sentences(count, max_sentence:, filename:, repeats:) source = max_sentence.zero? ? get_text(filename) : get_text(filename).select { |sentence| sentence.length <= max_sentence } raise Error, "Unable to find sentences of length <= #{max_sentence}." if source.empty? obj = [] count.times { obj << source.sample } repeat(repeats, obj) end
words(count, filename:, repeats:)
click to toggle source
# File lib/lit_ipsum.rb, line 34 def words(count, filename:, repeats:) source = get_text(filename).select { |sentence| sentence.scan(/\w+/).size <= count } obj = [] loop do length_in_words = obj.map { |el| el.scan(/\w+/) }.flatten.length obj << source.select { |sentence| sentence.scan(/\w+/).size <= count - length_in_words }.sample break if length_in_words == count end repeat(repeats, obj) end
Private Class Methods
full_filename(txt)
click to toggle source
# File lib/lit_ipsum.rb, line 58 def full_filename(txt) File.join(File.dirname(File.expand_path(__FILE__)), txt) end
repeat(repeats, obj)
click to toggle source
# File lib/lit_ipsum.rb, line 48 def repeat(repeats, obj) phrases = [] if !repeats.nil? repeats.times { phrases << obj.join(' ') } phrases.join("\n") else obj.join(' ') end end