简体   繁体   中英

Java - binarySearch(). How do I set up a binarySearch for a Spell Check

I am doing a Spell Check project. I have a words list and then the Gettysburg address with some words misspelled. my job is to identify which words are misspelled and then print out astericks or something under the misspelled word when i print out the Address. my issue is in the binarySearch part. Im not sure of the syntax and the javadoc looks like its in chinese. here is my souce code (binarySearch is towards the bottom)

/*
 * Assignment 1: Spell Check
 * Professor Subrina Thompson
 * CS102
 */
package spellcheck;

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

public class SpellCheck {

    //48,219 words in the words.txt

    //Declare Variables
    static FileReader reader;
    static Scanner input;
    static ArrayList <String> wordList = new ArrayList<String>();
    static FileReader reader2;
    static Scanner input2;
    static String testWord;
    static String index;

    //Main Method
    public static void main(String[] args) throws FileNotFoundException {
        fileSort();
    }

    //Open file to be read from
    public static void openFile() throws FileNotFoundException {
        reader = new FileReader("words.txt");
        input = new Scanner(reader);

    }

    //sort the file
    public static void fileSort() throws FileNotFoundException{
        openFile();

        //read the word list into an ArrayList
        while (input.hasNext()){
            wordList.add(input.next());
        }

        //Sort the array
        Collections.sort(wordList);
    }

    //read the gettysburg address
    public static void gAddress()throws FileNotFoundException{
        reader2 = new FileReader("gettysburg.txt");
        input2 = new Scanner(reader2);

        //create loop to place word from file into a var then test to see if it is in the dictionary
        for(int i = 0; i < wordList.size(); i++){

            //place the word into a variable
            testWord = input2.next();

            //test if the word is in the dictionary
            index = Collections.binarySearch(wordList,testWord);
        }
    }

    //compare the address and array through binary search 

    //print out if spelling is correct
}

PS. I know its not complete and with a lot of loose ends, its still a work-in-progress.

EDIT:

i tried making a new search function based off how i understand binarySearch works. this is the code for that function. the "string w" would be the dictionary word to test against the testWord from the Address:

public static int binarySearch(String w){

        int start = 0;
        int stop  = wordList.size() - 1;

        while (start != stop){
            int half = ((stop - start)/2) + start;
            int res = wordList.get(half).compareToIgnoreCase(w);

            if( res == 0 ){
                return half;
            }
        else if( stop - start <= 1 ){
                return -1;
            }
        else if( res > 0 ){
                start = half;
            }
        else if( res < 0 ){
                stop  = half;
            }


   }

   return -1;
}

This is all you need:

if(index < 0) {
  System.out.println(testWord + " not in dictionary");
}

In addition by examining the absolute value of index you can easily find words in dictionary that were alphabetically close to your mistyped word.

The javadoc looks like chinese cause the lists are Generic.

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)

Should be read with T as any generic type, T is the type of the key. The first parameter, the list, must be a list of a type that implements the Comparable interface for a type that T derives from.

In your case, T, the key type, is String. And it's a list of Strings. String implements Comparable, and String is a super class of String. So that is valid.

If you fill in String, the method signature turns into something more normal:

public static int binarySearch(List<String> list, String key)

So therefore, given

int index;
List<String> list;
String key;

an invocation looks like

index = Collections.binarySearch(list, key);

after which index will contain the index of the search key in the list, or a negative number if the key was not found. More precisely:

index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1) . The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

create loop to place word from file into a var then test to see if it is in the dictionary

But that is not what you are doing. You are creating a loop to go through all the words in the dictionary and checking if the next word in the address is in dictionary and doing nothing about it, whether it is found or not.

If the dictionary has more words then address you will probably get exception, if the address has more words then you will not check all of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM