-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordPuzzle.java
313 lines (248 loc) · 7.42 KB
/
WordPuzzle.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package assignment;
import java.io.*;
import java.util.*;
public class WordPuzzle{
private List<String> potentialWords = new ArrayList<>();
private HashMap<String, Integer> actualWords = new HashMap<>();
private List<String[]> puzzleBoard = new ArrayList<String[]>();
private HashSet<String> dictionary = new HashSet<>();
private String puzzleFile;
private String dictFile;
private String outputFile;
private int puzzleSize = 0;
public WordPuzzle(String puzzleFile, String dictFile,String outputFile){
this.puzzleFile = puzzleFile;
this.dictFile = dictFile;
this.outputFile = outputFile;
readPuzzle();
readDict();
}
/************************************************************************
* READ AND WRITE METHODS |
************************************************************************/
/**Method that parses a CSV file into a 2D matrix
* @return Returns a 2D matrix in the form of a List of String[]
*/
private List<String[]> readPuzzle(){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(new File(puzzleFile)));
String line = "";
String splitBy = ",";
while((line = br.readLine()) != null){
String[] row = line.split(splitBy);
puzzleSize = row.length;
puzzleBoard.add(row);
}
}catch(IOException e){
System.err.println("An error occured with the following file: " + puzzleFile);
System.exit(1);
}finally {
try {
br.close();
} catch (IOException e) {
System.err.println("Error when closing Reader!");
e.printStackTrace();
}
}
return puzzleBoard;
}
/**
* Method that parses a CSV file into a HashSet
* @return HashSet used to store the words in the dictionary given
*/
private HashSet<String> readDict(){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(new File(dictFile)));
String word = "";
while((word = br.readLine()) != null){
dictionary.add(word.toLowerCase());
}
}catch(IOException e){
System.err.println("An error occured with the following file: " + dictFile);
System.exit(1);
}finally {
try {
br.close();
} catch (IOException e) {
System.err.println("Error when closing Reader!");
e.printStackTrace();
}
}
return dictionary;
}
/**
* A method that takes the entries in a HashMap and writes
* them to a CSV file containing the solution to the puzzle in the form of:
*
* word, count
* earth,1
* ...
* scrabble,2
*/
private void writeOutputCSV(){
String comma = ",";
String nLine = "\n";
String header = "word,count";
FileWriter fw = null;
try{
fw = new FileWriter(outputFile);
fw.append(header);
fw.append(nLine);
for(Map.Entry<String, Integer> entry:actualWords.entrySet()){
fw.append(entry.getKey());
fw.append(comma);
fw.append(entry.getValue().toString());
fw.append(nLine);
}
}catch(Exception e){
System.err.println("Error writing to file: "+outputFile);
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
System.err.println("Error when closing FileWriter!");
e.printStackTrace();
}
}
}
/************************************************************************
* SEARCH METHODS |
************************************************************************/
/**
* The following methods index into the 2D matrix using for loops in order to build
* a potential word.
*/
private void findWordsInRows(){
StringBuilder b = new StringBuilder();
String word = null;
String reverse = null;
for(int row=0; row<puzzleSize; ++row){
for(int col=0; col<puzzleSize; ++col){
word = b.append(puzzleBoard.get(row)[col]).toString();
}
parseString(word);
b.setLength(0);
}
}
private void findWordsInColumns(){
StringBuilder b = new StringBuilder();
String word = null;
for(int col=0; col<puzzleSize; ++col){
for(int row=0; row<puzzleSize; ++row){
word = b.append(puzzleBoard.get(row)[col]).toString();
}
parseString(word);
b.setLength(0);
}
}
private void findWordsInDiagonals(){
StringBuilder b = new StringBuilder();
String word = null;
for(int k=puzzleSize-1; k>0; k--){
for(int col=0, row=k; row<puzzleSize-1; row++, col++){
word = b.append(puzzleBoard.get(row)[col]).toString();
if(word.length()==puzzleSize){
parseString(word);
}
}
b.setLength(0);
}
for(int k=0; k<puzzleSize-1; k++){
for(int row=0, col=k; col<=puzzleSize-1; row++, col++){
word = b.append(puzzleBoard.get(row)[col]).toString();
if(word.length()==puzzleSize){
parseString(word);
}
}
b.setLength(0);
}
for( int k=0; k<puzzleSize; k++) {
for( int col=0; col<=k; col++) {
int row = k - col;
word = b.append(puzzleBoard.get(row)[col]).toString();
if(word.length()==puzzleSize){
parseString(word);
}
}
b.setLength(0);
}
for(int k=puzzleSize-2; k>=0; k--){
for(int row=0; row<=k; row++){
int col = k - row;
word = b.append(puzzleBoard.get(puzzleSize-row-1)[puzzleSize-col-1]).toString();
if(word.length()==puzzleSize){
parseString(word);
}
}
b.setLength(0);
}
}
/**
* A method that looks at the list of possible words and see if they contain any words in the dictionary.
* If it finds a matching word then it checks the hashMap that contains the actual words. If the hashMap already
* has that word accounted for then it simply increases the count for that word. If it's a new word, then it creates a
* new entry in the map with the new word as the key and the respective value set to 1.
*
*/
private void findWordsInList(){
for(String word:potentialWords){
if(dictionary.contains(word)){
if(actualWords.containsKey(word)){
actualWords.put(word, actualWords.get(word)+1);
}
else{
actualWords.put(word, 1);
}
}
}
}
/************************************************************************
* DRIVER METHODS |
************************************************************************/
/**
* The following methods simply call other methods,
* in other words they get the ball rolling.
*/
public void solvePuzzle(){
findWordsInBoard();
findWordsInList();
writeOutputCSV();
}
private void findWordsInBoard(){
findWordsInRows();
findWordsInColumns();
findWordsInDiagonals();
}
/************************************************************************
* HELPER METHODS |
************************************************************************/
/**
* Simple method to reverse a string.
*
* @param The string you want to reverse
* @return The reversed string
*/
private String reverseString(String word){
String reverse = new StringBuilder(word).reverse().toString();
return reverse;
}
/**
* A method that parses a completed string built from a row, column or diagonal, using the substring method.
* If the substring is larger than 3 (smallest length of the average word) then it adds it to the list of potential
* words.The reversed version of the word also gets added to the list.
*
* @param A complete line, i.e. a row, column, or diagonal
*/
private void parseString(String line){
int length = line.length();
for(int i=0; i<length; i++){
for(int j=length; j>=i+3; j--){
potentialWords.add(line.substring(i, j));
potentialWords.add(reverseString(line.substring(i, j)));
}
}
}
}