1

スペルチェックプロジェクトを行っています。単語リストがあり、ゲティスバーグの住所にいくつかの単語のつづりが間違っています。私の仕事は、スペルミスのある単語を特定し、アドレスを印刷するときにスペルミスのある単語の下にアスタリスクなどを印刷することです。私の問題は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
}

PS。私はそれが完全ではなく、多くのルーズエンドがあり、まだ進行中であることを知っています。

編集:

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

3 に答える 3

2

必要なのはこれだけです。

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

さらに、あなたの絶対値を調べることにより、indexあなたのタイプミスした単語にアルファベット順に近い単語を辞書で簡単に見つけることができます。

于 2013-02-13T22:45:12.663 に答える
2

リストが一般的であるため、javadoc は中国語のように見えます。

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

T を任意のジェネリック型として読み取る必要があります。T はキーの型です。最初のパラメーターであるリストは、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 になることが保証されることに注意してください。

于 2013-02-13T22:56:37.640 に答える
1

ループを作成して単語をファイルから変数に配置し、それが辞書にあるかどうかをテストします

しかし、それはあなたがしていることではありません。辞書内のすべての単語を調べるループを作成し、住所の次の単語が辞書にあるかどうかを確認し、見つかったかどうかに関係なく何もしません。

辞書に住所よりも多くの単語が含まれている場合、おそらく例外が発生します。住所に複数の単語が含まれている場合、それらすべてをチェックするわけではありません。

于 2013-02-13T22:49:26.057 に答える