This repository was archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpellChecker.java
136 lines (116 loc) · 3.42 KB
/
SpellChecker.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
package assign08;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Represents a "dictionary" of strings using a binary search tree and offers
* methods for spell-checking documents.
*
* @author Erin Parker, Paul Nuffer and Nils Streedain
* @version March 17, 2021
*/
public class SpellChecker {
private BinarySearchTree<String> dictionary;
/**
* Default constructor--creates empty dictionary.
*/
public SpellChecker() {
dictionary = new BinarySearchTree<String>();
}
/**
* Creates dictionary from a list of words.
*
* @param words - the List of Strings used to build the dictionary
*/
public SpellChecker(List<String> words) {
this();
buildDictionary(words);
}
/**
* Creates dictionary from a file.
*
* @param dictionaryFile - the File that contains Strings used to build the
* dictionary
*/
public SpellChecker(File dictionaryFile) {
this();
buildDictionary(readFromFile(dictionaryFile));
}
/**
* Add a word to the dictionary.
*
* @param word - the String to be added to the dictionary
*/
public void addToDictionary(String word) {
dictionary.add(word);
}
/**
* Remove a word from the dictionary.
*
* @param word - the String to be removed from the dictionary
*/
public void removeFromDictionary(String word) {
dictionary.remove(word);
}
/**
* Spell-checks a document against the dictionary.
*
* @param document_file - the File that contains Strings to be looked up in the
* dictionary
* @return a List of misspelled words
*/
public List<String> spellCheck(File documentFile) {
List<String> wordsToCheck = readFromFile(documentFile);
List<String> missSpelledWords = new ArrayList<>();
for (String word : wordsToCheck)
if (!dictionary.contains(word))
missSpelledWords.add(word);
return missSpelledWords;
}
/**
* Fills in the dictionary with the input list of words.
*
* @param words - the List of Strings to be added to the dictionary
*/
private void buildDictionary(List<String> words) {
dictionary.addAll(words);
}
/**
* Returns a list of the words contained in the specified file. (Note that
* symbols, digits, and spaces are ignored.)
*
* @param file - the File to be read
* @return a List of the Strings in the input file
*/
private List<String> readFromFile(File file) {
ArrayList<String> words = new ArrayList<String>();
try {
/*
* Java's Scanner class is a simple lexer for Strings and primitive types (see
* the Java API, if you are unfamiliar).
*/
Scanner fileInput = new Scanner(file);
/*
* The scanner can be directed how to delimit (or divide) the input. By default,
* it uses whitespace as the delimiter. The following statement specifies
* anything other than alphabetic characters as a delimiter (so that punctuation
* and such will be ignored). The string argument is a regular expression that
* specifies "anything but an alphabetic character". You need not understand any
* of this for the assignment.
*/
fileInput.useDelimiter("\\s*[^a-zA-Z]\\s*");
while (fileInput.hasNext()) {
String s = fileInput.next();
if (!s.equals(""))
words.add(s.toLowerCase());
}
fileInput.close();
}
catch(FileNotFoundException e) {
System.err.println("File " + file + " cannot be found.");
}
return words;
}
}