text-gen 0.13.0 → 0.13.2

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: 2afbd1cd20f4be4fa1c6ebb125fda4894f46026a078b8b54f808ed1e9fb684e6
4
- data.tar.gz: '043274280f1ff52837206196ee489932b64c85f1a2af037b9b29cf1127ae1236'
3
+ metadata.gz: 5581ccb42efa3604510e17f8af69e6d5b804cc60702f6e1f7001cebf8def8c54
4
+ data.tar.gz: 840ffc9473992cb64850991e4ee442c774acee38ddbdf40e81257be9c69b756d
5
5
  SHA512:
6
- metadata.gz: cce97e373664b52db5752f18d0a672041246486fdd4848aeb74febf96f099543ac04ac97c5609ea71877ac867afa9a805a51862d5099ddad3dc74be81f9579ea
7
- data.tar.gz: 5762c3a92aca626f66e5127ab9aef91bc7a14bce1111e44836f7b083b7cc35ee387b4f8f5f0ea4351d38fad789a3bdde015dfce7f81257f78740f5627870f8c6
6
+ metadata.gz: 511f34f2a2a29d8c50a85d8b63f504b420047db4e219783525ab855ccd41582b6e9673b3f02bcbb5c2ea1ed9fdf515160a5a6e3b18b251686f55cf0ee3cb10fd
7
+ data.tar.gz: 1b74d7812ff49c5026afd623630435921b7f366c754388439e628a00e0766919458cebf6a22f93bd914f307d8016138b1bcab15104cb01bac3cc132c454d5eb5
@@ -12,7 +12,7 @@ module Text
12
12
  end
13
13
 
14
14
  def component_key
15
- "fn(#{to_s})"
15
+ "fn(#{self})"
16
16
  end
17
17
 
18
18
  # Shared utilities
@@ -33,8 +33,9 @@ module Text
33
33
  end
34
34
 
35
35
  def to_s
36
- return "#{ type }:#{ key }:#{ value }" if value
37
- return "#{ type }:#{ key }" if key
36
+ return "#{type}:#{key}:#{value}" if value
37
+ return "#{type}:#{key}" if key
38
+
38
39
  type
39
40
  end
40
41
 
@@ -52,9 +52,13 @@ module Text
52
52
 
53
53
  def pluralize_by_key(str, multiplier, result)
54
54
  tokens = tokenize(str)
55
- word_tokens = tokens.select { |t| t.match?(/\w/) }
55
+ word_tokens = tokens.grep(/\w/)
56
56
  idx = word_tokens.rindex { |s| to_num(s) }
57
- num = multiplier > 1 ? multiplier : (idx ? to_num(word_tokens[idx]) : nil)
57
+ num = if multiplier > 1
58
+ multiplier
59
+ else
60
+ (idx ? to_num(word_tokens[idx]) : nil)
61
+ end
58
62
  return str if num.nil? || num <= 1
59
63
 
60
64
  component = find_component(result, key)
@@ -68,7 +72,8 @@ module Text
68
72
  return str unless last_word_idx
69
73
 
70
74
  dc = component_tokens[last_word_idx].downcase
71
- component_tokens[last_word_idx] = exceptions(dc) || single_letter(dc) || others(dc) || ends_with_y(dc) || simple(dc)
75
+ component_tokens[last_word_idx] =
76
+ exceptions(dc) || single_letter(dc) || others(dc) || ends_with_y(dc) || simple(dc)
72
77
  str.sub(component_text, component_tokens.join)
73
78
  end
74
79
 
@@ -84,9 +89,13 @@ module Text
84
89
 
85
90
  def substitute(str, multiplier = 1)
86
91
  tokens = tokenize(str)
87
- word_tokens = tokens.select { |t| t.match?(/\w/) }
92
+ word_tokens = tokens.grep(/\w/)
88
93
  idx = word_tokens.rindex { |s| to_num(s) }
89
- num = multiplier > 1 ? multiplier : (idx ? to_num(word_tokens[idx]) : nil)
94
+ num = if multiplier > 1
95
+ multiplier
96
+ else
97
+ (idx ? to_num(word_tokens[idx]) : nil)
98
+ end
90
99
  return str if num.nil? || num <= 1
91
100
 
