-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpellDictionary.java
More file actions
157 lines (145 loc) · 4.91 KB
/
SpellDictionary.java
File metadata and controls
157 lines (145 loc) · 4.91 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
/**
* author: Felicia Villalobos
* version: 4/19/2017
*/
public class SpellDictionary{
private HashSet<String> dictHash;
private HashSet<String> spellingSuggestions;
//new constructor for SpellDictionary
public SpellDictionary(String fname){
dictHash = new HashSet<String>();
buildDictionary(fname);
}
/**
* buildDictionary()
* method that uses a file to build a dictionary of type HashSet
* @param String fname
*/
private void buildDictionary(String fname){
try{
BufferedReader buildD = new BufferedReader(new FileReader(fname));
String wordsforDict = buildD.readLine();
while(wordsforDict != null){
dictHash.add(wordsforDict);
wordsforDict = buildD.readLine();
}
} catch(FileNotFoundException exception){
System.err.println("File not found!");
System.exit(-1);
}catch(IOException e){
System.err.println("IO exception!");
System.exit(-1);
}
}
/**
*spellCheck()
* a method that holds all of the smaller checking methods (delete, etc)
*@param String checkingWord
*/
public HashSet<String> spellCheck(String checkingWord){
spellingSuggestions = new HashSet<String>();
if(dictHash.contains(checkingWord) == false){
deletion(checkingWord);
insert(checkingWord);
substitutions(checkingWord);
splits(checkingWord);
}
else{
// System.out.println("No misspelling");
}
// System.out.println("Misspelling, here are some suggestions:");
System.out.println(spellingSuggestions);
return spellingSuggestions;
}
/**
*deletion()
* a method that checks if a word is in the dictionary by deleting
* one letter at a time
* @param String word
*/
public void deletion(String word){
for(int j = 0; j < word.length(); j++){
String first = word.substring(0,j); //now we have word w/o one of the letters
String option = word.substring(j+1);
String suggestion = first.concat(option);
if(dictHash.contains(suggestion) == true){
spellingSuggestions.add(suggestion);
// System.out.println("spellingSuggestions Delete= " + spellingSuggestions);
}
else{
//System.out.println("Sorry, not a real word!" + suggestion);
}
}//close for loop
}
/**
* insert()
* a method that checks if a word is in the dicitionary by inserting
* every letter in the alphabet in every spot of the word
* @param String word
*/
public void insert(String word){
// System.out.println("inside insert");
String alphabet = "abcdefghijklmnopqrstuvwxyz";
StringBuilder suggestion = new StringBuilder(word);
// System.out.println(suggestion);
for(int l = 0; l < alphabet.length(); l++){
// System.out.println("inside first for");
char letter = alphabet.charAt(l);
for(int k = 0; k <= word.length(); k++){
// System.out.println("inside second for");
suggestion.insert(k, letter);
if(dictHash.contains(suggestion.toString()) == true){
// System.out.println("insde if");
spellingSuggestions.add(suggestion.toString());
// System.out.println("spellingSuggestions Insert= " + spellingSuggestions);
}
suggestion.deleteCharAt(k);
}
}
}
/**
*substitutions()
*a method that replaces each letter of the word being checked with
*every other letter in the alphabet and looks for a match in the dict
*@param String word
*/
public void substitutions(String word){
String alphabet = "abcdefghijklmnopqrstuvwxyz";
for(int m = 0; m < alphabet.length(); m++){
char letterS = alphabet.charAt(m);
for(int n = 0; n <= word.length(); n++){
StringBuilder suggestion = new StringBuilder(word);
// System.out.println("current letter=" + letterS);
String letter = Character.toString(letterS);
// System.out.println(letter);
suggestion.replace(n,n+1, letter);
// System.out.println("sub suggestion=" + suggestion);
if(dictHash.contains(suggestion.toString()) == true){
spellingSuggestions.add(suggestion.toString());
// System.out.println("Spelling suggestions Sub=" + spellingSuggestions);
} //closes conditional
}
}
}
/**
*split()
*a method that splits the word being checked to see if it is actually
*two words that both exist in the dictionary
*@param String word
*/
public void splits(String word){
for(int z = 0; z < word.length(); z++){
String firstHalf = word.substring(0,z);
String lastHalf = word.substring(z);
if(dictHash.contains(firstHalf) && dictHash.contains(lastHalf)){
String suggestion = firstHalf.concat(lastHalf);
spellingSuggestions.add(suggestion);
}
}
}
}