Your program, called should take two command line arguments, the dictionary, and the text that you wish to spellcheck. Provide some sample text. Your program should be case insensitive (so you can toLower everything). Numbers and contractions are considered valid words. You may use the java HashTable or HashMap to implement this program; but it must use some kind of hash table.
Program 1:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Scanner;
public class Dictionary {
private static String stringInput;
private static String[] checkThis;
public static HashSet dictionary;
public static void main(String[] args) {
setup();
}
public static void setup() {
int tableSIZE = 59000;
dictionary = new
HashSet(tableSIZE);
try {
BufferedReader
bufferedReader = new BufferedReader(new
FileReader("C:\\data\\words.txt"));
String line =
null;
while ((line =
bufferedReader.readLine()) != null) {
dictionary.add(line);
}
prompt();
bufferedReader.close();
} catch (FileNotFoundException ex)
{
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void prompt() {
startwInput();
}
public static void startwInput() {
System.out.println("Please enter
text to check: ");
Scanner theLine = new
Scanner(System.in);
stringInput =
theLine.nextLine();
System.out.print("\n This word is
found: " + stringInput + "");
WordFinder grammarNazi = new
WordFinder();
splitString(removePunctuation(stringInput));
grammarNazi.initialCheck(checkThis);
}
public static void printDictionary(BufferedReader
bufferedReader) {
String line = null;
try {
while ((line =
bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException ex)
{
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void splitString(String sentence)
{
checkThis = sentence.split("
");
}
public static String removePunctuation(String
sentence) {
String newSentence;
newSentence =
sentence.toLowerCase().replaceAll("[^a-zA-Z\\s]",
"").replaceAll("\\s+", " ");
return newSentence;
}
}
Program 2:
import java.util.ArrayList;
import java.util.List;
public class MisSpell extends Dictionary {
public List<String> possibilities = new
ArrayList<String>();
private List<String> tempHolder = new
ArrayList<String>();
private int Ldistance = 0;
private String wrongWord;
public void generateMispellings(String wordCheck)
{
wrongWord = wordCheck;
try {
concatFL(wordCheck);
concatLL(wordCheck);
replaceFL(wordCheck);
replaceLL(wordCheck);
deleteFL(wordCheck);
deleteLL(wordCheck);
pluralize(wordCheck);
transposition(wordCheck);
} catch
(StringIndexOutOfBoundsException e) {
System.out.println();
} catch
(ArrayIndexOutOfBoundsException e) {
System.out.println();
}
}
public void concatFL(String word) {
char cur;
String tempWord = "";
for (int i = 97; i < 123; i++)
{
cur = (char)
i;
tempWord +=
cur;
tempWord =
tempWord.concat(word);
checkDict(tempWord);
tempWord =
"";
}
}
public void concatLL(String word) {
char cur;
String tempWord = "";
for (int i = 123; i > 97; i--)
{
cur = (char)
i;
tempWord =
tempWord.concat(word);
tempWord +=
cur;
checkDict(tempWord);
tempWord =
"";
}
}
public void replaceFL(String word) {
char cur;
String tempWord = "";
for (int i = 97; i < 123; i++)
{
cur = (char)
i;
tempWord = cur +
word.substring(1, word.length());
checkDict(tempWord);
tempWord =
"";
}
}
public void replaceLL(String word) {
char cur;
String tempWord = "";
for (int i = 97; i < 123; i++)
{
cur = (char)
i;
tempWord =
word.substring(0, word.length() - 1) + cur;
checkDict(tempWord);
tempWord =
"";
}
}
public void deleteFL(String word) {
String tempWord = word.substring(1,
word.length() - 1); // stores temp
// made up word
checkDict(tempWord);
}
public void deleteLL(String word) {
String tempWord = word.substring(0,
word.length() - 1); // stores temp
// made up word
checkDict(tempWord);
}
public void pluralize(String word) {
String tempWord = word + "s";
checkDict(tempWord);
}
public void checkDict(String word) {
if (dictionary.contains(word))
{
if
(!possibilities.contains(word))
possibilities.add(word);
}
}
public void transposition(String word) {
wrongWord = word;
int wordLen = word.length();
String[] mixer = new
String[wordLen];
for (int i = 0; i < wordLen;
i++) {
mixer[i] =
word.substring(i, i + 1);
}
shift(mixer);
}
public void shift(String[] mixer) {
System.out.println();
String wordValue = "";
for (int i = 0; i <=
tempHolder.size(); i++) {
resetHelper(tempHolder);
transposeHelper(mixer);
String
wordFirstValue = tempHolder.remove(i);
for (int j = 0;
j < tempHolder.size(); j++) {
int inttemp = 0;
String temp;
while (inttemp < j) {
temp =
tempHolder.remove(inttemp);
tempHolder.add(temp);
wordValue += wordFirstValue +
printWord(tempHolder);
inttemp++;
if
(dictionary.contains(wordValue))
if
(!possibilities.contains(wordValue))
possibilities.add(wordValue);
wordValue = "";
} // end of while
} // end of
for
} // end for
}// end of shift
public void transposeHelper(String[] wordMix) {
for (int i = 0; i <
wordMix.length; i++) {
tempHolder.add(wordMix[i]);
}
}
public void resetHelper(List<String> thisList)
{
while (!thisList.isEmpty())
thisList.remove(0);
}// end of resetHelper
public void print(List<String> listPrint)
{
if (possibilities.isEmpty())
{
System.out.print("Can't seem to find any related words for " +
wrongWord);
return;
}
System.out.println("Maybe you meant
these for " + wrongWord + ": ");
System.out.printf("%s",
listPrint);
resetHelper(possibilities);
}// end of print
public String printWord(List<String> listPrint)
{
Object[] suggests =
listPrint.toArray();
String theWord = "";
for (Object word : suggests) {//
form listPrint elements into a word
theWord +=
word;
}
return theWord;
}// end of printWord
}
Program 3:
import java.util.ArrayList;
import java.util.List;
public class WordFinder extends Dictionary {
private int wordsLength;
private List<String> wrongWords = new
ArrayList<String>();
public void initialCheck(String[] words) {
wordsLength = words.length;
System.out.println();
for (int i = 0; i < wordsLength;
i++) {
if
(!dictionary.contains(words[i]))
wrongWords.add(words[i]);
}
if (!wrongWords.isEmpty()) {
System.out.println("Mistakes have been made!");
printIncorrect();
}
}
public void printIncorrect() {
System.out.print("These words [
");
for (String wrongWord : wrongWords)
{
System.out.print(wrongWord + " ");
}
System.out.println("]seems
incorrect.\n");
suggest();
}
public void suggest() {
MisSpell test = new
MisSpell();
while (!wrongWords.isEmpty()
&& test.possibilities.size() <= 5) {
String wordCheck
= wrongWords.remove(0);
test.generateMispellings(wordCheck);
if
(test.possibilities.size() >= 0)
test.print(test.possibilities);
}
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.