-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
115 lines (92 loc) · 3.19 KB
/
Dictionary.java
File metadata and controls
115 lines (92 loc) · 3.19 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
import java.io.*;
import java.util.TreeSet;
/**
* A dictionary manages a collection of known, correctly-spelled words.
*
* The dictionary is case insensitive and only stores "valid" word.
* A valid word is any sequence of letters (as determined by Character.isLetter)
* or apostrophes characters.
*/
public class Dictionary {
private TreeSet<String> index;
@SuppressWarnings("unused")
private int numWords;
/**
* Constructs a dictionary from words provided by a TokenScanner.
* <p>
* A word is any sequence of letters (see Character.isLetter) or apostrophe
* characters. All non-word tokens provided by the TokenScanner should be ignored.
*
* <p>
*
* @param ts sequence of words to use as a dictionary
* @throws IOException if error while reading
* @throws IllegalArgumentException if the provided reader is null
*/
public Dictionary(TokenScanner ts) throws IOException {
//use TreeSet for fast "contains" and "add methods
//as well as no-duplicate invariant
index = new TreeSet<String>();
numWords = 0;
//int duplicates = 0;
//make the dictionary by iterating through the file
//System.out.println(ts.hasNext());
if (ts == null) {
throw new IllegalArgumentException ();
}
else {
while (ts.hasNext()) {
String word = ts.next();
if (TokenScanner.isWord(word) && !(index.contains(word))) {
word = word.toLowerCase();
index.add(word);
numWords++;
}
}
}
}
/**
* Constructs a dictionary from words from a file.
*
*
* @param filename location of file to read words from
* @throws FileNotFoundException if the file does not exist
* @throws IOException if error while reading
*/
public static Dictionary make(String filename) throws IOException {
Reader r = new FileReader(filename);
Dictionary d = new Dictionary(new TokenScanner(r));
r.close();
return d;
}
/**
* Returns the number of unique words in this dictionary. This count is
* case insensitive: if both "DOGS" and "dogs" appeared in the file, it
* should only be counted once in the returned sum.
*
* @return number of unique words in the dictionary
*/
public int getNumWords() {
return index.size();
}
/**
* Test whether the input word is present in the Dictionary.
* <p>
* This check should be case insensitive. For example, if the
* Dictionary contains "dog" or "dOg" then isWord("DOG") should return true.
* <p>
* The argument to this method should only contain letters or apostrophe characters.
* The empty string, or strings containing only whitespace characters, are not in the dictionary.
* If the argument is null, the method returns false.
* <p>
* Calling this method should not re-open or re-read the source file.
*
* @param word a string token to check. Assume any leading or trailing
* whitespace has already been removed.
* @return whether the word is in the dictionary
*/
public boolean isWord(String word) {
word = word.toLowerCase();
return index.contains(word);
}
}