Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for SpellChecker in Java (some bugs may occur)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package spellcheck;

import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import spellcheck.AbstractMap;

/**
 *
 * @author obobotette0 Section of the source code were adapted from
 * http://codereview.stackexchange.com/questions/48908/java-implementation-of-spell-checking-algorithm
 * @SuppressWarnings("unchecked");
 */

public class SpellCheck {
 
    //variable for hash table
    AbstractMap alphabet = new AbstractMap<>(0, '0');
    AbstractMap words = new AbstractMap<>(0, "string");
 
    //upper and lower case character arrays
    final char[] lower = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    final char[] upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    int list = 0;

    SpellCheck() throws FileNotFoundException {
        //keys are assigned arbitrary value for all alphanumeric set
        alphabet.input(1, '0');
        alphabet.input(2, '1');
        alphabet.input(3, '2');
        alphabet.input(4, '3');
        alphabet.input(5, '4');
        alphabet.input(6, '5');
        alphabet.input(7, '6');
        alphabet.input(8, '7');
        alphabet.input(9, '8');
        alphabet.input(10, '9');
        alphabet.input(11, 'a');
        alphabet.input(12, 'b');
        alphabet.input(13, 'c');
        alphabet.input(14, 'd');
        alphabet.input(15, 'e');
        alphabet.input(16, 'f');
        alphabet.input(17, 'g');
        alphabet.input(18, 'h');
        alphabet.input(19, 'i');
        alphabet.input(20, 'j');
        alphabet.input(21, 'k');
        alphabet.input(22, 'l');
        alphabet.input(23, 'm');
        alphabet.input(24, 'n');
        alphabet.input(25, 'o');
        alphabet.input(26, 'p');
        alphabet.input(27, 'q');
        alphabet.input(28, 'r');
        alphabet.input(29, 's');
        alphabet.input(30, 't');
        alphabet.input(31, 'u');
        alphabet.input(32, 'v');
        alphabet.input(33, 'w');
        alphabet.input(34, 'x');
        alphabet.input(35, 'y');
        alphabet.input(36, 'z');
        alphabet.input(37, 'A');
        alphabet.input(38, 'B');
        alphabet.input(39, 'C');
        alphabet.input(40, 'D');
        alphabet.input(41, 'E');
        alphabet.input(42, 'F');
        alphabet.input(43, 'G');
        alphabet.input(44, 'H');
        alphabet.input(45, 'I');
        alphabet.input(46, 'J');
        alphabet.input(47, 'K');
        alphabet.input(48, 'L');
        alphabet.input(49, 'M');
        alphabet.input(50, 'N');
        alphabet.input(51, 'O');
        alphabet.input(52, 'P');
        alphabet.input(53, 'Q');
        alphabet.input(54, 'R');
        alphabet.input(55, 'S');
        alphabet.input(56, 'T');
        alphabet.input(57, 'U');
        alphabet.input(58, 'V');
        alphabet.input(59, 'W');
        alphabet.input(60, 'X');
        alphabet.input(61, 'Y');
        alphabet.input(62, 'Z');
     
        File ininput = new File("lexicon.txt");
        try (Scanner load = new Scanner(ininput)) {
            while (load.hasNextLine()) {
                words.input(list, load.nextLine());
                list++;
            }
        }

    }

    //hash function adds all values of the word
    int hashvalue(String word) {
        int val = 0;
        char[] array = word.toCharArray();
        for (int i = 0; i < array.length; i++) {
            for (int j = 1; j <= 62; j++) {
                if (alphabet.findValue(array[i])) {
                    val += alphabet.findIndex(j);
                }
            }
        }

        return val;
    }

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     *
     */
    public static void main(String[] args) throws FileNotFoundException {
        // TODO code application logic here

        //Variables
        SpellCheck checker = new SpellCheck();
        int first;
        Scanner read = new Scanner(System.in);
        //Prompt for user entry
        System.out.println("Please type a sentence");
        String gui = JOptionPane.showInputDialog("Please type a sentence");
        //Seperate sentence into words
        //String[] temp = read.nextLine().split(" ");
        String[] temp = gui.split(" ");
        boolean[] found = new boolean[temp.length];
        boolean print = false;

        //Loop to process each word of entered string
        for (int i = 0; i < temp.length; i++) {
            temp[i] = temp[i].replace(".", "");
            temp[i] = temp[i].replace("!", "");
            temp[i] = temp[i].replace("?", "");
            temp[i] = temp[i].replace(",", "");

            found[i] = false;
            first = checker.hashvalue(temp[i]);
            System.out.println(temp[i] + " has a hash value of " + first);
            for (int j = 0; j < checker.list; j++) {
                if (checker.words.findValue(temp[i])) {
                    System.out.println(temp[i] + " was found in our lexicon.");
                    found[i] = true;
                    break;
                }
            }
            //If words was not found
            if (found[i] == false) {
                System.out.println(temp[i] + " could not be found in our lexicon.");
                StringBuilder suggestion = new StringBuilder();

                //Add character to the first and last nodes
                //and check to see if it is in the dictionary
                suggestion.append(checker.addChar(temp[i], checker));
                if (suggestion.length() > 0) {
                    System.out.println("Did you mean to type " + suggestion.toString() + "?");
                    suggestion.delete(0, suggestion.length());
                    print = true;
                }
             
                //Add character at each index from the first and to the last nodes
                //and check to see if it is in the dictionary
                suggestion.append(checker.add2Char(temp[i], checker));
                if (suggestion.length() > 0) {
                    System.out.println("Did you mean to type " + suggestion.toString() + "?");
                    suggestion.delete(0, suggestion.length());
                    print = true;
                }
             

                //Swap character with the next index
                //and check if it is in the dictionary
                suggestion.append(checker.swapChar(temp[i], checker));
                if (suggestion.length() > 0) {
                    System.out.println("Did you mean to type " + suggestion.toString() + "?");
                    suggestion.delete(0, suggestion.length());
                    print = true;
                }

                //Remove character at each index
                //and check to see if it is in the dictionary
                suggestion.append(checker.extraChar(temp[i], checker));
                if (suggestion.length() > 0) {
                    System.out.println("Did you mean to type " + suggestion.toString() + "?");
                    suggestion.delete(0, suggestion.length());
                    print = true;
                }
             
                //Remove character at each index and insert a new character
                //and check to see if it is in the dictionary
                suggestion.append(checker.replaceChar(temp[i], checker));
                if (suggestion.length() > 0) {
                    System.out.println("Did you mean to type " + suggestion.toString() + "?");
                    suggestion.delete(0, suggestion.length());
                    print = true;
                }

                //If no suggestions were printed
                if (suggestion.length() == 0 && print == false) {
                    System.out.println("No substitution for " + temp[i] + " could not be found in our lexicon.");
                 
                }

            }

        }
    }

    //Word suggestion modules are based on example from
    //http://codereview.stackexchange.com/questions/48908/java-implementation-of-spell-checking-algorithm
    //Checks if error is the first or last characters
    String addChar(String word, SpellCheck add) throws FileNotFoundException {
        StringBuilder temp = new StringBuilder();
        String result = new String();
        //For each letter of the alphabet
     
        for (int i = 0; i < add.lower.length; i++) {
            //Insert at the beginning
            temp = temp.insert(0, add.lower[i] + word);
            //System.out.println(temp);
         
                //See if it can be found
                if (add.words.findValue(temp.toString())) {
                    result = result.concat(temp.toString());
                    //System.out.println(result);
                    break;
                }
         
            //Delete modification
            temp = temp.delete(0, temp.length());
            //insert letter at the end
            temp = temp.insert(0, word + add.lower[i]);
            //System.out.println(temp);
         
                //See if it can be found
                if (add.words.findValue(temp.toString())) {
                    result = result.concat(temp.toString());
                    //System.out.println(result);
                    break;
                }
         
            temp = temp.delete(0, temp.length());

        }
        return result;
    }

    String add2Char(String word, SpellCheck add) throws FileNotFoundException {
        StringBuilder temp = new StringBuilder();
        String result = new String();
        //try inserting each chars at an array location 1 and n - 1
        //checking if it is in the dictionary
        for (int i = 1; i < word.length()-1; i++) {
            for (int j = 0; j < add.lower.length; j++) {
                temp = temp.append(word);
                temp = temp.insert(i, add.lower[j]);
                //System.out.println(temp);
                if (add.words.findValue(temp.toString())) {
                 
                    result = result.concat(temp.toString());
                    //System.out.println(result);
                    break;
                }

                temp = temp.delete(0, temp.length());
            }
        }
        return result;
    }

    String swapChar(String word, SpellCheck add) throws FileNotFoundException {
        StringBuilder temp = new StringBuilder();
        //temp = temp.append(word);
        String result = new String();
        char[] tempChar;
        char swap;
        //for length of word sawp characters next to each other
        //and see if it can be found
        for (int i = 0; i < word.length() - 1; i++) {
            tempChar = word.toCharArray();
            swap = tempChar[i];
            tempChar[i] = tempChar[i + 1];
            tempChar[i + 1] = swap;
            temp = temp.append(tempChar);
             if (add.words.findValue(temp.toString())) {
                result = result.concat(temp.toString());
                //System.out.println(result);
                break;
            }
         
        }

        return result;
    }

    String extraChar(String word, SpellCheck add) throws FileNotFoundException {
        StringBuilder temp = new StringBuilder();
        String result = new String();
        //delete character at each array location
        //and see if it can be found
        for (int i = 0; i < word.length(); i++) {
            temp = temp.insert(0, word);
            temp = temp.deleteCharAt(i);
            //System.out.println(temp);
            if (add.words.findValue(temp.toString())) {
                result = result.concat(temp.toString());
                //System.out.println(result);
                break;
            }

            temp = temp.delete(0, temp.length());
         
         
        }
        return result;
    }
 
    String replaceChar(String word, SpellCheck add) throws FileNotFoundException {
        StringBuilder temp = new StringBuilder();
        String result = new String();
        for (int i = 0; i < word.length(); i++) {
            //check to see if removing and replacing can produce a result
            for (int j = 0; j < add.lower.length; j++) {
                temp = temp.insert(0, word);
                temp = temp.deleteCharAt(i);
                temp = temp.insert(i, add.lower[j]);

             
                if (add.words.findValue(temp.toString())) {
                    result = result.concat(temp.toString());
                    //System.out.println(result);
                    break;
                }
                temp = temp.delete(0, temp.length());
            }
         
        }
     
        return result;
    }

}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!