-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpellChecker.java
More file actions
135 lines (104 loc) · 4.22 KB
/
Copy pathSpellChecker.java
File metadata and controls
135 lines (104 loc) · 4.22 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
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
/* Abenezer Amanuel
* ata2152
* 4/18/2022
* This class implements a spell-checker using a hash table
*/
import java.util.*;
import java.io.*;
public class SpellChecker implements SpellCheckerInterface {
private HashSet<String> wordset;
//constructor takes in the file address of the dictionary and insert its
//words into a hashset
public SpellChecker(String filename) {
wordset = new HashSet<>();
File dictionaryFile = null;
Scanner scan1 = null;
try {
dictionaryFile = new File(filename);
scan1 = new Scanner(dictionaryFile);
} catch(FileNotFoundException e) {
System.err.println("Dictionary file not found!");
System.exit(-1);
} catch(Exception e) {
System.err.println("Incorrect file input!");
System.exit(-1);
}
//remove punctuation and white space and add to hashset
while(scan1.hasNextLine()) {
String word = scan1.nextLine();
//source: https://stackoverflow.com/questions/7552253/how-to-remove-special-characters-from-a-string
word = word.replaceAll("[^A-Za-z0-9]", "");
wordset.add((word.toLowerCase()).trim());
}
scan1.close();
}
//accepts an input file and returns a list of all words that don't match
//any from our dictionary
public List<String> getIncorrectWords(String filename) {
List<String> incorrectWords = new ArrayList<>();
File inputFile = null;
Scanner scan2 = null;
try {
inputFile = new File(filename);
scan2 = new Scanner(inputFile);
} catch(FileNotFoundException e) {
System.err.println("Input file not found!");
System.exit(-1);
} catch(Exception e) {
System.err.println("Incorrect file input!");
System.exit(-1);
}
//add the non-matching words to the created array list
while(scan2.hasNextLine()) {
String[] line = (scan2.nextLine()).split(" ");
for(String s : line) {
//source: see above
s = s.replaceAll("[^A-Za-z0-9]", "");
s = (s.toLowerCase()).trim();
if(s != null && !(s.equals("")) && !(wordset.contains(s)))
incorrectWords.add(s);
}
}
scan2.close();
return incorrectWords;
}
//takes in a word and returns a set of all possible suggestions based on
//our dictionary
public Set<String> getSuggestions(String word) {
if(word == null || word.equals("") || wordset.contains(word)) {
Set<String> set1 = new HashSet<>();
return set1;
} else {
Set<String> set2 = new HashSet<>();
char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w',
'x','y','z'};
//looks for suggestions after adding one character
for(int i = 0; i <= word.length(); i++) {
for(char c : letters) {
StringBuilder sb1 = new StringBuilder(word);
sb1.insert(i, c);
if(wordset.contains(sb1.toString()))
set2.add(sb1.toString());
}
}
//looks for suggestions by removing one character
for(int i = 0; i < word.length(); i++) {
StringBuilder sb2 = new StringBuilder(word);
sb2.deleteCharAt(i);
if(wordset.contains(sb2.toString()))
set2.add(sb2.toString());
}
//looks for suggestions by swapping adjacent characters
for(int i = 0; i < word.length()-1; i++) {
StringBuilder sb3 = new StringBuilder(word);
char temp = sb3.charAt(i);
sb3.setCharAt(i, sb3.charAt(i+1));
sb3.setCharAt(i+1, temp);
if(wordset.contains(sb3.toString()))
set2.add(sb3.toString());
}
return set2;
}
}
}