Module: RiveScript::Utils
- Defined in:
- lib/rivescript/utils.rb
Class Method Summary collapse
- .clone(obj) ⇒ Object
- .extend(a, b) ⇒ Object
- .is_atomic(trigger) ⇒ Object
- .n_index_of(string, match, index) ⇒ Object
- .parse_call_args(str) ⇒ Object
- .quotemeta(string) ⇒ Object
- .string_format(type, string) ⇒ Object
- .strip(text) ⇒ Object
- .strip_nasties(string, utf8) ⇒ Object
- .trim(text) ⇒ Object
- .word_count(trigger, all = false) ⇒ Object
Class Method Details
.clone(obj) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rivescript/utils.rb', line 99 def clone(obj) case obj when nil nil when Array obj.map { |item| clone(item) } when Hash obj.transform_values { |v| clone(v) } else obj end end |
.extend(a, b) ⇒ Object
17 18 19 20 21 |
# File 'lib/rivescript/utils.rb', line 17 def extend(a, b) b.each do |attr, value| a[attr] = value end end |
.is_atomic(trigger) ⇒ Object
48 49 50 51 |
# File 'lib/rivescript/utils.rb', line 48 def is_atomic(trigger) specials = ["*", "#", "_", "(", "[", "<", "@"] specials.none? { |special| trigger.include?(special) } end |
.n_index_of(string, match, index) ⇒ Object
112 113 114 |
# File 'lib/rivescript/utils.rb', line 112 def n_index_of(string, match, index) string.split(match, index).join(match).length end |
.parse_call_args(str) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rivescript/utils.rb', line 71 def parse_call_args(str) result = [] buff = "" inside_a_string = false str.each_char do |c| if c.match?(/\s/) && !inside_a_string unless buff.empty? result.push(buff) buff = "" end elsif c == '"' unless inside_a_string # opening quote - don't add to buff else result.push(buff) unless buff.empty? buff = "" end inside_a_string = !inside_a_string else buff += c end end result.push(buff) unless buff.empty? result end |
.quotemeta(string) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/rivescript/utils.rb', line 40 def (string) unsafe = "\\.+*?[^]$(){}=!<>|:".chars unsafe.each do |char| string = string.gsub(char, "\\#{char}") end string end |
.string_format(type, string) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rivescript/utils.rb', line 53 def string_format(type, string) case type when "uppercase" string.upcase when "lowercase" string.downcase when "sentence" string = string.to_s string[0].upcase + string[1..] when "formal" string.split(/\s+/).map do |word| word[0].upcase + word[1..] end.join(" ") else string end end |
.strip(text) ⇒ Object
9 10 11 |
# File 'lib/rivescript/utils.rb', line 9 def strip(text) text.gsub(/^[\s\t]+/, "").gsub(/[\s\t]+$/, "").gsub(/[\x0D\x0A]+/, "") end |
.strip_nasties(string, utf8) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/rivescript/utils.rb', line 32 def strip_nasties(string, utf8) if utf8 string.gsub(/[\\<>]+/, "") else string.gsub(/[^A-Za-z0-9 ]/, "") end end |
.trim(text) ⇒ Object
13 14 15 |
# File 'lib/rivescript/utils.rb', line 13 def trim(text) text.gsub(/^[\x0D\x0A\s\t]+/, "").gsub(/[\x0D\x0A\s\t]+$/, "") end |
.word_count(trigger, all = false) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/rivescript/utils.rb', line 23 def word_count(trigger, all = false) words = if all trigger.split(/\s+/) else trigger.split(/[\s\*#_\|]+/) end words.count { |word| !word.empty? } end |