text-gen 0.9.0 → 0.9.1
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/result/censor.rb +1 -1
- data/lib/text/gen/filter/result_filter.rb +2 -2
- data/lib/text/gen/runner.rb +15 -14
- data/lib/text/gen/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: bd9670884657a6180bafe5eb9afdd1625cba971f3f713b5d176c6dccad309d37
|
|
4
|
+
data.tar.gz: e236d1dda136729987647f723d6428ac5899c0f2109435e49b185eee978fde11
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8f6714227cf6ca68ac3514d1138ed4d24973eab26ab89ee363ccd68a9896bfc2f5cd826c85884def446d1b7a7c11489c304bbfc29d4a06e430ca239de90eb5b2
|
|
7
|
+
data.tar.gz: db65c0508e82bc14714ed80e7308f879bac3d5e07e7035a41eeb8eb17a4d809c4d195dfdbc5402c2cbafff8be92d9ee71cce4014b9f7f00d03e1f304a4d1d447
|
data/lib/text/gen/filter/base.rb
CHANGED
|
@@ -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/runner.rb
CHANGED
|
@@ -80,13 +80,13 @@ module Text
|
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
# A builder is hash with a key field, items, filters, and meta
|
|
83
|
-
def run_builder(
|
|
83
|
+
def run_builder(key, builder, filters, depth)
|
|
84
84
|
depth += 1
|
|
85
85
|
raise MaxRecursionError if depth > max_recursion
|
|
86
86
|
|
|
87
87
|
current_filters = merge_filters(builder, filters)
|
|
88
88
|
current_items = apply_item_filters(builder["items"], current_filters)
|
|
89
|
-
result = run_items(builder["strategy"], current_items, current_filters, builder["meta"], depth)
|
|
89
|
+
result = run_items(key, builder["strategy"], current_items, current_filters, builder["meta"], depth)
|
|
90
90
|
return unless result
|
|
91
91
|
|
|
92
92
|
result.merge_meta(builder["meta"])
|
|
@@ -98,14 +98,14 @@ module Text
|
|
|
98
98
|
Result.new(text:, type: :error)
|
|
99
99
|
end
|
|
100
100
|
|
|
101
|
-
def run_items(strategy, items, filters, meta, depth)
|
|
101
|
+
def run_items(key, strategy, items, filters, meta, depth)
|
|
102
102
|
case strategy
|
|
103
103
|
when "sequence"
|
|
104
|
-
run_item_sequence(items, filters, meta, depth)
|
|
104
|
+
run_item_sequence(key, items, filters, meta, depth)
|
|
105
105
|
when "weighted"
|
|
106
|
-
run_weighted_items(items, meta, depth)
|
|
106
|
+
run_weighted_items(key, items, meta, depth)
|
|
107
107
|
else
|
|
108
|
-
run_random_item(strategy, items, meta, depth)
|
|
108
|
+
run_random_item(key, strategy, items, meta, depth)
|
|
109
109
|
end
|
|
110
110
|
rescue StandardError => e
|
|
111
111
|
error_result("{#{e.class.name}:#{e.message}}")
|
|
@@ -121,23 +121,23 @@ module Text
|
|
|
121
121
|
items[index]
|
|
122
122
|
end
|
|
123
123
|
|
|
124
|
-
def run_random_item(strategy, items, meta, depth)
|
|
124
|
+
def run_random_item(key, strategy, items, meta, depth)
|
|
125
125
|
item = random_item(strategy, items)
|
|
126
|
-
result = run_item(item, depth)
|
|
126
|
+
result = run_item(key, item, depth)
|
|
127
127
|
return unless result
|
|
128
128
|
|
|
129
129
|
result.merge_meta(meta)
|
|
130
130
|
result
|
|
131
131
|
end
|
|
132
132
|
|
|
133
|
-
def run_item_sequence(items, filters, meta, depth)
|
|
134
|
-
results = items.map { |item| run_item(item, depth) }.compact
|
|
133
|
+
def run_item_sequence(key, items, filters, meta, depth)
|
|
134
|
+
results = items.map { |item| run_item(key, item, depth) }.compact
|
|
135
135
|
return if results.empty?
|
|
136
136
|
|
|
137
137
|
Result.merge(results, filters:, meta:, type: :sequence)
|
|
138
138
|
end
|
|
139
139
|
|
|
140
|
-
def run_weighted_items(items, meta, depth)
|
|
140
|
+
def run_weighted_items(key, items, meta, depth)
|
|
141
141
|
total_weight = items.sum { |item| [item.fetch("weight", 1).to_i, 1].max }
|
|
142
142
|
rand_weight = rand(total_weight)
|
|
143
143
|
current_weight = 0
|
|
@@ -145,14 +145,14 @@ module Text
|
|
|
145
145
|
current_weight += [item.fetch("weight", 1).to_i, 1].max
|
|
146
146
|
current_weight > rand_weight
|
|
147
147
|
end
|
|
148
|
-
result = run_item(item, depth)
|
|
148
|
+
result = run_item(key, item, depth)
|
|
149
149
|
return unless result
|
|
150
150
|
|
|
151
151
|
result.merge_meta(meta)
|
|
152
152
|
result
|
|
153
153
|
end
|
|
154
154
|
|
|
155
|
-
def run_item(item, depth)
|
|
155
|
+
def run_item(key, item, depth)
|
|
156
156
|
locale_item = Filter.replace_locale(item, locale)
|
|
157
157
|
item = locale_item || item
|
|
158
158
|
|
|
@@ -161,7 +161,8 @@ module Text
|
|
|
161
161
|
value: item["value"],
|
|
162
162
|
multiplier: item["multiplier"],
|
|
163
163
|
filters: item["filters"],
|
|
164
|
-
meta: item["meta"]
|
|
164
|
+
meta: item["meta"],
|
|
165
|
+
type: key)
|
|
165
166
|
result = apply_result_function(result, item["filters"])
|
|
166
167
|
apply_result_filters(result, item["filters"])
|
|
167
168
|
end
|
data/lib/text/gen/version.rb
CHANGED