-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpellCheckV2.java
More file actions
62 lines (60 loc) · 2.06 KB
/
Copy pathSpellCheckV2.java
File metadata and controls
62 lines (60 loc) · 2.06 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
import java.io.*;
import java.util.*;
class SpellCheckV2
{
// dictionary here !
//Happy Programmers! :)
private final static int DICT_SIZE=178695;
private static String[] dictionary=new String[DICT_SIZE],words;
private static char[] invalid={'.',',','!','$','?','(',')',';',':','#','^'};
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new FileReader("sample.txt"));
BufferedReader dict=new BufferedReader(new FileReader("dictionary.txt"));
FileWriter fw = new FileWriter("wrong.html",false);
String prefix = "<!DOCTYPE html>\n<html>\n<body>\n<h1>List of Incorrect Words in the Text</h1>\n<p style=\"color:red;\">\n";
String suffix = "</p></body></html>";
fw.write(prefix);
loadDictionary(dict);
dict.close();
String in;
while((in=br.readLine())!=null)
{
words=in.split(" ");
for(String i:words)
{
for(char j:invalid) {
i=i.replace(j,' ');
}
i=i.trim();
if(!isCorrect(i))fw.write(i+"<br>");
}
}
fw.write(suffix);
br.close();
fw.close();
}
private static boolean isCorrect(String word)
{
int lb=0,ub=dictionary.length-1,mid;
String curr;
word=word.toUpperCase();
while(lb<=ub)
{
mid=(lb+ub)/2;
curr=dictionary[mid];
if(curr.equals(word)) return true;
else if(curr.compareTo(word)>0) ub=mid-1;
else lb=mid+1;
}
return false;
}
private static void loadDictionary(BufferedReader brc) throws IOException
{
int wordNo = 0; //To Keep Track of the Word Number
String line = "";
while((line = brc.readLine())!=null) { //Checking if it's not the E.O.F
dictionary[wordNo++] = line; // Assigning Words to the Dictionary Array
}
}
}