-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI2.java
251 lines (213 loc) · 8.53 KB
/
AI2.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
248
249
250
251
/*
* This AI program deconstructs a user input, builds statistics, and generates
* a new sentence on its own.
*/
package ai2;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import toolbox.*;
/**
* @author Debrah Wyatt
*/
public class AI2 {
//Variables created by UserInput.
public static String userInput;
private static int avgWordSize;
private static int maxWordSize;
//This array stores the frequency of characters used.
private static final int[] letterPercent = new int[93];
public static void main(String[] args) throws IOException {
System.out.println(AiString());
}
//This method deconstructs the user's String.
public static void UserInput() {
int currentWord = 0;
int letterCount = 0;
int wordCount = 0;
// User input String.
userInput = "The best day I ever had at school was the day I met you.";
//Converts userInput to lower case for ease of use.
userInput = userInput.toLowerCase();
try {
//Initiallizes letterPercent by adding 1 to each element.
for (int i = 0; i < 93; i++) {
AI2.letterPercent[i]++;
}
//Checks every character for variety.
for (int i = 0; i < userInput.length(); i++) {
char currentChar = userInput.charAt(i);
// Counts characters from a to z.
if (currentChar >= 33 && currentChar <= 126) {
letterCount++;
currentWord++;
AI2.letterPercent[userInput.charAt(i) - 33]++;
}
// Counts spaces for words and finds the max word size.
if (currentChar == ' ' || (i + 1) == userInput.length()) {
wordCount++;
if (currentWord > maxWordSize) {
maxWordSize = currentWord;
}
currentWord = 0;
}
}
//Calculates the average word size.
avgWordSize = letterCount / wordCount;
//Prints out the useful information
// System.out.println("userInput = " + userInput);
// System.out.println("wordCount = " + wordCount);
// System.out.println("AvgWordSize = " + avgWordSize);
// System.out.println("maxWordSize = " + maxWordSize);
// System.out.println("AiWordLength: " + AiWordLength());
// System.out.println("AiCharStats: " + AiCharStats());
} catch (Exception e) {
System.out.println(e);
}
}
//This method will return the Ai's word length
public static int AiWordLength() {
int topRank;
int totalRank = 0;
topRank = maxWordSize + 1;
//Min to max word range
int[] rank = new int[maxWordSize];
int[] rankPercent = new int[maxWordSize];
//Associates the word size with its rank and finds the sum of all ranks.
for (int i = 0; i < rank.length; i++) {
rank[i] = Math.abs(Math.abs(i - avgWordSize) - topRank);
totalRank += rank[i];
}
//Converts rank into % likelyhood
for (int i = 0; i < rankPercent.length; i++) {
rankPercent[i] = rank[i] * 1000 / totalRank;
}
//Stacks the percentages to add up to 1000%
int[] percentStack = new int[maxWordSize];
percentStack[0] = rankPercent[0];
for (int i = 1; i < percentStack.length; i++) {
percentStack[i] = percentStack[i - 1] + rankPercent[i];
}
//Randomizer to pick the word size based on probability
Random random = new Random();
int pickWordSize = random.nextInt(1000);
for (int i = 0; i < maxWordSize; i++) {
if (pickWordSize <= percentStack[i]) {
return (i + 1);
}
}
return avgWordSize;
}
//Returns a character based on probability
public static char AiCharStats() {
int rank = 0;
Random random = new Random();
//rank = the sum of every element in the count array.
for (int i = 0; i < letterPercent.length; i++) {
rank = rank + letterPercent[i];
}
//Array creates a percent rank for each letter
int[] rankPercent = new int[93];
for (int i = 0; i < letterPercent.length; i++) {
rankPercent[i] = letterPercent[i] * 10000 / rank;
}
//Stacks the percentages to add up to 10000%
int[] percentStack = new int[93];
percentStack[0] = rankPercent[0];
for (int i = 1; i < letterPercent.length; i++) {
percentStack[i] = percentStack[i - 1] + rankPercent[i];
}
//Randomizer to pick the letter based on probability
int pickChar = random.nextInt(10000);
for (int i = 0; i < letterPercent.length; i++) {
if (pickChar <= percentStack[i]) {
char probableChar = (char) (i + 33); //Converts int to char
return (probableChar);
}
}
return (char) (random.nextInt(93) + 33);
}
//This method creates a string of words.
public static String AiString() throws IOException {
UserInput();
String aiString = "";
int aiStringLength = 0;
//Turns a word list into an array.
File file = new File("C:\\Users\\devli\\OneDrive\\Documents\\NetBeansProjects\\AI2\\src\\ai2\\3000-Common-Words.txt");
Initialize wL = new Initialize();
String[] wordList = wL.Initialization(file);
//Finds the longest word in the wordlist.
int longWord = 0;
for (int i = 0; i < wordList.length; i++) {
if (wordList[i].length() > longWord) {
longWord = wordList[i].length();
}
}
//Reduces maxWordSize to the longest word found.
if (maxWordSize > longWord) {
maxWordSize = longWord;
}
//Creates an array of compatible word lengths.
boolean[] compatibleWordLengths = new boolean[maxWordSize];
for (int i = 0; i < wordList.length; i++) {
for (int j = 0; j < compatibleWordLengths.length; j++) {
if (wordList[i].length() == j + 1) {
compatibleWordLengths[j] = true;
}
}
}
//Creates the ai string
while (aiStringLength < userInput.length()) {
String aiWord;
//Omits word lengths that are incompatible.
int thisWordLength = 0;
boolean lengthOK = false;
do {
thisWordLength = AiWordLength();
for (int i = 0; i < compatibleWordLengths.length; i++) {
if (i + 1 == thisWordLength && compatibleWordLengths[i] == true) {
lengthOK = true;
}
}
} while (lengthOK == false);
//Creates the next word.
char[] currentWord = new char[thisWordLength];
for (int j = 0; j < currentWord.length; j++) {
currentWord[j] = AiCharStats();
boolean compatible = false;
for (int i = 0; i < wordList.length; i++) {
if (currentWord.length == wordList[i].length()) {
for (int k = 0; k <= j; k++) {
if (wordList[i].charAt(k) != currentWord[k]) {
compatible = false;
break;
} else if (wordList[i].charAt(k) == currentWord[k]) {
compatible = true;
}
}
}
if (compatible == true) {
break;
}
if (i == wordList.length - 1) {
j--;
break;
}
}
}
//Converts currentWord to a String
aiWord = new String(currentWord);
//Capitalizes 'i'
if (aiWord.equalsIgnoreCase("i")) {
aiWord = aiWord.toUpperCase();
}
//Combines the Strings
aiString = aiString + aiWord + " ";
//Capitalizes the first letter of the String
aiString = aiString.substring(0, 1).toUpperCase() + aiString.substring(1);
//Tracks the string length
aiStringLength = aiStringLength + thisWordLength;
}
return aiString;
}
}