Java Spell Checker hashing program We will create a spell ch

Java Spell Checker: hashing program

We will create a spell checker program. This program will use  a text file with a list of words as a dictionary to look up properly spelled words. When the program starts, it will open the dictionary file (name it \"Words.txt\"). You will utilize a hash function you have created to hash the words into an externally-chained hash table you have created. The hash table will be of a size that you will determine. You must submit a document that describes how you determined the tablesize/load factor. You will report the following statistics:

maximum chain length

average chain length

percentage of buckets to which one or more words hashed

Your program will then open an input file named \"inputTextFile.txt.\" When the checking proceeds, you will extract a word from the file to be checked, hash it into the table, and determine if it exists. You will continue this process until you have checked all the words in the file. Each time you find a word that you cannot match in the dictionary, you will let the user know and you will attempt to generate a list of suggested words. You will generate the list by assembling similar words via four methods:

One letter missing. You assume that one letter has been left out of the word. You can assemble new words to check by adding letters a..z in each of the positions in the word from the start to the end of the word.

One letter added. You assume the word has an extra letter. You scan through the word deleting each of the letters in turn, and looking up the word formed by the remaining letters.

Two letters reversed. You swap letters in positions 0..1, 1..2, 2..3, ... , n-2..n-1, to form new words which you look up.

One letter changed. You try each letter at each position in the word to see if one letter was simply mis-typed.

Each time you find a legal word from any of the four methods, you add it to the suggestion ArrayList, which you present to the user when you have finished this part of the process. If you cannot identify any suggestions, let the user know.

Example file:

Hello. My plan is to hav a test fiile that has UPPer and LOWER case words. All fuor cises of misspellings will be represented. The file will encompass more than one line and will have no other puncktuation than commas, and the dot at the end of a line.

Solution

import java.io.*;
import java.util.*;

public class spellchecker {
  
Hashtable<String,String> spelluserWordDictionary; // it stores all userWords
boolean userWordSuggest ; // tells userWord spelled correct or not
  
  
public spellchecker()
{
spelluserWordDictionary = new Hashtable<String,String>();
System.out.println(\"Spell checker simulation\");
  
try
{
  
//reading userWords from teit file
BufferedReader fileReader = new BufferedReader(new FileReader(\"userWordsionaryuserWords.tit\"));
  
while (fileReader.ready())
{
String userWordInput = fileReader.readLine() ;
String [] userWords = userWordInput.split(\"\\\\s\"); // reading userWords
  
for(int i = 0; i < userWords.length;i++)
{
// storing into dictionary
spelluserWordDictionary.put(userWords[i], userWords[i]);
}
}
fileReader.close();
  
String file = \"useruserWords.tit\";
// reading user input file
BufferedReader inputFile = new BufferedReader(new FileReader(file));
System.out.println(\"Reading userWords from \"+file);
  
// Initialising a spelling suggest object
spellingsuggest suggest = new spellingsuggest(\"userWordsProbDatabase.tit\");

// reading input file line by line
while ( inputFile.ready() )
{
String s = inputFile.readLine() ;
System.out.println (s);
String[] userWordResults = s.split(\"\\\\s\");
  
for (int i=0; i<userWordResults.length; i++)
{
userWordSuggest = true;
String outputuserWord = checkSuggestuserWord(userWordResults[i]);
  
if(userWordSuggest)
{
System.out.println(\"Suggestions: \"+userWordResults[i]+\" ==> \"+suggest.correct(outputuserWord));
}
}
}
inputFile.close();
}
catch (IOEiception e)
{
System.out.println(\"IOException Occured! \");
e.printStackTrace();
}
}
  
public String checkSuggestuserWord(String userWordToCheck)
{
String checkuserWord, unpunctuationuserWord;
String userWord = userWordToCheck.toLowerCase();
  
// check userWord available in dictionary
if ((checkuserWord = (String)spelluserWordDictionary.get(userWord)) != null)
{
userWordSuggest = false; // if correct userWord, suggestion false
return checkuserWord;
}
int length = userWord.length();
  
//Checking quotes
if (length > 1 && userWord.substring(0,1).equals(\"\\\"\"))
{
unpunctuationuserWord = userWord.substring(1, length);
  
if ((checkuserWord = (String)spelluserWordDictionary.get(unpunctuationuserWord)) != null)
{
userWordSuggest = false; // no suggestion
return checkuserWord ;
}
else // not found
return unpunctuationuserWord;g
}

if( userWord.substring(length - 1).equals(\".\") || userWord.substring(length - 1).equals(\",\") || userWord.substring(length - 1).equals(\"!\")
|| userWord.substring(length - 1).equals(\";\") || userWord.substring(length - 1).equals(\":\"))
{
unpunctuationuserWord = userWord.substring(0, length-1);
  
if ((checkuserWord = (String)spelluserWordDictionary.get(unpunctuationuserWord)) != null)
{
userWordSuggest = false;// if correct word, no suggestion
return checkuserWord ;
}
else
{
return unpunctuationuserWord;
}
}

if (length > 2 && userWord.substring(length-2).equals(\",\\\"\") || userWord.substring(length-2).equals(\".\\\"\")
|| userWord.substring(length-2).equals(\"?\\\"\") || userWord.substring(length-2).equals(\"!\\\"\") )
{
unpunctuationuserWord = userWord.substring(0, length-2);
  
if ((checkuserWord = (String)spelluserWordDictionary.get(unpunctuationuserWord)) != null)
{
userWordSuggest = false; if correct word, no suggestion
return checkuserWord ;
}
else
return unpunctuationuserWord; // return final word
}

return userWord;
}
  
   public static void main(String [] args)
{
spellchecker checkerObj = new spellchecker();
}
}

Java Spell Checker: hashing program We will create a spell checker program. This program will use a text file with a list of words as a dictionary to look up pr
Java Spell Checker: hashing program We will create a spell checker program. This program will use a text file with a list of words as a dictionary to look up pr
Java Spell Checker: hashing program We will create a spell checker program. This program will use a text file with a list of words as a dictionary to look up pr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site