【问题标题】:Java - binarySearch(). How do I set up a binarySearch for a Spell CheckJava - 二进制搜索()。如何为拼写检查设置 binarySearch
【发布时间】:2013-02-15 04:15:18
【问题描述】:

我正在做一个拼写检查项目。我有一个单词列表,然后是葛底斯堡地址,其中一些单词拼写错误。我的工作是识别哪些单词拼写错误,然后在我打印出地址时打印出拼写错误的单词下方的星号或其他内容。我的问题在 binarySearch 部分。我不确定语法,javadoc 看起来像中文。这是我的源代码(binarySearch 在底部)

/*
 * 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
}

附言。我知道它不完整并且有很多松散的结局,它仍在进行中。

编辑:

我尝试根据我对 binarySearch 工作原理的理解创建一个新的搜索功能。这是该功能的代码。 “字符串 w” 将是字典中的单词,用于测试来自地址的 testWord:

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;
}

【问题讨论】:

    标签: java binary-search


    【解决方案1】:

    这就是你所需要的:

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

    此外,通过检查index 的绝对值,您可以轻松地在字典中找到按字母顺序与您输入错误的单词相近的单词。

    【讨论】:

    • 他仍然会遇到我在回答中概述的问题
    【解决方案2】:

    javadoc 看起来像中文,因为列表是通用的。

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

    应该将 T 视为任何泛型类型,T 是键的类型。 第一个参数 list 必须是一个类型的列表,该类型实现了 T 派生自的类型的 Comparable 接口。

    在您的情况下,键类型 T 是字符串。它是一个字符串列表。 String 实现了 Comparable,String 是 String 的超类。所以这是有效的。

    如果你填写String,方法签名会变成更正常的东西:

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

    因此,给定

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

    调用看起来像

    index = Collections.binarySearch(list, key);
    

    之后index 将包含列表中搜索键的索引,如果未找到该键,则为负数。更准确地说:

    搜索键的索引,如果它包含在列表中;否则, (-(插入点) - 1)。插入点定义为点 键将被插入到列表中的位置: 第一个大于键的元素,或者 list.size(),如果所有元素都在 该列表小于指定的键。请注意,这保证 当且仅当找到键时,返回值才会 >= 0。

    【讨论】:

    • 感谢您的信息,但我仍然有点困惑(java n00b 在这里大声笑,请多多包涵)。在最后一段代码中,为什么返回类型是“int”?以及如何使用文件代替“字符串键”?
    • Collections.binarySearch 不会在列表中搜索文件,只会搜索一个键(其中许多您可以从文件中读取并在列表中一一查找)。我已经用特定的调用更新了答案。返回类型是 int 因为它(有点隐蔽)这样说: public static int binarySearch(yadayada)
    【解决方案3】:

    创建循环将文件中的单词放入 var 然后测试它是否在字典中

    但这不是你正在做的。您正在创建一个循环来遍历字典中的所有单词,并检查地址中的下一个单词是否在字典中,并且不管它是否找到。

    如果字典有更多的单词然后地址你可能会得到异常,如果地址有更多的单词那么你不会检查所有的。

    【讨论】:

    • 实际上代码不起作用。我遇到了 binarySearch 行的问题。也许是因为“索引”是一个字符串,应该是不同的类型?我不确定是什么。我以前从未使用过 binarySearch,所以我不熟悉它究竟会返回什么
    • 如果indexString 你的程序甚至无法编译。它编译了吗?
    • err,编译错误和运行时错误是有区别的。你面对的是哪一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 2016-02-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多