text-gen 0.6.1 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49044057cb087e22b62a113c84d6575c3bd23e1320a841cbebf650bd244c0edb
4
- data.tar.gz: c4a93ede17d96f95f218060503d4d9cd527e69c3d75c3759bb2479fe6dbbc47f
3
+ metadata.gz: a45d470fc7741737db5056376f2d72ce0aba37634166e19038249a860c23dee0
4
+ data.tar.gz: 394a4dca77c33b151bb64b83f92e717f6ddff0fa653f20ffa8bcf508f7bbad02
5
5
  SHA512:
6
- metadata.gz: 606e6034518c44efad32c68b559daff0dabad3d96e09d3bd22394b47e77c03d17f0aa3259e812d285a226c070f4223b1627204ff07a71d54ab733a8da5ec611e
7
- data.tar.gz: b3deabab0cd407e5723ad275cdbda413728b515b62d836bf69a4a75642dae9082165956f0f2b5c09ab8adaa7936a0bdb6713a8eb89bec6b206859a905c7aed8f
6
+ metadata.gz: 92c649a990e045d937b503bcd9eaa2ac3e64c1f60a3846ba4842b410f4b063e27772c35b3fe53a9b95c998dfdcaf568edef03b077686c37e075539dda72d229a
7
+ data.tar.gz: c5da5b4dcff94578ff48fa3b03ebd197376baf67755d4dbc0b23f8bc2e857b3ec7e4be8afee209b97aefa417b4f1008f1c8dd7c370e5fe1e0ee1ee44de6a7464
@@ -1,46 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "function/capitalizer"
4
+ require_relative "function/clear"
5
+ require_relative "function/downcase"
6
+ require_relative "function/meta"
4
7
  require_relative "function/pluralizer"
5
8
  require_relative "function/swapper"
6
9
  require_relative "function/titleizer"
10
+ require_relative "function/upcase"
7
11
 
8
12
  module Text
9
13
  module Gen
10
14
  class Filter
11
15
  SEPARATORS = { "tab" => "\t", "newline" => "\n", "space" => " " }.freeze
16
+
17
+ FUNCTION_CLASSES = {
18
+ "capitalize" => Function::Capitalizer,
19
+ "clear" => Function::Clear,
20
+ "downcase" => Function::Downcase,
21
+ "meta" => Function::Meta,
22
+ "pluralize" => Function::Pluralizer,
23
+ "swap" => Function::Swapper,
24
+ "titleize" => Function::Titleizer,
25
+ "upcase" => Function::Upcase
26
+ }.freeze
27
+
12
28
  class << self
13
29
  def functions(result, filters)
14
30
  return result if filters.nil? || filters.empty?
15
31
 
16
32
  filters.each do |filter|
17
- case filter["type"]
18
- when "capitalize"
19
- text = Function::Capitalizer.capitalize(result.text)
20
- result = Result.from(text:, type: :function, result:)
21
- when "downcase"
22
- result = Result.from(text: result.text.downcase, type: :function, result:)
23
- when "upcase"
24
- result = Result.from(text: result.text.upcase, type: :function, result:)
25
- when "titleize"
26
- text = Function::Titleizer.titleize(result.text)
27
- result = Result.from(text:, type: :function, result:)
28
- when "pluralize"
29
- text = Function::Pluralizer.pluralize(result.text)
30
- result = Result.from(text:, type: :function, result:)
31
- when "clear"
32
- result = Result.from(text: result.text, type: :function, result:)
33
- result.clear_meta(filter["key"], filter["value"])
34
- result
35
- when "meta"
36
- result = Result.from(text: result.text, type: :function, result:)
37
- result.merge_kv(filter["key"], filter["value"])
38
- result
39
- when "swap"
40
- text = Function::Swapper.swap(result.text, filter["key"], filter["value"])
41
- result = Result.from(text:, type: :function, result:)
42
- result
43
- end
33
+ function_class = FUNCTION_CLASSES[filter["type"]]
34
+ result = function_class.apply(result, filter) if function_class
44
35
  end
45
36
 
46
37
  result
@@ -117,11 +108,6 @@ module Text
117
108
  locale_text = meta[locale.downcase]&.sample
118
109
  return unless locale_text
119
110
 
120
- # Create new item with locale text, preserving value and multiplier
121
- # new_item = constant_item(locale_text)
122
- # new_item["value"] = item["value"] if item["value"]
123
- # new_item["multiplier"] = item["multiplier"] if item["multiplier"]
124
- # new_item
125
111
  constant_item(locale_text, item)
126
112
  end
127
113
 
@@ -150,15 +136,11 @@ module Text
150
136
  end
151
137
 
152
138
  def filter_by_type(filters, type)
153
- return if filters.nil?
154
- return if filters.empty?
155
-
156
- filters.find { |f| f["type"] == type }
139
+ filters&.find { |f| f["type"] == type }
157
140
  end
158
141
 
159
142
  def filters_by_type(filters, type)
160
- return [] if filters.nil?
161
- return filters if filters.empty?
143
+ return [] if filters.nil? || filters.empty?
162
144
 
163
145
  filters.select { |f| f["type"] == type }
164
146
  end
@@ -5,8 +5,9 @@ module Text
5
5
  module Function
6
6
  class Capitalizer
7
7
  class << self
8
- def capitalize(str)
9
- str.capitalize
8
+ def apply(result, _filter)
9
+ text = result.text.capitalize
10
+ Result.from(text:, type: :function, result:)
10
11
  end
11
12
  end
12
13
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Text
4
+ module Gen
5
+ module Function
6
+ class Clear
7
+ class << self
8
+ def apply(result, filter)
9
+ new_result = Result.from(text: result.text, type: :function, result:)
10
+ new_result.clear_meta(filter["key"], filter["value"])
11
+ new_result
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Text
4
+ module Gen
5
+ module Function
6
+ class Downcase
7
+ class << self
8
+ def apply(result, _filter)
9
+ text = result.text.downcase
10
+ Result.from(text:, type: :function, result:)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Text
4
+ module Gen
5
+ module Function
6
+ class Meta
7
+ class << self
8
+ def apply(result, filter)
9
+ new_result = Result.from(text: result.text, type: :function, result:)
10
+ new_result.merge_kv(filter["key"], filter["value"])
11
+ new_result
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -27,6 +27,11 @@ module Text
27
27
  SINGLE = Set.new(%w[a an the this his her its my your our that their])
28
28
 
29
29
  class << self
30
+ def apply(result, _filter)
31
+ text = pluralize(result.text)
32
+ Result.from(text:, type: :function, result:)
33
+ end
34
+
30
35
  def pluralize(str)
31
36
  return str if str.empty?
32
37
 
@@ -4,12 +4,21 @@ module Text
4
4
  module Gen
5
5
  module Function
6
6
  class Swapper
7
+ DIGIT_WORDS = {
8
+ "1" => "one", "2" => "two", "3" => "three",
9
+ "4" => "four", "5" => "five", "6" => "six",
10
+ "7" => "seven", "8" => "eight", "9" => "nine"
11
+ }.freeze
12
+
7
13
  class << self
14
+ def apply(result, filter)
15
+ text = swap(result.text, filter["key"], filter["value"])
16
+ Result.from(text:, type: :function, result:)
17
+ end
18
+
8
19
  def swap(str, key, val)
9
20
  return str if key.nil? || key.empty?
10
21
 
11
- puts("str=#{str} key=#{key} val=#{val}")
12
-
13
22
  arr = str.split(/\s+/)
14
23
  arr = arr.map do |s|
15
24
  case key
@@ -27,28 +36,7 @@ module Text
27
36
  end
28
37
 
29
38
  def swap_digit(str)
30
- case str
31
- when "1"
32
- "one"
33
- when "2"
34
- "two"
35
- when "3"
36
- "three"
37
- when "4"
38
- "four"
39
- when "5"
40
- "five"
41
- when "6"
42
- "six"
43
- when "7"
44
- "seven"
45
- when "8"
46
- "eight"
47
- when "9"
48
- "nine"
49
- else
50
- str
51
- end
39
+ DIGIT_WORDS.fetch(str, str)
52
40
  end
