Module: Blundersaur::Extensions::String

Defined in:
lib/blundersaur/extensions/string.rb

Constant Summary collapse

MISTAKE_METHODS =
[:fat_finger!, :stutter!, :mistype!, :phonetic_swap!, :transpose!]
PHONETIC_GROUPS =
[
  # whole words
  ["your", "you're"],
  ["there", "their", "they're"],
  nil,

  # syllables
  ["ph", "f"]
]

Instance Method Summary collapse

Instance Method Details

#fat_fingerObject



34
35
36
# File 'lib/blundersaur/extensions/string.rb', line 34

def fat_finger
  dup.fat_finger!
end

#fat_finger!Object



24
25
26
27
28
29
30
31
32
# File 'lib/blundersaur/extensions/string.rb', line 24

def fat_finger!
  index = index_of_random_letter
  if index
    chars = Keyboards::Qwerty.keys_near(self[index..index])
    char = chars[rand(chars.length)]
    self[index..index] = rand(2) == 1 ? self[index..index] + char : char + self[index..index]
  end
  self
end

#index_of_random_letterObject



15
16
17
18
19
20
21
22
# File 'lib/blundersaur/extensions/string.rb', line 15

def index_of_random_letter
  indices = (0...length).sort { |a, b| rand(3) - 1 }
  index = indices.shift
  while index && self[index..index] =~ /\W/
    index = indices.shift
  end
  index
end

#mistypeObject



58
59
60
# File 'lib/blundersaur/extensions/string.rb', line 58

def mistype
  dup.mistype!
end

#mistype!Object



48
49
50
51
52
53
54
55
56
# File 'lib/blundersaur/extensions/string.rb', line 48

def mistype!
  index = index_of_random_letter
  if index
    chars = Keyboards::Qwerty.keys_near(self[index..index])
    char = chars[rand(chars.length)]
    self[index..index] = char
  end
  self
end

#phonetic_swap(passes = 1) ⇒ Object



139
140
141
# File 'lib/blundersaur/extensions/string.rb', line 139

def phonetic_swap(passes = 1)
  dup.phonetic_swap!(passes)
end

#phonetic_swap!(passes = 1) ⇒ Object

This method is kind of expensive :(



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/blundersaur/extensions/string.rb', line 71

def phonetic_swap!(passes = 1)
  # Find all of the matches in the string
  # NOTE: if passes == 1, it would probably be better to sort the groups
  #       randomly and stop at the first one we find that has a match
  matches = {}
  whole_words = true
  PHONETIC_GROUPS.each_with_index do |group, index|
    if group.nil?
      whole_words = false
      next
    end
    re_str = "(?:#{group.join('|')})"
    re = Regexp.new(whole_words ? '\b'+re_str+'\b' : re_str, 'i')

    matches[index] = []
    scan(re) do |str|
      matches[index] << [$~.begin(0), str]
    end
    matches.delete(index) if matches[index].empty?
  end

  if !matches.empty?
    adjustments = []
    passes.times do |i|
      # Pick a random group's matches
      group_index = matches.keys[rand(matches.keys.length)]
      group_matches = matches[group_index]
      group = PHONETIC_GROUPS[group_index]

      # Pick a random match to replace
      offset, string = group_matches.delete_at(rand(group_matches.length))
      taken_index = group.index(string.downcase)
      matches.delete(group_index) if group_matches.empty?

      # Adjust offset if necessary
      new_offset = adjustments.inject(offset) do |n, (a_offset, a_diff)|
        offset > a_offset ? n + a_diff : n
      end
      offset = new_offset

      # Pick a random string from the group to replace it with
      elt_indices = (0...group.length).sort { |a, b| rand(3) - 1 }
      elt_index = elt_indices.shift
      elt_index = elt_indices.shift if elt_index == taken_index
      replacement = group[elt_index].dup

      # Add adjustment for further iterations
      diff = replacement.length - string.length
      if diff != 0
        adjustments << [offset, diff]
      end

      # Match case of string in replacement
      len = diff < 0 ? replacement.length : string.length
      len.times do |i|
        ord = string[i].ord
        replacement[i] = replacement[i].upcase if ord >= 65 && ord <= 90
      end

      # Booyah!
      self[offset, string.length] = replacement

      break if matches.empty?
    end
  end
  self
end

#random_mistakeObject



66
67
68
# File 'lib/blundersaur/extensions/string.rb', line 66

def random_mistake
  dup.random_mistake!
end

#random_mistake!Object



62
63
64
# File 'lib/blundersaur/extensions/string.rb', line 62

def random_mistake!
  send(MISTAKE_METHODS[rand(MISTAKE_METHODS.length)])
end

#stutterObject



44
45
46
# File 'lib/blundersaur/extensions/string.rb', line 44

def stutter
  dup.stutter!
end

#stutter!Object



38
39
40
41
42
# File 'lib/blundersaur/extensions/string.rb', line 38

def stutter!
  i = rand(length)
  self[i] = self[i] * 2
  self
end

#transposeObject



152
153
154
# File 'lib/blundersaur/extensions/string.rb', line 152

def transpose
  dup.transpose!
end

#transpose!Object



143
144
145
146
147
148
149
150
# File 'lib/blundersaur/extensions/string.rb', line 143

def transpose!
  if length > 1
    i = rand(length-1)
    r = i..(i+1)
    self[r] = self[r].reverse
  end
  self
end