text-gen 0.9.0 → 0.10.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/lib/text/gen/filter/base.rb +4 -0
- data/lib/text/gen/filter/item/locale.rb +29 -0
- data/lib/text/gen/filter/result/censor.rb +1 -1
- data/lib/text/gen/filter/result_filter.rb +2 -2
- data/lib/text/gen/filter.rb +11 -18
- data/lib/text/gen/runner.rb +17 -19
- data/lib/text/gen/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fa9bc639bc212fcea40f4d4203eb6c1efe3d8a306d1731da627e2361949c6316
|
|
4
|
+
data.tar.gz: 67e2819d32009c0ed84f8dcfdf4c166ac970167716234b5744d534dd6e9f6ee3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2012f28cd368ca0415c936424371b40dff77c59c7eccd9b2ed958daca83ba598afb6c8dfe480e7e194fec9801237580dc44fac831b6220363740ecce007e3abc
|
|
7
|
+
data.tar.gz: 56892e85ef8c3641437a8ad5d3a0ef4167ce50bae8090cf05e20ea32f374bc9bcade4c26cdb89ca7b7d023eb2684b40b75adb51763d70e06cd19a8c2631eedd6
|
data/lib/text/gen/filter/base.rb
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../item_filter"
|
|
4
|
+
|
|
5
|
+
module Text
|
|
6
|
+
module Gen
|
|
7
|
+
module Filter
|
|
8
|
+
module Item
|
|
9
|
+
class Locale < ItemFilter
|
|
10
|
+
def apply(items)
|
|
11
|
+
items.map { |item| locale_item(item) || item }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def locale_item(item)
|
|
17
|
+
meta = item["meta"]
|
|
18
|
+
return if meta.nil? || meta.empty?
|
|
19
|
+
|
|
20
|
+
locale_text = meta[key.downcase]&.sample
|
|
21
|
+
return unless locale_text
|
|
22
|
+
|
|
23
|
+
Filter.constant_item(locale_text, item)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -19,7 +19,7 @@ module Text
|
|
|
19
19
|
|
|
20
20
|
# Run each item in the builder once
|
|
21
21
|
censor_texts = builder["items"].map do |item|
|
|
22
|
-
item_result = runner.send(:run_item, item, 0)
|
|
22
|
+
item_result = runner.send(:run_item, key, item, 0)
|
|
23
23
|
apply_function(item_result.text) if item_result
|
|
24
24
|
end.compact.uniq
|
|
25
25
|
|
|
@@ -16,14 +16,14 @@ module Text
|
|
|
16
16
|
|
|
17
17
|
# Helper to create a new result preserving metadata
|
|
18
18
|
def transform_text(result, new_text)
|
|
19
|
-
Text::Gen::Result.from(text: new_text, type:
|
|
19
|
+
Text::Gen::Result.from(text: new_text, type: component_key, result: result)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# Helper to create a new result and modify metadata
|
|
23
23
|
def transform_with_meta(result, new_text = nil)
|
|
24
24
|
new_result = Text::Gen::Result.from(
|
|
25
25
|
text: new_text || result.text,
|
|
26
|
-
type:
|
|
26
|
+
type: component_key,
|
|
27
27
|
result: result
|
|
28
28
|
)
|
|
29
29
|
yield new_result if block_given?
|
data/lib/text/gen/filter.rb
CHANGED
|
@@ -18,6 +18,7 @@ require_relative "filter/result/titleize"
|
|
|
18
18
|
require_relative "filter/result/upcase"
|
|
19
19
|
|
|
20
20
|
# Item filters
|
|
21
|
+
require_relative "filter/item/locale"
|
|
21
22
|
require_relative "filter/item/select"
|
|
22
23
|
require_relative "filter/item/reject"
|
|
23
24
|
|
|
@@ -41,6 +42,7 @@ module Text
|
|
|
41
42
|
}.freeze
|
|
42
43
|
|
|
43
44
|
ITEM_FILTER_CLASSES = {
|
|
45
|
+
"locale" => Filter::Item::Locale,
|
|
44
46
|
"select" => Filter::Item::Select,
|
|
45
47
|
"reject" => Filter::Item::Reject
|
|
46
48
|
}.freeze
|
|
@@ -60,6 +62,14 @@ module Text
|
|
|
60
62
|
result
|
|
61
63
|
end
|
|
62
64
|
|
|
65
|
+
def filter_locale(items, filters)
|
|
66
|
+
locale_filters = filters_by_type(filters, "locale")
|
|
67
|
+
return items if locale_filters.empty?
|
|
68
|
+
|
|
69
|
+
filter_instance = ITEM_FILTER_CLASSES["locale"].new(locale_filters.first)
|
|
70
|
+
filter_instance.apply(items)
|
|
71
|
+
end
|
|
72
|
+
|
|
63
73
|
def filter_select(items, filters)
|
|
64
74
|
select_filters = filters_by_type(filters, "select")
|
|
65
75
|
return items if select_filters.empty?
|
|
@@ -129,24 +139,6 @@ module Text
|
|
|
129
139
|
citem
|
|
130
140
|
end
|
|
131
141
|
|
|
132
|
-
# Replace locale is used on an item; if the item is selected and has
|
|
133
|
-
# associated meta value of "locale" and the request is using that locale value
|
|
134
|
-
# then replace the item text with the value of the meta. If there is
|
|
135
|
-
# more than one value, select at random.
|
|
136
|
-
# Returns a new item with locale text and preserved value/multiplier, or nil
|
|
137
|
-
def replace_locale(item, locale)
|
|
138
|
-
return if locale.nil?
|
|
139
|
-
return if item.nil?
|
|
140
|
-
|
|
141
|
-
meta = item["meta"]
|
|
142
|
-
return if meta.nil? || meta.empty?
|
|
143
|
-
|
|
144
|
-
locale_text = meta[locale.downcase]&.sample
|
|
145
|
-
return unless locale_text
|
|
146
|
-
|
|
147
|
-
constant_item(locale_text, item)
|
|
148
|
-
end
|
|
149
|
-
|
|
150
142
|
def separator(filters)
|
|
151
143
|
return "" unless filters
|
|
152
144
|
|
|
@@ -186,6 +178,7 @@ module Text
|
|
|
186
178
|
|
|
187
179
|
# Define which filters require explicit parameters
|
|
188
180
|
FILTERS_WITH_PARAMETERS = {
|
|
181
|
+
"locale" => ["key"],
|
|
189
182
|
"swap" => ["key", "value"],
|
|
190
183
|
"meta" => ["key", "value"],
|
|
191
184
|
"censor" => ["key", "value"],
|
data/lib/text/gen/runner.rb
CHANGED
|
@@ -8,12 +8,11 @@ module Text
|
|
|
8
8
|
# Runner generates results from the builder that is found with the given key.
|
|
9
9
|
class Runner
|
|
10
10
|
attr_reader :key, :lookup, :max_attempts, :max_recursion
|
|
11
|
-
attr_accessor :unique, :
|
|
11
|
+
attr_accessor :unique, :store
|
|
12
12
|
|
|
13
13
|
def initialize(key:, lookup:, max_recursion: 10, max_attempts: 10, request_filters: [])
|
|
14
14
|
@key = key.to_s.downcase
|
|
15
15
|
@lookup = lookup
|
|
16
|
-
@locale = nil
|
|
17
16
|
@store = Store.new
|
|
18
17
|
@unique = false
|
|
19
18
|
@request_filters = request_filters
|
|
@@ -80,13 +79,13 @@ module Text
|
|
|
80
79
|
end
|
|
81
80
|
|
|
82
81
|
# A builder is hash with a key field, items, filters, and meta
|
|
83
|
-
def run_builder(
|
|
82
|
+
def run_builder(key, builder, filters, depth)
|
|
84
83
|
depth += 1
|
|
85
84
|
raise MaxRecursionError if depth > max_recursion
|
|
86
85
|
|
|
87
86
|
current_filters = merge_filters(builder, filters)
|
|
88
87
|
current_items = apply_item_filters(builder["items"], current_filters)
|
|
89
|
-
result = run_items(builder["strategy"], current_items, current_filters, builder["meta"], depth)
|
|
88
|
+
result = run_items(key, builder["strategy"], current_items, current_filters, builder["meta"], depth)
|
|
90
89
|
return unless result
|
|
91
90
|
|
|
92
91
|
result.merge_meta(builder["meta"])
|
|
@@ -98,14 +97,14 @@ module Text
|
|
|
98
97
|
Result.new(text:, type: :error)
|
|
99
98
|
end
|
|
100
99
|
|
|
101
|
-
def run_items(strategy, items, filters, meta, depth)
|
|
100
|
+
def run_items(key, strategy, items, filters, meta, depth)
|
|
102
101
|
case strategy
|
|
103
102
|
when "sequence"
|
|
104
|
-
run_item_sequence(items, filters, meta, depth)
|
|
103
|
+
run_item_sequence(key, items, filters, meta, depth)
|
|
105
104
|
when "weighted"
|
|
106
|
-
run_weighted_items(items, meta, depth)
|
|
105
|
+
run_weighted_items(key, items, meta, depth)
|
|
107
106
|
else
|
|
108
|
-
run_random_item(strategy, items, meta, depth)
|
|
107
|
+
run_random_item(key, strategy, items, meta, depth)
|
|
109
108
|
end
|
|
110
109
|
rescue StandardError => e
|
|
111
110
|
error_result("{#{e.class.name}:#{e.message}}")
|
|
@@ -121,23 +120,23 @@ module Text
|
|
|
121
120
|
items[index]
|
|
122
121
|
end
|
|
123
122
|
|
|
124
|
-
def run_random_item(strategy, items, meta, depth)
|
|
123
|
+
def run_random_item(key, strategy, items, meta, depth)
|
|
125
124
|
item = random_item(strategy, items)
|
|
126
|
-
result = run_item(item, depth)
|
|
125
|
+
result = run_item(key, item, depth)
|
|
127
126
|
return unless result
|
|
128
127
|
|
|
129
128
|
result.merge_meta(meta)
|
|
130
129
|
result
|
|
131
130
|
end
|
|
132
131
|
|
|
133
|
-
def run_item_sequence(items, filters, meta, depth)
|
|
134
|
-
results = items.map { |item| run_item(item, depth) }.compact
|
|
132
|
+
def run_item_sequence(key, items, filters, meta, depth)
|
|
133
|
+
results = items.map { |item| run_item(key, item, depth) }.compact
|
|
135
134
|
return if results.empty?
|
|
136
135
|
|
|
137
136
|
Result.merge(results, filters:, meta:, type: :sequence)
|
|
138
137
|
end
|
|
139
138
|
|
|
140
|
-
def run_weighted_items(items, meta, depth)
|
|
139
|
+
def run_weighted_items(key, items, meta, depth)
|
|
141
140
|
total_weight = items.sum { |item| [item.fetch("weight", 1).to_i, 1].max }
|
|
142
141
|
rand_weight = rand(total_weight)
|
|
143
142
|
current_weight = 0
|
|
@@ -145,23 +144,21 @@ module Text
|
|
|
145
144
|
current_weight += [item.fetch("weight", 1).to_i, 1].max
|
|
146
145
|
current_weight > rand_weight
|
|
147
146
|
end
|
|
148
|
-
result = run_item(item, depth)
|
|
147
|
+
result = run_item(key, item, depth)
|
|
149
148
|
return unless result
|
|
150
149
|
|
|
151
150
|
result.merge_meta(meta)
|
|
152
151
|
result
|
|
153
152
|
end
|
|
154
153
|
|
|
155
|
-
def run_item(item, depth)
|
|
156
|
-
locale_item = Filter.replace_locale(item, locale)
|
|
157
|
-
item = locale_item || item
|
|
158
|
-
|
|
154
|
+
def run_item(key, item, depth)
|
|
159
155
|
results = item["segments"].map { |seg| run_segment(seg, depth) }
|
|
160
156
|
result = Result.merge(results,
|
|
161
157
|
value: item["value"],
|
|
162
158
|
multiplier: item["multiplier"],
|
|
163
159
|
filters: item["filters"],
|
|
164
|
-
meta: item["meta"]
|
|
160
|
+
meta: item["meta"],
|
|
161
|
+
type: key)
|
|
165
162
|
result = apply_result_function(result, item["filters"])
|
|
166
163
|
apply_result_filters(result, item["filters"])
|
|
167
164
|
end
|
|
@@ -217,6 +214,7 @@ module Text
|
|
|
217
214
|
end
|
|
218
215
|
|
|
219
216
|
def apply_item_filters(items, filters)
|
|
217
|
+
items = Filter.filter_locale(items, @request_filters + filters)
|
|
220
218
|
items = Filter.filter_select(items, filters)
|
|
221
219
|
items = Filter.filter_reject(items, filters)
|
|
222
220
|
raise FilterError if items.empty?
|
data/lib/text/gen/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: text-gen
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- G Palmer
|
|
@@ -43,6 +43,7 @@ files:
|
|
|
43
43
|
- lib/text/gen.rb
|
|
44
44
|
- lib/text/gen/filter.rb
|
|
45
45
|
- lib/text/gen/filter/base.rb
|
|
46
|
+
- lib/text/gen/filter/item/locale.rb
|
|
46
47
|
- lib/text/gen/filter/item/reject.rb
|
|
47
48
|
- lib/text/gen/filter/item/select.rb
|
|
48
49
|
- lib/text/gen/filter/item_filter.rb
|