92
101
  tokens.map! { |t| t.match?(/\A#{Regexp.escape(key)}\z/i) ? value : t }
@@ -97,13 +106,17 @@ module Text
97
106
  return str if str.empty?
98
107
 
99
108
  tokens = tokenize(str)
100
- word_tokens = tokens.select { |t| t.match?(/\w/) }
109
+ word_tokens = tokens.grep(/\w/)
101
110
  return str if word_tokens.length < 2
102
111
 
103
112
  idx = word_tokens.rindex { |s| to_num(s) }
104
113
 
105
114
  # Use the multiplier if available, otherwise parse from text
106
- num = multiplier > 1 ? multiplier : (idx ? to_num(word_tokens[idx]) : nil)
115
+ num = if multiplier > 1
116
+ multiplier
117
+ else
118
+ (idx ? to_num(word_tokens[idx]) : nil)
119
+ end
107
120
 
108
121
  return str if num.nil? || num <= 1
109
122
  return str if idx && idx >= word_tokens.length - 1
@@ -4,7 +4,7 @@ module Text
4
4
  module Gen
5
5
  module Filter
6
6
  class Replace < Base
7
- def builder(context, builder)
7
+ def builder(_context, builder)
8
8
  return builder unless builder["key"] == key
9
9
 
10
10
  Result.new(text: value, type: "replace")
@@ -14,7 +14,7 @@ module Text
14
14
  def build(hsh_or_key, depth)
15
15
  hsh = hsh_or_key.is_a?(Hash) ? hsh_or_key : key_to_hsh(hsh_or_key)
16
16
  filter_class = available_filters.find { |f| f.filter_key == hsh["type"] }
17
- filter_class.new(hsh, depth) if filter_class
17
+ filter_class&.new(hsh, depth)
18
18
  end
19
19
 
20
20
  def key_to_hsh(key)
data/lib/text/gen/meta.rb CHANGED
@@ -33,7 +33,7 @@ module Text
33
33
  else
34
34
  hsh.each_key do |k|
35
35
  if hsh.key?(k)
36
- new_hsh[k] = k == key ? hsh[k].select { |v| v != val } : hsh[k].dup
36
+ new_hsh[k] = k == key ? hsh[k].reject { |v| v == val } : hsh[k].dup
37
37
  end
38
38
  end
39
39
  end
@@ -104,7 +104,6 @@ module Text
104
104
  end
105
105
  return if rand_weight > total_weight
106
106
 
107
-
108
107
  current_weight = 0
109
108
  item = items.find do |item|
110
109
  current_weight += [item.fetch("weight", 1).to_i, 1].max
@@ -143,10 +142,10 @@ module Text
143
142
  return if results.any?(&:nil?) || results.empty?
144
143
 
145
144
  result = Result.merge(results,
146
- value: item["value"],
147
- multiplier: item["multiplier"],
148
- meta: item["meta"],
149
- type: context.current_key)
145
+ value: item["value"],
146
+ multiplier: item["multiplier"],
147
+ meta: item["meta"],
148
+ type: context.current_key)
150
149
  context.apply_result_filters(result)
151
150
  end
152
151
  end
@@ -210,7 +209,7 @@ module Text
210
209
  weight = lookup_and_count_items(context, key)
211
210
  end
212
211
 
213
- weight.to_i < 1 ? 1 : weight.to_i
212
+ [weight.to_i, 1].max
214
213
  end
215
214
 
216
215
  def lookup_and_count_items(context, key)
@@ -4,7 +4,7 @@ module Text
4
4
  module Gen
5
5
  module Segment
6
6
  class Reference
7
- REFERENCE_MATCHER = /\[\s*([a-z0-9-]+(?::[a-z0-9-]+)*)\s*\]/
7
+ REFERENCE_MATCHER = /\[\s*([a-z0-9-]+(?:(?:>>|>|:)[a-z0-9-]+)*)\s*\]/
8
8
  class << self
9
9
  def scan(scanner)
10
10
  str = scanner.scan(REFERENCE_MATCHER)
@@ -37,7 +37,7 @@ module Text
37
37
  keys = key.split("+")
38
38
  builders = keys.map do |key|
39
39
  builder = @lookup.call(key)
40
- builder = not_found(key) unless builder
40
+ builder ||= not_found(key)
41
41
  builder
42
42
  end
43
43
 
@@ -47,14 +47,14 @@ module Text
47
47
 
48
48
  def merge_builders(builders)
49
49
  all_filters = builders.map { |b| b["filters"] }.flatten
50
- all_meta = builders.inject({}) { |b, acc| acc = Text::Gen::Meta.merge_meta(acc, b["meta"]) }
50
+ all_meta = builders.inject({}) { |b, acc| Text::Gen::Meta.merge_meta(acc, b["meta"]) }
51
51
  all_items = builders.map { |b| b["items"] }.flatten
52
- strategy = builders.all? {|b| b["strategy"] == "weighted" } ? "weighted" : "sample"
52
+ strategy = builders.all? { |b| b["strategy"] == "weighted" } ? "weighted" : "sample"
53
53
  {
54
54
  "strategy" => strategy,
55
55
  "filters" => all_filters,
56
56
  "meta" => all_meta,
57
- "items" => all_items,
57
+ "items" => all_items
58
58
  }
59
59
  end
60
60
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Text
4
4
  module Gen
5
- VERSION = "0.13.0"
5
+ VERSION = "0.13.2"
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.13.0
4
+ version: 0.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - G Palmer