Created
October 20, 2015 04:32
-
-
Save apsun/0c8277c39640ab32fc54 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import java.util.HashMap; | |
| import java.util.Map; | |
| public class Georgify { | |
| private static final Map<Character, String> NEIGHBOR_KEYS; | |
| static { | |
| NEIGHBOR_KEYS = new HashMap<>(26); | |
| // Top row | |
| NEIGHBOR_KEYS.put('q', "wa"); | |
| NEIGHBOR_KEYS.put('w', "qase"); | |
| NEIGHBOR_KEYS.put('e', "wsdr"); | |
| NEIGHBOR_KEYS.put('r', "edft"); | |
| NEIGHBOR_KEYS.put('t', "rfgy"); | |
| NEIGHBOR_KEYS.put('y', "tghu"); | |
| NEIGHBOR_KEYS.put('u', "yhji"); | |
| NEIGHBOR_KEYS.put('i', "ujko"); | |
| NEIGHBOR_KEYS.put('o', "iklp"); | |
| NEIGHBOR_KEYS.put('p', "ol"); | |
| // Middle row | |
| NEIGHBOR_KEYS.put('a', "qwsz"); | |
| NEIGHBOR_KEYS.put('s', "wazxde"); | |
| NEIGHBOR_KEYS.put('d', "esxcfr"); | |
| NEIGHBOR_KEYS.put('f', "rdcvgt"); | |
| NEIGHBOR_KEYS.put('g', "tfvbhy"); | |
| NEIGHBOR_KEYS.put('h', "ygbnju"); | |
| NEIGHBOR_KEYS.put('j', "uhnmki"); | |
| NEIGHBOR_KEYS.put('k', "ijmlo"); | |
| NEIGHBOR_KEYS.put('l', "okp"); | |
| // Bottom row | |
| NEIGHBOR_KEYS.put('z', "asx"); | |
| NEIGHBOR_KEYS.put('x', "zsdc"); | |
| NEIGHBOR_KEYS.put('c', "xdfv"); | |
| NEIGHBOR_KEYS.put('v', "cfgb"); | |
| NEIGHBOR_KEYS.put('b', "vghn"); | |
| NEIGHBOR_KEYS.put('n', "bhjm"); | |
| NEIGHBOR_KEYS.put('m', "njk"); | |
| } | |
| private static int randomInt(int upper) { | |
| return (int)(Math.random() * upper); | |
| } | |
| private static char neighborLetter(char input) { | |
| String replacementKeys = NEIGHBOR_KEYS.get(input); | |
| int index = randomInt(replacementKeys.length()); | |
| return replacementKeys.charAt(index); | |
| } | |
| private static String misspellDelete(String word) { | |
| int index = randomInt(word.length()); | |
| return word.substring(0, index) + word.substring(index + 1); | |
| } | |
| private static String misspellInsert(String word) { | |
| int index = randomInt(word.length()); | |
| char newChar = neighborLetter(word.charAt(index)); | |
| if (Math.random() < 0.5) index++; | |
| return word.substring(0, index) + newChar + word.substring(index); | |
| } | |
| private static String misspellSwap(String word) { | |
| int index = randomInt(word.length() - 1); | |
| char c1 = word.charAt(index); | |
| char c2 = word.charAt(index + 1); | |
| return word.substring(0, index) + c2 + c1 + word.substring(index + 2); | |
| } | |
| private static String misspellDuplicate(String word) { | |
| int index = randomInt(word.length()); | |
| char c = word.charAt(index); | |
| return word.substring(0, index) + c + c + word.substring(index + 1); | |
| } | |
| private static String misspellReplace(String word) { | |
| int index = randomInt(word.length()); | |
| char newChar = neighborLetter(word.charAt(index)); | |
| return word.substring(0, index) + newChar + word.substring(index + 1); | |
| } | |
| public static String georgify(String word) { | |
| if (word.length() <= 1) { | |
| return word; | |
| } | |
| if (Math.random() < 0.7) { | |
| // Spelled correctly 70% of the time | |
| return word; | |
| } | |
| double rnd = Math.random(); | |
| if (rnd < 0.1) { | |
| // 10% | |
| return misspellDelete(word); | |
| } else if (rnd < 0.2) { | |
| // 10% | |
| return misspellInsert(word); | |
| } else if (rnd < 0.3) { | |
| // 10% | |
| return misspellSwap(word); | |
| } else if (rnd < 0.65) { | |
| // 35% | |
| return misspellDuplicate(word); | |
| } else { | |
| // 35% | |
| return misspellReplace(word); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment