Created
May 19, 2012 03:17
-
-
Save alloy-d/2728846 to your computer and use it in GitHub Desktop.
'cause robots should type drunk, too
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array; def random; self[rand(self.count)]; end; end | |
class Hash; def random; self[self.keys.random]; end; end | |
class Alcohol | |
DRINKING_START = 20 | |
PEAK_INTOXICATION = 4 | |
LAYOUT = [ | |
['`1234567890-= ', '~!@#$%^&*()_+ '], | |
[' qwertyuiop[]\\', ' QWERTYUIOP{}|'], | |
[' asdfghjkl;\' ', ' ASDFGHJKL:" '], | |
[' zxcvbnm,./ ', ' ZXCVBNM<>? '], | |
].map{|row| row.map {|keys| keys.chars.to_a } } | |
MANGLING_RULES = { | |
1 => [ | |
:omit, | |
:inject, | |
:substitute, | |
:repeat, | |
], | |
2 => [ | |
:transpose, | |
], | |
} | |
class << self | |
def mistakes | |
@mistakes ||= {}.tap do |m| | |
keys = LAYOUT.each_with_index do |row, r| | |
row.each_with_index do |keys, shift| | |
keys.each_with_index do |key, c| | |
m[key] = {}.tap do |h| | |
h[:up] = LAYOUT[r-1][shift][c] if r > 0 | |
h[:right] = LAYOUT[r][shift][c+1] if c < keys.count - 1 | |
h[:down] = LAYOUT[r+1][shift][c] if r < LAYOUT.count - 1 | |
h[:left] = LAYOUT[r][shift][c-1] if c > 0 | |
end | |
end | |
end | |
end | |
end | |
end | |
def omit(letter); ''; end | |
def inject(letter) | |
side = rand(2) | |
addition = mistakes[letter].random | |
letter.insert(side, addition) | |
end | |
def repeat(letter) | |
letter + letter | |
end | |
def substitute(letter) | |
mistakes[letter].random | |
end | |
def transpose(letters) | |
letters.reverse | |
end | |
def mangle(string) | |
self.send MANGLING_RULES[string.length].random, string | |
end | |
def word_mangle_probability | |
range_of_time_out = (DRINKING_START...(PEAK_INTOXICATION+24)) | |
hours_of_drinking = range_of_time_out.count * 1.0 | |
(range_of_time_out.map{|h| h % 24 }.index(Time.now.hour) || 0) / hours_of_drinking | |
end | |
def mangle_word(word) | |
return word unless rand < word_mangle_probability | |
letters_mangled = MANGLING_RULES.keys.random | |
sliced = word.chars.to_a | |
indices = (0..sliced.length).to_a | |
case letters_mangled | |
when 1 | |
num_mangles = 1 + rand(word.length / 3).to_i | |
num_mangles.times do | |
index = indices.delete_at(rand(indices.count-1).to_i) | |
sliced[index] = mangle(sliced[index]) | |
end | |
when 2 | |
index = rand(word.length-1).to_i | |
sliced[index] = mangle(sliced[index] + sliced[index+1]) | |
sliced.delete_at(index+1) | |
end | |
sliced.join | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment