forked from steve-coward/CS454-SW-Engineering-WordSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionary.java
79 lines (67 loc) · 2.23 KB
/
Dictionary.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
//
package wordSearch;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.io.*;
public class Dictionary {
// HashSet is a collection. You can not store duplicate value in HashSet.
// This class makes no guarantees as to the order of the map; in particular,
// it does not guarantee that the order will remain constant over time.
// This implementation provides constant-time performance for the basic
// operations (get and put), assuming the hash function disperses the
// elements properly among the buckets.
HashSet<String> m_dictionary;
HashMap<Integer, Integer> m_wordCount;
public Dictionary () {
m_dictionary = new HashSet<String>();
m_wordCount = new HashMap<Integer, Integer>();
for (int i = 0; i < 15; i++ ) {
m_wordCount.put(i, 0);
}
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("dictionary.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
// dictionary.txt should contain 16610 3, 4, and 5 letter words
while ((strLine = br.readLine()) != null) {
// Add the string to the dictionary
m_dictionary.add(strLine);
// Update word length count
m_wordCount.put(new Integer(strLine.length()),
m_wordCount.get(strLine.length()) + 1);
}
//Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
};
public boolean IsWord(String word) {
return(m_dictionary.contains(word));
}
public String findMatch(String regExp, int randNum) {
String match = "";
Iterator<String> it=m_dictionary.iterator();
if (randNum > m_wordCount.get(regExp.length())) {
randNum = m_wordCount.get(regExp.length());
}
else if (randNum <= 0) {
randNum = 1;
}
while(it.hasNext() && (randNum > 0)) {
String word = (String)it.next();
if ((word.length() == regExp.length())
&& (word.matches(regExp))) {
randNum--;
match = word;
}
}
return(match);
}
}