-
Notifications
You must be signed in to change notification settings - Fork 0
/
WrongWordGenerator.java
153 lines (137 loc) · 4.4 KB
/
WrongWordGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
public class WrongWordGenerator {
private static final String dictionaryPath = "/usr/share/dict/words";
// Using a map allows us to group the original dictionary by the first
// letter.
private static Map<Character, LinkedHashSet<String>> dictionary = new HashMap<Character, LinkedHashSet<String>>();
private Set<Character> vowels = new HashSet<Character>(5);
private boolean isVowelChanged;
private boolean isPairCreated;
private static WrongWordGenerator instance = null;
public static WrongWordGenerator getInstance() {
if (instance == null) {
instance = new WrongWordGenerator();
}
return instance;
}
public WrongWordGenerator() {
Collections.addAll(vowels, 'a', 'e', 'i', 'o', 'u');
try {
loadDictionary();
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadDictionary() throws IOException {
BufferedReader br = new BufferedReader(new FileReader(dictionaryPath));
String line;
// Use ASCII code to put each letter of the alphabet as a map key.
for (int i = 0; i < 26; i++) {
dictionary.put(((char) (i + 97)), new LinkedHashSet<String>());
}
// Fill the map values without case-sensitivity.
while ((line = br.readLine()) != null) {
dictionary.get(Character.toLowerCase(line.charAt(0))).add(line);
}
br.close();
}
private String duplicateLetter(String word) {
String newWord = word;
if (!isVowelChanged) {
int index = (int) (Math.random() * (word.length()));
if (index == 0) {
newWord = newWord.substring(0, 1)
+ newWord.substring(index, newWord.length());
} else {
newWord = newWord.substring(0, index - 1)
+ newWord.charAt(index - 1)
+ newWord.substring(index - 1, newWord.length());
}
isPairCreated = true;
}
return newWord;
}
private String changeVowel(String word) {
String newWord = word;
Object[] vowelsArray = (vowels.toArray());
int index = (int) (Math.random() * (vowelsArray.length));
if (!isPairCreated) {
for (Character c : newWord.toCharArray()) {
if (vowels.contains(Character.toLowerCase(c))) {
char replacingVowel = (Character) (vowelsArray[index]);
while (replacingVowel == Character.toLowerCase(c)) {
index = (int) (Math.random() * (vowelsArray.length));
replacingVowel = (Character) (vowelsArray[index]);
}
newWord = newWord.replace(Character.toLowerCase(c), replacingVowel);
isVowelChanged = true;
}
break;
}
}
return newWord;
}
private String randomUpper(String word) {
String newWord = word;
int index = (int) (Math.random() * (newWord.length()));
if (index == 0) {
newWord = Character.toUpperCase(newWord.charAt(index))
+ newWord.substring(index, newWord.length());
} else {
newWord = newWord.substring(0, index - 1)
+ Character.toUpperCase(newWord.charAt(index - 1))
+ newWord.substring(index, newWord.length());
}
return newWord;
}
private String corruptWord(String word) {
isVowelChanged = false;
isPairCreated = false;
String corruptedWord = word;
int numberModif = 1 + (int) (Math.random() * ((word.length() - 1) + 1));
for (int i = 0; i < numberModif; i++) {
int typeModif = (int) (Math.random() * (2));
switch (typeModif) {
case 0:
corruptedWord = duplicateLetter(corruptedWord);
break;
case 1:
corruptedWord = changeVowel(corruptedWord);
break;
default:
break;
}
}
//The case modification is separated to avoid creating new letters by
//combining the 3 possible modifications multiple times.
for (int i = 0; i < numberModif; i++) {
corruptedWord = randomUpper(corruptedWord);
}
return corruptedWord;
}
public static void main(String[] args) {
WrongWordGenerator.getInstance();
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
int randomDictIndex = (int) (Math.random() * (26));
char key = (char) (97 + randomDictIndex);
int dictionarySize = dictionary.get(key).size();
int randomDictWord = (int) (Math.random() * (dictionarySize));
Object[] words = dictionary.get(key).toArray();
String word = (String) words[randomDictWord];
System.out.println(WrongWordGenerator.getInstance().corruptWord(word));
}
}
}