-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordCorrection.java
166 lines (159 loc) · 6.3 KB
/
WordCorrection.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.*;
public class WordCorrection
{
public WordCorrection()
{
}
private Hashtable<String, Integer> wordList()
{
Hashtable<String, Integer> wordSet = new Hashtable<String, Integer>();
String dirPath = "doc";
// Scanner scan = new Scanner(System.in);
FileScanner fileScan = new FileScanner();
//System.out.print("Please input standard doc set path: ");
//dirPath = scan.nextLine().trim();
// while (!fileScan.isDir(dirPath))
// {
// System.out.println("Error, the inputted path is not a valid directory.");
// System.out.print("Please try again to enter a directory path: ");
// dirPath = scan.nextLine().trim();
// }
//ArrayList<String> wordSet = new ArrayList<String>();
String[] files = fileScan.readDir(dirPath);
for (int index = 0; index < files.length; index++)
{
String fileContent = fileScan.readFile(dirPath + "\\" + files[index]);
fileContent = fileContent.replaceAll("[\\r\\n{}()'\"]", " ");
String[] fileContentList = fileContent.split(" ");
for (int index2 = 0; index2 < fileContentList.length; index2++)
{
if (!wordSet.containsKey(fileContentList[index2]))
wordSet.put(fileContentList[index2].toLowerCase(), 1);
}
}
return wordSet;
}
public void correction(String inputtedString)
{
inputtedString = inputtedString.trim();
Hashtable<String, Integer> wordSet = wordList();
String[] inputtedWordList = inputtedString.split(" ");
char[] alphabat = alphabat();
int correct = 0;
Hashtable<String, ArrayList<String>> correctedTable = new Hashtable<String, ArrayList<String>>();
for (int index = 0; index < inputtedWordList.length; index++)
{
ArrayList<String> corrected = new ArrayList<String>();
String word = inputtedWordList[index].toLowerCase();
if (wordSet.containsKey(word))
correct++;
else
{
ArrayList<String> added = add(word);
ArrayList<String> removed = remove(word);
ArrayList<String> replaced = replace(word);
ArrayList<String> changed = change(word);
for (String correctedWord : added)
{
if (wordSet.containsKey(correctedWord))
corrected.add(correctedWord);
}
for (String correctedWord : removed)
{
if (wordSet.containsKey(correctedWord))
corrected.add(correctedWord);
}
for (String correctedWord : replaced)
{
if (wordSet.containsKey(correctedWord))
corrected.add(correctedWord);
}
for (String correctedWord : changed)
{
if (wordSet.containsKey(correctedWord))
corrected.add(correctedWord);
}
correctedTable.put(word, corrected);
}
}
if (correct == inputtedWordList.length)
System.out.println("The inputted String is correct.");
else if (correctedTable.size() != 0)
{
Enumeration<String> wrongWords = correctedTable.keys();
while (wrongWords.hasMoreElements())
{
String wrongWord = wrongWords.nextElement();
ArrayList<String> corrected = correctedTable.get(wrongWord);
if (corrected.size() != 0)
{
System.out.println("The inputted word " + wrongWord + " may be wrong, do you mean the following?");
for (String correctedWord : corrected)
{
System.out.print(correctedWord + " ");
}
System.out.println();
}
else
System.out.println("The inputted word " + wrongWord + " may contain undetectable errors.");
}
}
}
private char[] alphabat()
{
char[] alphabat = new char[26];
for (int index = 0; index < alphabat.length; index++)
{
alphabat[index] = (char)('a' + index);
}
return alphabat;
}
private ArrayList<String> add(String word)
{
char[] alphabat = alphabat();
ArrayList<String> result = new ArrayList<String>();
for (int index = 0; index < word.length() + 1; index++)
{
for(int index2 = 0; index2 < alphabat.length; index2++)
{
String wordAltered = word.substring(0, index) + alphabat[index2] + word.substring(index, word.length());
result.add(wordAltered);
}
}
return result;
}
private ArrayList<String> remove(String word)
{
ArrayList<String> result = new ArrayList<String>();
for (int index = 0; index < word.length(); index++)
{
String wordAltered = word.substring(0, index) + word.substring(index + 1, word.length());
result.add(wordAltered);
}
return result;
}
public ArrayList<String> replace(String word)
{
char[] alphabat = alphabat();
ArrayList<String> result = new ArrayList<String>();
for (int index = 0; index < word.length(); index++)
{
for (int index2 = 0; index2 < alphabat.length; index2++)
{
String wordAltered = word.substring(0, index) + alphabat[index2] + word.substring(index + 1);
result.add(wordAltered);
}
}
return result;
}
private ArrayList<String> change(String word)
{
ArrayList<String> result = new ArrayList<String>();
for (int index = 0; index < word.length() - 1; index++)
{
String wordAltered = word.substring(0, index) + word.charAt(index + 1) + word.charAt(index) + word.substring(index + 2, word.length());
result.add(wordAltered);
}
return result;
}
}