53
41
  end
54
42
  end
@@ -7,15 +7,15 @@ module Text
7
7
  SKIP_WORDS = Set.new(%w[a an and as at but by for if in of on or the to v via vs])
8
8
 
9
9
  class << self
10
+ def apply(result, _filter)
11
+ text = titleize(result.text)
12
+ Result.from(text:, type: :function, result:)
13
+ end
14
+
10
15
  def titleize(str)
11
- arr = str.split(/\s+/)
12
- idx = 0
13
- len = arr.length
14
- while idx < len
15
- arr[idx] = arr[idx].capitalize if idx.zero? || !SKIP_WORDS.include?(arr[idx])
16
- idx += 1
17
- end
18
- arr.join(" ")
16
+ str.split(/\s+/).map.with_index do |word, idx|
17
+ idx.zero? || !SKIP_WORDS.include?(word) ? word.capitalize : word
18
+ end.join(" ")
19
19
  end
20
20
  end
21
21
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Text
4
+ module Gen
5
+ module Function
6
+ class Upcase
7
+ class << self
8
+ def apply(result, _filter)
9
+ text = result.text.upcase
10
+ Result.from(text:, type: :function, result:)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -43,6 +43,29 @@ module Text
43
43
  end
44
44
  end
45
45
 
46
+ def investigate
47
+ visit_and_recurse(key, 0).sort.uniq
48
+ end
49
+
50
+ def visit_and_recurse(key, depth)
51
+ depth += 1
52
+ raise MaxRecursionError if depth > max_recursion
53
+
54
+ builder = fetch_builder(key)
55
+ return ["not_found:#{ key }"] unless builder
56
+
57
+ keys = [key]
58
+ builder["items"].each do |item|
59
+ item["segments"].each do |segment|
60
+ if segment["type"] == "reference"
61
+ keys << segment["text"]
62
+ keys += visit_and_recurse(segment["text"], depth)
63
+ end
64
+ end
65
+ end
66
+ keys
67
+ end
68
+
46
69
  def fetch_builder(key)
47
70
  builder = @store.find(key) || lookup.call(key)
48
71
  @store.add(key, builder)
@@ -82,15 +105,13 @@ module Text
82
105
  end
83
106
 
84
107
  def random_item(strategy, items)
85
- if strategy.nil? || strategy.empty? || strategy == "random"
86
- items.sample
87
- else
88
- total, count = random_from_dice(strategy)
89
- total -= count # make the roll 0-indexed
90
- raise NoItemMatched("roll #{total} exceeds #{items.length}") if total >= items.length
108
+ return items.sample if strategy.nil? || strategy.empty? || strategy == "random"
91
109
 
92
- items[total]
93
- end
110
+ total, count = random_from_dice(strategy)
111
+ index = total - count # convert to 0-indexed
112
+ raise NoItemMatched("roll #{total} exceeds #{items.length}") if index >= items.length
113
+
114
+ items[index]
94
115
  end
95
116
 
96
117
  def run_random_item(strategy, items, meta, depth)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Text
4
4
  module Gen
5
- VERSION = "0.6.1"
5
+ VERSION = "0.7.0"
6
6
  end
7
7
  end
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.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer
@@ -43,9 +43,13 @@ files:
43
43
  - lib/text/gen.rb
44
44
  - lib/text/gen/filter.rb
45
45
  - lib/text/gen/function/capitalizer.rb
46
+ - lib/text/gen/function/clear.rb
47
+ - lib/text/gen/function/downcase.rb
48
+ - lib/text/gen/function/meta.rb
46
49
  - lib/text/gen/function/pluralizer.rb
47
50
  - lib/text/gen/function/swapper.rb
48
51
  - lib/text/gen/function/titleizer.rb
52
+ - lib/text/gen/function/upcase.rb
49
53
  - lib/text/gen/result.rb
50
54
  - lib/text/gen/result_accumulator.rb
51
55
  - lib/text/gen/runner.rb