CS1103 Programming Assignment Unit 5 - Use HashSet and TreeSet to implement a spell checker

Questions

Use HashSet and TreeSet to implement a spell checker. Specifically, please read the file words.txt, which contains a list of English words, and store them in a set. Then, check the words in a file provided by the user, and provide a list of possible correct spellings.

1. Codes and Files

Codes

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
import javax.swing.*;
import java.io.File;
import java.util.*;

public class CheckingWords {
public static void main(String[] args) {
Scanner filein = null;
Scanner inputFile = null;
// Use a LinkedHashSet to store corrections, the same misspelled word is only saved once
LinkedHashSet<String> outputs = new LinkedHashSet<>();

try {
// use a HashSet to store the words in the dictionary
HashSet<String> hashSet = new HashSet<>();

// read the words, change them to lowercase, and store them in the HashSet
filein = new Scanner(new File("F:\\words.txt"));
while (filein.hasNext()) {
String lowercaseWord = filein.next().toLowerCase();
hashSet.add(lowercaseWord);
}

// read the file from the user and skip over any non-letter characters
inputFile = new Scanner(getInputFileNameFromUser()).useDelimiter("[^a-zA-Z]+");

// process the file from the user
while (inputFile.hasNext()) {
String originalWord = inputFile.next();

// convert the word to lowercase for comparison with lowercase words in the hashSet
String lowerCaseWord = originalWord.toLowerCase();

// process the bad word and and add the corrections to the outputs set
if ((!hashSet.contains(lowerCaseWord))) {
var treeSet = corrections(lowerCaseWord, hashSet);
if (treeSet.isEmpty()) {
outputs.add(lowerCaseWord + ":" + " (no suggestions)");
} else {
outputs.add(lowerCaseWord + ": " + iterateSet(treeSet));
}
}
}

// print the outputs, and the same misspelled word is output only once
for (String s : outputs) {
System.out.println(s);
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
Objects.requireNonNull(filein).close();
Objects.requireNonNull(inputFile).close();
}
}

/**
* Iterate the set in the required manner.
* For example: txt: tat, tet, text, tit, tot, tut
*/
static String iterateSet(TreeSet<String> treeSet) {
int count = 0;
StringBuilder stringBuilder = new StringBuilder();

for (String s : treeSet) {
stringBuilder.append(s);
if (count < treeSet.size() - 1) {
stringBuilder.append(", ");
}
count++;
}

return stringBuilder.toString();
}


/***
* Lets the user select an input file using a standard file
* selection dialog box. If the user cancels the dialog
* without selecting a file, the return value is null.
*/
static File getInputFileNameFromUser() {
JFileChooser fileDialog = new JFileChooser();
fileDialog.setDialogTitle("Select File for Input");
int option = fileDialog.showOpenDialog(null);
if (option != JFileChooser.APPROVE_OPTION)
return null;
else
return fileDialog.getSelectedFile();
}

/**
* Providing a list of possible correct spellings.
* The possible corrections that the program considers are as follows:
* • Delete any one of the letters from the misspelled word.
* • Change any letter in the misspelled word to any other letter.
* • Insert any letter at any point in the misspelled word.
* • Swap any two neighboring characters in the misspelled word.
* • Insert a space at any point in the misspelled word (and check that both of the words that are produced are in the dictionary)
*
* @param badWord
* @param dictionary
* @return
*/
static TreeSet corrections(String badWord, HashSet dictionary) {
TreeSet<String> treeSet = new TreeSet<String>();

// Delete any one of the letters from the misspelled word
for (int i = 0; i < badWord.length(); i++) {
String newWord = badWord.substring(0, i) + badWord.substring(i + 1);
if ((dictionary.contains(newWord))) {
treeSet.add(newWord);
}
}

// Change any letter in the misspelled word to any other letter
for (int i = 0; i < badWord.length(); i++) {
for (char ch = 'a'; ch <= 'z'; ch++) {
String newWord = badWord.substring(0, i) + ch + badWord.substring(i + 1);
if (dictionary.contains(newWord)) {
treeSet.add(newWord);
}
}
}

// Insert any letter at any point in the misspelled word
for (int i = 0; i <= badWord.length(); i++) {
for (char ch = 'a'; ch <= 'z'; ch++) {
String newWord = badWord.substring(0, i) + ch + badWord.substring(i);
if (dictionary.contains(newWord)) {
treeSet.add(newWord);
}
}
}

// Swap any two neighboring characters in the misspelled word
for (int i = 0; i < badWord.length() - 1; i++) {
String newWord = badWord.substring(0, i) + badWord.substring(i + 1, i + 2) + badWord.substring(i, i + 1) + badWord.substring(i + 2);
if (dictionary.contains(newWord)) {
treeSet.add(newWord);
}

}

// Insert a space at any point in the misspelled word,
// and check that both of the words that are produced are in the dictionary
for (int i = 1; i < badWord.length(); i++) {
String twoNewWords = badWord.substring(0, i) + " " + badWord.substring(i);
StringTokenizer stringTokenizer = new StringTokenizer(twoNewWords, " ");
while (stringTokenizer.hasMoreTokens()) {
String newWord1 = stringTokenizer.nextToken();
String newWord2 = stringTokenizer.nextToken();
// check that both of the words that are produced are in the dictionary
if (dictionary.contains(newWord1) && dictionary.contains(newWord2)) {
treeSet.add(twoNewWords);
}
}
}

return treeSet;
}
}

Files
Please click here to download the dictionary: words.txt

Please click here to download the test file: testPage.txt

2. Outputs

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
s: a, as, cs, es, gs, is, ks, ls, ms, os, rs, sb, sc, se, sh, si, sm, sn, so, sq, sr, st, ts, us, vs
hashset: hash set
treeset: tree set
isempty: is empty
txt: tat, tet, text, tit, tot, tut
filein: file in
hasnext: has next
tk: bk, ok, ta, tb, tc, th, ti, tl, tm, to, ts, ty
wordlist: word list
ve: ave, be, eve, fe, ge, he, le, me, ne, re, se, va, vet, vex, vie, vs, we, xe, ye
getinputfilenamefromuser: (no suggestions)
jfilechooser: (no suggestions)
filedialog: file dialog
setdialogtitle: (no suggestions)
int: ant, dint, hint, in, ina, inc, ind, ink, inn, ins, inti, into, it, lint, mint, nit, pint, tint
showopendialog: (no suggestions)
getselectedfile: (no suggestions)
usedelimiter: use delimiter
za: a, ba, ca, fa, ga, ha, la, ma, na, pa, ra, ta, va, ya, zap, zn, zr
z: a, hz, oz, zn, zr
shouldn: should
t: a, at, it, lt, mt, pt, st, ta, tb, tc, th, ti, tl, tm, to, ts, ty
badword: bad word
html: (no suggestions)
cpsc: (no suggestions)
cvs: cs, vs
href: ref
pre: are, ere, ire, ore, pare, pee, per, pie, poe, pore, prep, pres, prey, pro, pry, pure, pyre, re
hav: av, ha, had, hag, hah, hal, ham, han, has, hat, have, haw, hay

Process finished with exit code 0

​


This is the ending, thanks for reading.