我正在为一个学校做一个项目,我必须测试拼写检查实现的二进制搜索与线性搜索的速度。我们必须使用此特定的SpellCheck。我的教授为使用递归进行binary
搜索而给出的示例是为int设计的一些单独的代码片段,他希望我们使它适用于使用字符串的此实现。任何帮助,我们将不胜感激。 SortedWordList
是我需要帮助的课程。
import java.util.Scanner;
public class Spellcheck {
private static WordList listOfWords;// The list of correctly spelled words.
public static void main(String[] args) {
long start = System.nanoTime();
listOfWords = new SortedWordList();
//listOfWords = new WordList();
long end = System.nanoTime();
long WL = (end-start);
//end = 0;
//start = 0;
long SW = 0;
Scanner in = new Scanner(System.in);
while (true) { // Get and process one word from the user.
System.out.println();
System.out.print("Enter a word to be cheched (press return to end): ");
String word = in.nextLine().trim().toLowerCase();
start = System.nanoTime();
if (word.length() == 0)
break;
if (listOfWords.contains(word)) {
System.out.println("'" + word + "' is a legal word.");
}
else {
System.out.println("'" + word + "' is not a legal word.");
System.out.println("If there are similar words, they are shown here:");
trySubstitute(word);
tryInsert(word);
tryDelete(word);
trySwap(word);
tryBreak(word);
end = System.nanoTime();
SW = (end-start);
}
System.out.println();
System.out.printf("Time to create word list: %,d nanoseconds.%n",(WL));
System.out.println();
System.out.printf("Time to test for similar words: %,d nanoseconds.%n",(SW));
System.out.println();
System.out.printf("Time to test total: %,d nanoseconds.%n",(SW + WL));
}
}
private static void trySubstitute(String word) {
for (int i = 0; i < word.length(); i++) {
String before = word.substring(0, i);
String after = word.substring(i+1);
for (char ch = 'a'; ch < 'z'; ch++) {
String newword = before + ch + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
}
private static void tryInsert(String word) {
for (int i = 0; i <= word.length(); i++) {
String before = word.substring(0,i);
String after = word.substring(i);
for (char ch = 'a'; ch < 'z'; ch++) {
String newword = before + ch + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
}
private static void tryDelete(String word) {
for (int i = 0; i < word.length(); i++) {
String before = word.substring(0, i);
String after = word.substring(i+1);
String newword = before + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
private static void trySwap(String word) {
for (int i = 0; i < word.length() - 1; i++) {
String before = word.substring(0, i);
char a = word.charAt(i);
char b = word.charAt(i+1);
String after = word.substring(i+2);
String newword = before + b + a + after;
if (listOfWords.contains(newword))
System.out.println(" " + newword);
}
}
private static void tryBreak(String word) {
for (int i = 1; i < word.length() - 1; i++) {
String before = word.substring(0, i);
String after = word.substring(i);
if (listOfWords.contains(before) && listOfWords.contains(after))
System.out.println(" " + before + " " + after);
}
}
} // end class Spellcheck
`
import java.util.ArrayList;
import java.util.Scanner;
import java.net.URL;
public class WordList {
protected final String[] words; // the list of words
public WordList() {
try {
URL listLocation = WordList.class.getClassLoader().getResource("unsorted_words.txt");
Scanner in = new Scanner( listLocation.openStream() );
ArrayList<String> wordList = new ArrayList<String>();
while (in.hasNextLine())
wordList.add(in.nextLine());
words = wordList.toArray(new String[] {});
in.close();
}
catch (Exception e) {
throw new IllegalStateException("Can't load list of words from file 'unsorted_words.txt'");
}
}
public boolean contains(String lowerCaseWord) {
for (String s : words) {
if (s.equals(lowerCaseWord))
return true;
}
return false;
}
public int size() {
return words.length;
}
public String get(int index) {
return words[index];
}
}
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class SortedWordList extends WordList {
public String [] words;
public SortedWordList() {
URL listLocation = SortedWordList.class.getClassLoader().getResource("unsorted_words.txt");
Scanner in = null;
try {
in = new Scanner( listLocation.openStream() );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<String> wordList = new ArrayList<String>();
while (in.hasNextLine())
wordList.add(in.nextLine());
words = wordList.toArray(new String[] {});
in.close();
}
//public boolean contains(String lowerCaseWord) {
//}
static int binarySearch(String[] A, int loIndex, int hiIndex, String value) {
if (loIndex > hiIndex) {
return -1;
}
else {
int middle = (loIndex + hiIndex) / 2;
if (value == A[middle])
return middle;
else if ((A[middle].compareTo(value)) > 0)
return binarySearch(A, loIndex, middle - 1, value);
else // value must be > A[middle]
return binarySearch(A, middle + 1, hiIndex, value);
}
} // end binarySearch()
static int quicksortStep(String[] A, int lo, int hi) {
String pivot = A[lo]; // Get the pivot value.
while (hi > lo) {
while (hi > lo && ((pivot.compareTo(A[hi])) <= 0)) {
hi--;
}
if (hi == lo)
break;
A[lo] = A[hi];
lo++;
while (hi > lo && ((pivot.compareTo(A[lo])) >= 0)) {
lo++;
}
if (hi == lo)
break;
A[hi] = A[lo];
hi--;
} // end while
A[lo] = pivot;
return lo;
} // end QuicksortStep
static void quicksort(String[] A, int lo, int hi) {
if (hi <= lo) {
return;
}
else {
int pivotPosition = quicksortStep(A, lo, hi);
quicksort(A, lo, pivotPosition - 1);
quicksort(A, pivotPosition + 1, hi);
}
}
}
答案 0 :(得分:0)
首先,您需要在哪里打电话给quicksort?只是通过类的SortedWordList名称进行猜测,在我看来,您需要在SortedWordList的构造函数中调用此quicksort方法。现在,构造函数似乎正在加载未排序的单词列表。
如果那不能解决问题,那么我还要再次查看SortedWordList类中的quicksortStep方法。这个quicksortStep方法基本上是分区方法。 (有关示例,请参见https://www.programcreek.com/2012/11/quicksort-array-in-java/)。我会尝试在quicksortStep方法中遵循与上面链接中的分区方法相同的代码结构,然后将其修改为字符串,直到类似:
public static int partition(String[] arr, int start, int end){
String pivot = arr[end];
for(int i=start; i<end; i++){
if((arr[i].compareTo(pivot)) < 0){
String temp= arr[start];
arr[start]=arr[i];
arr[i]=temp;
start++;
}
}
String temp = arr[start];
arr[start] = pivot;
arr[end] = temp;
return start;
}
可以随意重命名方法/变量。