well_read_faker 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.travis.yml +1 -1
- data/README.adoc +38 -1
- data/lib/well_read_faker/load_bundled_sources.rb +1 -0
- data/lib/well_read_faker/source.rb +56 -18
- data/lib/well_read_faker/version.rb +1 -1
- data/well_read_faker.gemspec +1 -0
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72f76968ddb4ab56cddb5fa3ddaf0ef3c296e5eb
|
4
|
+
data.tar.gz: 05a46f05bae05e1ff7baf49820b44cb1df6a7eae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54bb4b982dd7a15bdc1c986a0b1713859f6e09503bf9ef867974db2f01d6eaf42d49884c6ca23d96a9deed880de14f8f28125fd9a93a0c6c8a8f2ce6126dc271
|
7
|
+
data.tar.gz: 535ef04cb097a9fe9311b0d0d65cd9099240a2456386a89a4febbdea5a7988b98e22472fdc5eb2796b71c28f230a5eb23b0cc17f1b9fb144d20636c8d1ffd597
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.adoc
CHANGED
@@ -23,7 +23,7 @@ following piece of code:
|
|
23
23
|
puts WellReadFaker.paragraph
|
24
24
|
--------------------------------------------------------------------------------
|
25
25
|
|
26
|
-
Could print:
|
26
|
+
Could print (without newlines):
|
27
27
|
|
28
28
|
--------------------------------------------------------------------------------
|
29
29
|
Thus they prayed, but not as yet would Jove grant them their prayer.
|
@@ -33,6 +33,43 @@ dare not with my own eyes witness this fight between my son and
|
|
33
33
|
Menelaus, for Jove and the other immortals alone know which shall fall."
|
34
34
|
--------------------------------------------------------------------------------
|
35
35
|
|
36
|
+
== Custom sources
|
37
|
+
|
38
|
+
Well Read Faker comes with "Iliad" bundled as a default source. However,
|
39
|
+
custom sources can be used:
|
40
|
+
|
41
|
+
[source,ruby]
|
42
|
+
--------------------------------------------------------------------------------
|
43
|
+
WellReadFaker.add_source :book_in_my_language, "path/to/book", {}
|
44
|
+
WellReadFaker[:book_in_my_language].paragraph
|
45
|
+
WellReadFaker.default_source = :book_in_my_language
|
46
|
+
WellReadFaker.paragraph
|
47
|
+
WellReadFaker[:iliad].paragraph # Iliad can be still accessed
|
48
|
+
--------------------------------------------------------------------------------
|
49
|
+
|
50
|
+
The second argument passed to +WellReadFaker::add_source+ can be a string
|
51
|
+
containing a file path, or any object which responds to +#read+, like +File+
|
52
|
+
instance.
|
53
|
+
|
54
|
+
The third argument is optional but can be used to supply additional options,
|
55
|
+
for example to skip book descriptor or legal notes. See
|
56
|
+
+lib/well_read_faker/load_bundled_sources.rb+ for examples.
|
57
|
+
|
58
|
+
In the source text, paragraphs must be separated with blank lines.
|
59
|
+
|
60
|
+
== Note about randomness quality
|
61
|
+
|
62
|
+
For given text source, the paragraphs are returned in a random order, but will
|
63
|
+
not be repeated (in the sense of +String#==+ equality) until all unique
|
64
|
+
paragraphs from that text are returned exactly once. After that, they are
|
65
|
+
returned once again in the same order.
|
66
|
+
|
67
|
+
Therefore it is guaranteed that if given source text contains +n+ unique
|
68
|
+
paragraphs, then +n+ subsequent calls of +#paragraph+ method will return
|
69
|
+
unique values.
|
70
|
+
|
71
|
+
For your information, The Iliad contains over 1000 unique paragraphs.
|
72
|
+
|
36
73
|
== License
|
37
74
|
|
38
75
|
The gem is available as open source under the terms of the
|
@@ -4,45 +4,83 @@ module WellReadFaker
|
|
4
4
|
class Source
|
5
5
|
include Mutex_m
|
6
6
|
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :path_to_book_or_io, :options
|
8
8
|
|
9
|
-
def initialize
|
9
|
+
def initialize path_to_book_or_io, options = {}
|
10
10
|
super()
|
11
|
-
@
|
11
|
+
@path_to_book_or_io = path_to_book_or_io
|
12
12
|
@options = options
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
|
15
|
+
def paragraph
|
16
|
+
ensure_loaded
|
17
|
+
@paragraphs_arr[inc_paragraphs]
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
-
|
20
|
+
def ensure_loaded
|
21
|
+
@loaded or mu_synchronize{ @loaded or load }
|
21
22
|
end
|
22
23
|
|
23
24
|
private
|
24
25
|
|
25
26
|
def load
|
26
|
-
|
27
|
-
|
27
|
+
if path_to_book_or_io.respond_to? :read
|
28
|
+
raw = path_to_book_or_io.read
|
29
|
+
else
|
30
|
+
raw = File.read path_to_book_or_io
|
31
|
+
end
|
32
|
+
|
33
|
+
process_raw_text raw
|
34
|
+
@loaded = true
|
28
35
|
end
|
29
36
|
|
30
37
|
def process_raw_text raw
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
38
|
+
trimmed = trim_text_by_regexps raw, options[:begin], options[:end]
|
39
|
+
|
40
|
+
@paragraphs_arr = trimmed.split(/\n\s*\n/)
|
41
|
+
@paragraphs_arr.map!{ |m| m.gsub /\s*\n\s*/, " " }
|
42
|
+
@paragraphs_arr.uniq!
|
43
|
+
if options[:min_words]
|
44
|
+
@paragraphs_arr.select!{ |m| /(\w+\b\W*){#{options[:min_words]}}/ =~ m }
|
45
|
+
end
|
46
|
+
|
47
|
+
shuffle_array @paragraphs_arr
|
48
|
+
@paragraphs_idx = -1
|
49
|
+
end
|
50
|
+
|
51
|
+
def trim_text_by_regexps source_text, begin_rx, end_rx
|
52
|
+
retval = source_text.dup
|
53
|
+
|
54
|
+
if begin_rx
|
55
|
+
match_data = begin_rx.match(retval) or raise(
|
35
56
|
ArgumentError,
|
36
|
-
"Regular expression #{
|
57
|
+
"Regular expression #{begin_rx.inspect} not found in text."
|
37
58
|
)
|
59
|
+
retval[0..match_data.begin(0)-1] = ""
|
38
60
|
end
|
39
|
-
|
40
|
-
|
61
|
+
|
62
|
+
if end_rx
|
63
|
+
match_data = end_rx.match(retval) or raise(
|
41
64
|
ArgumentError,
|
42
|
-
"Regular expression #{
|
65
|
+
"Regular expression #{end_rx.inspect} not found in text."
|
43
66
|
)
|
67
|
+
retval[match_data.begin(0)..-1] = ""
|
68
|
+
end
|
69
|
+
|
70
|
+
retval.strip!
|
71
|
+
retval
|
72
|
+
end
|
73
|
+
|
74
|
+
def inc_paragraphs
|
75
|
+
mu_synchronize do
|
76
|
+
return @paragraphs_idx = (@paragraphs_idx + 1) % @paragraphs_arr.size
|
44
77
|
end
|
45
|
-
|
78
|
+
end
|
79
|
+
|
80
|
+
# Extracted to a separate method to make stubbing easy (sometimes we want
|
81
|
+
# to preserve natural order in specs)
|
82
|
+
def shuffle_array array
|
83
|
+
array.sort_by!{ rand }
|
46
84
|
end
|
47
85
|
|
48
86
|
end
|
data/well_read_faker.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.14"
|
24
|
+
spec.add_development_dependency "pry"
|
24
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
26
|
spec.add_development_dependency "rspec", "~> 3.5"
|
26
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: well_read_faker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastian Skałacki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
111
|
version: '0'
|
98
112
|
requirements: []
|
99
113
|
rubyforge_project:
|
100
|
-
rubygems_version: 2.6.
|
114
|
+
rubygems_version: 2.6.11
|
101
115
|
signing_key:
|
102
116
|
specification_version: 4
|
103
117
|
summary: Returns random fragments of Iliad or other book.
|