Class: LitIpsum::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/lit_ipsum.rb
Class Method Summary
collapse
Class Method Details
.get_text(filename) ⇒ Object
13
14
15
16
17
18
19
20
21
22
|
# 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:) ⇒ Object
24
25
26
27
28
29
30
31
32
|
# 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:) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# 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
|