-
Notifications
You must be signed in to change notification settings - Fork 5
/
Gibgen.java
247 lines (228 loc) · 8.23 KB
/
Gibgen.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import java.io.*;
import java.util.Random;
import java.util.Scanner;
/**
* @author: Tyler Bailey
* @date: 4/25/13
* @name: Gibgen
* @description: A console program that reads a string StndIn or as
* an arg and returns gibberish
* @ver: 1.0
**/
public class Gibgen{
/**
* @description: The main method to start the program
* @requires: input on stdin or a string as an argument
* @ensures: the program starts
**/
public static void main(String[] argv){
Gibgen gibgen = new Gibgen();
try{
if((System.in.available() > 0) && (argv.length > 0))
System.out.println("Please give input either over stdin or as an " +
"argument, not both");
else if(System.in.available() > 0)
gibgen.readFromFile();
else if(argv.length > 0)
gibgen.readFromArg(argv[0]);
else{
System.out.println("You must provide a file over stndin or a string" +
" as an argument");
System.out.println("Example: 'cat myfile.txt | java Gibgen', 'java " +
"Gibgen \"This is a sentence\"'");
}
}
catch(Exception e){
System.out.println("OH GOD YOU CAUSED SOME SORT OF ERROR OVER STNDIN, " +
"ABANDON SHIP");
}
}
/**
* @description: A constructor for this
* @requires: none
* @ensures: a new instance of this is created
**/
public Gibgen(){
}
/**
* @description: Method to start the gibberish generation from stdin
* @requires: input on stdin
* @ensures: returns the given input as gibberish in the console
**/
public void readFromFile(){
StringBuilder input = new StringBuilder();
try{
Scanner scanner = new Scanner(System.in);
String token;
while(scanner.hasNext()){
token = scanner.next();
//Only perform the swap when the word is larger than 3 characters
//and is not an uppercase abbreviation
if(lengthWithoutPunctuation(token) > 3 && !isAbbreviation(token)){
token = swapLetters(token);
}
input.append(" " + token + " ");
}
System.out.println(input.toString());
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
/**
* @description: Method to start the gibberish generation from arg string
* @requires: input as a arg
* @ensures: returns the given input as gibberish in the console
**/
public void readFromArg(String arg){
StringBuilder input = new StringBuilder();
try{
Scanner scanner = new Scanner(arg);
String token;
while(scanner.hasNext()){
token = scanner.next();
//Only perform the swap when the word is larger than 3 characters
//and is not an uppercase abbreviation
if(lengthWithoutPunctuation(token) > 3 && !isAbbreviation(token)){
token = swapLetters(token);
}
input.append(" " + token + " ");
}
System.out.println(input.toString());
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
/**
* @description: The length of a given token without any leading
* or trailing punctuation
* @requires:
* @ensures:
**/
public int lengthWithoutPunctuation(String word){
String wordWithoutPunctuation = removeNonletters(word);
return wordWithoutPunctuation.length();
}
/**
* @description: Checks to see if a string is an uppercase abbreviation
* by checking for at least 2 connsecutive uppercase letters.
* This way abbreviations with prefixes or suffixes can be
* detected such as UNO's or pre-GE.
* @requires: String is atleast atleast one character in length
* @ensures: Returns true if all characters are uppercase, else
* returns false
**/
public boolean isAbbreviation(String word){
int count = 0;
String wordWithoutPunctuation = removeNonletters(word);
for(int i = 0; i < wordWithoutPunctuation.length() && count < 2; i++){
if(Character.isUpperCase(wordWithoutPunctuation.charAt(i))){
count++;
}
else{
count = 0;
}
}
return count >= 2;
}
/**
* @description: Determines if the given character is a letter or not
* @requires: One character
* @ensures: Returns true if the character is an alphabet letter,
* else returns false
**/
public boolean isLetter(char character){
boolean isLetter = false;
if((character >= 'a' && character <= 'z') ||
(character >= 'A' && character <= 'Z')){
isLetter = true;
}
return isLetter;
}
/**
* @description: Swaps the letters of a given word ignoring
* punctuation and numbers
* @requires: A word greater than 3 characters excluding
* punctuation and numbers
* @ensures: returns a word with the possibility of any
* letter except the first and
* last being swapped in the word.
**/
public String swapLetters(String word){
String head, tail;
StringBuilder body;
char currentCharacter, swappedCharacter;
int swapIndex;
head = word.substring(0, findFirstLetter(word)+1);
tail = word.substring(findLastLetter(word), word.length());
body = new StringBuilder(word.substring(findFirstLetter(word)+1,
findLastLetter(word)));
for(int i = 0; i < body.length(); i++){
currentCharacter = body.charAt(i);
if(isLetter(currentCharacter)){
swapIndex = findSwapIndex(body, i);
swappedCharacter = body.charAt(swapIndex);
body.setCharAt(swapIndex, currentCharacter);
body.setCharAt(i, swappedCharacter);
}
}
return head + body.toString() + tail;
}
/**
* @description: Finds a suitable index to swap a letter at
* @requires: body be a word with atleast 2 characters and
* current index be a positive integer between 0 and
* body.length()-1
* @ensures: Returns an integer that is an index different from the
* currentIndex and between 0 and body.length()-1
**/
private int findSwapIndex(StringBuilder body, int currentIndex){
Random random = new Random();
int validSwap = random.nextInt(body.length()-1);
while(isLetter(body.charAt(validSwap)) == false){
validSwap = random.nextInt(body.length()-1);
}
return validSwap;
}
/**
* @description: Finds the first occurence of a letter character
* @requires: given word must contain at least one letter character
* @ensures: returns the index of the first letter occurence
**/
public int findFirstLetter(String word){
int firstLetter = 0;
for(int i = 0; i < word.length(); i++){
if(isLetter(word.charAt(i))){
firstLetter = i;
i = word.length();
}
}
return firstLetter;
}
/**
* @description: Finds the last occurence of a letter character
* in a given word and returns its index
* @requires: given word must contain at least one letter character
* @ensures: returns the index of the last letter character
**/
public int findLastLetter(String word){
int lastLetter = word.length()-1;
for(int i = word.length()-1; i > -1; i--){
if(isLetter(word.charAt(i))){
lastLetter = i;
i = -1;
}
}
return lastLetter;
}
/**
* @description: Strips the leading and trailing nonletter characters
* @requires: word != null
* @ensures: returns a word without leading and trailing
* nonletter characters
**/
public String removeNonletters(String word){
return word.substring(findFirstLetter(word),findLastLetter(word)+1);
}
}