text-gen 0.4.5 → 0.5.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.rb +4 -0
- data/lib/text/gen/function/pluralizer.rb +15 -3
- data/lib/text/gen/function/swapper.rb +57 -0
- data/lib/text/gen/runner.rb +2 -2
- data/lib/text/gen/version.rb +1 -1
- data/lib/text/gen.rb +1 -0
- 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: 86f9c9dd7fe5548ad60d314ac884c1094ae01a963f4b8857b5ef054ee80e2c21
|
|
4
|
+
data.tar.gz: be6f441194af3a555fa497a33397c27c230fbcaad7ae0607636ce4bba71826e2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3a50a01053a1e25fb2261f9cbbaa5f2c64c19e4542665deb1a164490e9a840a4b83b714dd53fd21a69593fb72820312129e8232978b01b88e6de53cc7aec2a8b
|
|
7
|
+
data.tar.gz: 740dfeda14160a78830087c87d82526131a11d1e30d3038220147ab743bf7db5821d2070ef88d4bd5d5319a57f8f83cb005b7fbc94e7e1c86abfefb9d8dbd15c
|
data/lib/text/gen/filter.rb
CHANGED
|
@@ -28,6 +28,10 @@ module Text
|
|
|
28
28
|
result = Result.from(text: result.text, type: :function, result:)
|
|
29
29
|
result.merge_kv(filter["key"], filter["value"])
|
|
30
30
|
result
|
|
31
|
+
when "swap"
|
|
32
|
+
text = Function::Swapper.swap(result.text, filter["key"], filter["value"])
|
|
33
|
+
result = Result.from(text:, type: :function, result:)
|
|
34
|
+
result
|
|
31
35
|
end
|
|
32
36
|
end
|
|
33
37
|
|
|
@@ -15,13 +15,16 @@ module Text
|
|
|
15
15
|
"elf" => "elves",
|
|
16
16
|
"man" => "men",
|
|
17
17
|
"ox" => "oxen",
|
|
18
|
+
"cactus" => "cacti",
|
|
19
|
+
"thesis" => "theses",
|
|
20
|
+
"criterion" => "criteria",
|
|
18
21
|
"thief" => "thieves",
|
|
19
22
|
"tooth" => "teeth",
|
|
20
23
|
"wolf" => "wolves",
|
|
21
24
|
"woman" => "women"
|
|
22
25
|
}.freeze
|
|
23
26
|
|
|
24
|
-
SINGLE = Set.new(%w[a an the this
|
|
27
|
+
SINGLE = Set.new(%w[a an the this his her its my your our that their])
|
|
25
28
|
|
|
26
29
|
class << self
|
|
27
30
|
def pluralize(str)
|
|
@@ -30,10 +33,13 @@ module Text
|
|
|
30
33
|
arr = str.split(/\s+/)
|
|
31
34
|
return str if arr.length < 2
|
|
32
35
|
|
|
33
|
-
|
|
36
|
+
idx = arr.rindex { |s| to_num(s) }
|
|
37
|
+
return str if idx.nil? || idx >= arr.length - 1
|
|
38
|
+
|
|
39
|
+
num = to_num(arr[idx])
|
|
34
40
|
if num && num > 1
|
|
35
41
|
dc = arr[-1].downcase
|
|
36
|
-
arr[-1] = exceptions(dc) || others(dc) || ends_with_y(dc) || simple(dc)
|
|
42
|
+
arr[-1] = exceptions(dc) || single_letter(dc) || others(dc) || ends_with_y(dc) || simple(dc)
|
|
37
43
|
end
|
|
38
44
|
arr.join(" ")
|
|
39
45
|
end
|
|
@@ -48,6 +54,12 @@ module Text
|
|
|
48
54
|
EXCEPTIONS[str]
|
|
49
55
|
end
|
|
50
56
|
|
|
57
|
+
def single_letter(str)
|
|
58
|
+
if str =~ /^[a-zA-Z0-9]$/
|
|
59
|
+
"#{str}'s"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
51
63
|
def others(str)
|
|
52
64
|
if str.end_with?("s") || str.end_with?("x") || str.end_with?("z") || str.end_with?("ch") || str.end_with?("sh")
|
|
53
65
|
"#{str}es"
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Text
|
|
4
|
+
module Gen
|
|
5
|
+
module Function
|
|
6
|
+
class Swapper
|
|
7
|
+
class << self
|
|
8
|
+
def swap(str, key, val)
|
|
9
|
+
return str if key.nil? || key.empty?
|
|
10
|
+
|
|
11
|
+
puts("str=#{str} key=#{key} val=#{val}")
|
|
12
|
+
|
|
13
|
+
arr = str.split(/\s+/)
|
|
14
|
+
arr = arr.map do |s|
|
|
15
|
+
case key
|
|
16
|
+
when "digit"
|
|
17
|
+
swap_digit(s)
|
|
18
|
+
else
|
|
19
|
+
swap_any(s, key, val.to_s)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
arr.join(" ")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def swap_any(str, key, val)
|
|
26
|
+
str == key ? val : str
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
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
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
data/lib/text/gen/runner.rb
CHANGED
|
@@ -99,11 +99,11 @@ module Text
|
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
def run_weighted_items(items, meta, depth)
|
|
102
|
-
total_weight = items.sum { |item| item.fetch("weight", 1).to_i }
|
|
102
|
+
total_weight = items.sum { |item| [item.fetch("weight", 1).to_i, 1].max }
|
|
103
103
|
rand_weight = rand(total_weight)
|
|
104
104
|
current_weight = 0
|
|
105
105
|
item = items.find do |item|
|
|
106
|
-
current_weight += item.fetch("weight", 1).to_i
|
|
106
|
+
current_weight += [item.fetch("weight", 1).to_i, 1].max
|
|
107
107
|
current_weight > rand_weight
|
|
108
108
|
end
|
|
109
109
|
return unless item
|
data/lib/text/gen/version.rb
CHANGED
data/lib/text/gen.rb
CHANGED
|
@@ -8,6 +8,7 @@ require_relative "gen/max_recursion_error"
|
|
|
8
8
|
|
|
9
9
|
require_relative "gen/function/titleizer"
|
|
10
10
|
require_relative "gen/function/pluralizer"
|
|
11
|
+
require_relative "gen/function/swapper"
|
|
11
12
|
require_relative "gen/result_accumulator"
|
|
12
13
|
require_relative "gen/result"
|
|
13
14
|
require_relative "gen/segment/parser"
|
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.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- G Palmer
|
|
@@ -43,6 +43,7 @@ files:
|
|
|
43
43
|
- lib/text/gen/filter.rb
|
|
44
44
|
- lib/text/gen/filter_error.rb
|
|
45
45
|
- lib/text/gen/function/pluralizer.rb
|
|
46
|
+
- lib/text/gen/function/swapper.rb
|
|
46
47
|
- lib/text/gen/function/titleizer.rb
|
|
47
48
|
- lib/text/gen/lookup_error.rb
|
|
48
49
|
- lib/text/gen/max_attempts_error.rb
|