- It only accepts English words
- It will spell check the same word if it's used more than once
- It prints out each suggestion in a new line, so large websites produce a messy output.
import java.io.*;
import java.util.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;
public class ParseCleanCheck {
static Hashtable<String, String> dictionary;// To store all the words of the
// dictionary
static boolean suggestWord;// To indicate whether the word is spelled
// correctly or not.
static Scanner urlInput = new Scanner(System.in);
public static String cleanString = "";cleanString;
public static String url = "";
public static boolean correct = true;
/**
* PARSER METHOD
*/
public static void PageScanner() throws IOException {
System.out.println("Pick an english website to scan.");
// This do-while loop allows the user to try again after a mistake
do {
try {
System.out.println("Enter a URL, starting with http://");
url = urlInput.nextLine();
// This creates a document out of the HTML on the web page
Document doc = Jsoup.connect(url).get();
// This converts the document into a string to be cleaned
String htmlToClean = doc.toString();
cleanString = Jsoup.clean(htmlToClean, Whitelist.none());
correct = false;
} catch (Exception e) {
System.out.println("Incorrect format for a URL. Please try again.");
}
} while (correct);
}
/**
* SPELL CHECKER METHOD
*/
public static void SpellChecker() throws IOException {
dictionary = new Hashtable<String, String>();
System.out.println("Searching for spelling errors ... ");
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"); // create an array of
// dictionary words
for (int i = 0; i < dict.length; i++) {
// key and value are identical
dictionary.put(dict[i], dict[i]);
}
}
dictReader.close();
String user_text = "";
// Initializing a spelling suggestion object based on probability
SuggestSpelling suggest = new SuggestSpelling("wordprobabilityDatabase.txt");
// get user input for correction
{
user_text = cleanString;
String[] words = user_text.split(" ");
Set<String> wordSet = new HashSet<>();
int error = 0;
for (String word : words) {
suggestWord =if(!wordSet.contains(word)) true;{
String outputWord = checkWord(word);
suggestWord = true;
String outputWord = checkWord(word);
if (suggestWord) {
System.out.println("Suggestions for " + word + " are: " + suggest.correct(outputWord) + "\n");
error++;
}
}
wordSet.add(word);
}
if (error == 0) {
System.out.println("No mistakes found");
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
/**
* METHOD TO SPELL CHECK THE WORDS IN A STRING. IS USED IN SPELL CHECKER
* METHOD THROUGH THE "WORD" STRING
*/
public static String checkWord(String wordToCheck) {
String wordCheck, unpunctWord;
String word = wordToCheck.toLowerCase();
// if word is found in dictionary then it is spelled correctly, so
// return as it is.
if ((wordCheck = (String) dictionary.get(word)) != null) {
suggestWord = false; // no need to ask for suggestion for a correct
// word.
return wordCheck;
}
// Removing punctuation 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 punctuation and returning it
// clean
}
}
// 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
}
// If after all of these checks a word could not be corrected, return as
// a misspelled word.
return word;
}
}