module Blundersaur::Extensions::String

Constants

MISTAKE_METHODS
PHONETIC_GROUPS

Public Instance Methods

fat_finger() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 34
def fat_finger
  dup.fat_finger!
end
fat_finger!() click to toggle source
# 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_letter() click to toggle source
# 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
mistype() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 58
def mistype
  dup.mistype!
end
mistype!() click to toggle source
# 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) click to toggle source
# File lib/blundersaur/extensions/string.rb, line 139
def phonetic_swap(passes = 1)
  dup.phonetic_swap!(passes)
end
phonetic_swap!(passes = 1) click to toggle source

This method is kind of expensive :(

# 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_mistake() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 66
def random_mistake
  dup.random_mistake!
end
random_mistake!() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 62
def random_mistake!
  send(MISTAKE_METHODS[rand(MISTAKE_METHODS.length)])
end
stutter() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 44
def stutter
  dup.stutter!
end
stutter!() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 38
def stutter!
  i = rand(length)
  self[i] = self[i] * 2
  self
end
transpose() click to toggle source
# File lib/blundersaur/extensions/string.rb, line 152
def transpose
  dup.transpose!
end
transpose!() click to toggle source
# 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