-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaryClean.java
More file actions
59 lines (56 loc) · 2.5 KB
/
Copy pathDictionaryClean.java
File metadata and controls
59 lines (56 loc) · 2.5 KB
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
import java.io.File; // Import the File class
import java.io.FileWriter;
import java.util.Scanner; // Import the Scanner class to read text files
import java.io.IOException;
public class DictionaryClean {
/*
Takes a dictionary file and outputs a cleaned dictionary that doesn't include words not allowed in Countdown.
*/
public static void main(String[] args) {
try {
// sets current folder directory, may need to be altered depending on where running from
String baseDirectory = System.getProperty("user.dir") + "\\res\\";
// read dictionary text file
File fileObject = new File(baseDirectory + "dictionary.txt");
Scanner myReader = new Scanner(fileObject);
// write cleaned dictionary text file
FileWriter myWriter = new FileWriter(baseDirectory + "dictionary_cleaned.txt");
// if true, keep the current dictionary entry in cleaned version
boolean keep;
// hold the current dictionary entry
String line;
// contains characters that are not allowed in words in cleaned dictionary
String exclude_chars = "-./'&0123567890";
// loop over words in dictionary
while (myReader.hasNextLine()) {
// start with keep as true
keep = true;
// set next line in list
line = myReader.nextLine();
// loop over forbidden characters
for(int i = 0; i < exclude_chars.length(); i++){
// if any of these characters are in word
if(line.contains("" + exclude_chars.charAt(i))){
// don't keep line and stop look
keep = false;
break;
}
}
// if still keeping line, check it's not over 9 characters and there are no upper case characters
if(keep && (line.length() > 9 || !line.equals(line.toLowerCase()))) {
// don't keep
keep = false;
}
// if word allowed, write to new file
if(keep) {
myWriter.write(line + "\n");
}
}
myWriter.close();
myReader.close();
} catch (IOException e) { // catch exceptions in reading writing files
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}