Repository navigation
Expand file tree
/
Copy pathspellchecker.java
More file actions
145 lines (117 loc) · 5.56 KB
/
Copy pathspellchecker.java
File metadata and controls
145 lines (117 loc) · 5.56 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
import java.io.*;
import java.util.*;
public class spellchecker {
Hashtable<String,String> dictionary; // To store all the words of the dictionary
boolean suggestWord ; // To indicate whether the word is spelled correctly or not.
public static void main(String [] args)
{
spellchecker checker = new spellchecker();
}
public spellchecker()
{
dictionary = new Hashtable<String,String>();
System.out.println("******Welcome to the spell checker using Hashtable*****");
System.out.println("The spell checker would check every line from the input file and then give suggestions if needed after each line. \n\n");
try
{
//Read and store the words of the dictionary
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.txt"));
while (dictReader.ready())
{
String dictInput = dictReader.readLine() ;
String [] dict = dictInput.split("\\s");
for(int i = 0; i < dict.length;i++)
{
// key and value are identical
dictionary.put(dict[i], dict[i]);
}
}
dictReader.close();
String file = "inputtext.txt";
// Read and check the input from the text file
BufferedReader inputFile = new BufferedReader(new FileReader(file));
System.out.println("Reading from "+file);
// Initialising a spelling suggest object
spellingsuggest suggest = new spellingsuggest("wordprobabilityDatabase.txt");
// Reads input lines one by one
while ( inputFile.ready() )
{
String s = inputFile.readLine() ;
System.out.println (s);
String[] result = s.split("\\s");
for (int x=0; x<result.length; x++)
{
suggestWord = true;
String outputWord = checkWord(result[x]);
if(suggestWord)
{
System.out.println("Suggestions for "+result[x]+" are: "+suggest.correct(outputWord)+"\n");
}
}
}
inputFile.close();
}
catch (IOException e)
{
System.out.println("IOException Occured! ");
e.printStackTrace();
// System.exit(-1);
}
}
public String checkWord(String wordToCheck)
{
String wordCheck, unpunctWord;
String word = wordToCheck.toLowerCase();
// if word is found in dictionary then it is spelt correctly, so return as it is.
//note: inflections like "es","ing" provided in the dictionary itself.
if ((wordCheck = (String)dictionary.get(word)) != null)
{
suggestWord = false; // no need to ask for suggestion for a correct word.
return wordCheck;
}
// Removing punctuations at end of word and giving it a shot ("." or "." or "?!")
int length = word.length();
//Checking for the beginning of quotes(example: "she )
if (length > 1 && word.substring(0,1).equals("\""))
{
unpunctWord = word.substring(1, length);
if ((wordCheck = (String)dictionary.get(unpunctWord)) != null)
{
suggestWord = false; // no need to ask for suggestion for a correct word.
return wordCheck ;
}
else // not found
return unpunctWord; // removing the punctuations and returning
}
// Checking if "." or ",",etc.. at the end is the problem(example: book. when book is present in the dictionary).
if( word.substring(length - 1).equals(".") || word.substring(length - 1).equals(",") || word.substring(length - 1).equals("!")
|| word.substring(length - 1).equals(";") || word.substring(length - 1).equals(":"))
{
unpunctWord = word.substring(0, length-1);
if ((wordCheck = (String)dictionary.get(unpunctWord)) != null)
{
suggestWord = false; // no need to ask for suggestion for a correct word.
return wordCheck ;
}
else
{
return unpunctWord; // removing the punctuations and returning
}
}
// Checking for "!\"",etc ... in the problem (example: watch!" when watch is present in the dictionary)
if (length > 2 && word.substring(length-2).equals(",\"") || word.substring(length-2).equals(".\"")
|| word.substring(length-2).equals("?\"") || word.substring(length-2).equals("!\"") )
{
unpunctWord = word.substring(0, length-2);
if ((wordCheck = (String)dictionary.get(unpunctWord)) != null)
{
suggestWord = false; // no need to ask for suggestion for a correct word.
return wordCheck ;
}
else // not found
return unpunctWord; // removing the inflections and returning
}
// After all these checks too, word could not be corrected, hence it must be misspelt word. return and ask for suggestions
return word;
}
}