Java 拼写检查程序缺少单词

Java 拼写检查程序缺少单词,java,recursion,binary-search-tree,spell-checking,Java,Recursion,Binary Search Tree,Spell Checking,我有一个包含单词列表的字典文件,我将该文件读入数组列表sArray。然后我有一本书,我使用字符串解析器从这本书中提取每个字符串,并将其发送到二进制搜索方法,bSearchb搜索将使用递归二进制搜索来确定是否在包含字典的数组sArray中找到键。如果找不到该单词,它将打印出该单词可能拼写错误 我的问题是,我的字典数组中有我知道的单词的输出。我已经验证了单词是否被正确阅读,因此问题归结为使用b搜索浏览sArray。我不确定代码出了什么问题。下面列出了一些误报的例子 这里有一个指向我的字典的粘贴转储的

我有一个包含单词列表的字典文件,我将该文件读入数组列表
sArray
。然后我有一本书,我使用字符串解析器从这本书中提取每个字符串,并将其发送到二进制搜索方法,
bSearch
b搜索
将使用递归二进制搜索来确定是否在包含字典的数组
sArray
中找到键。如果找不到该单词,它将打印出该单词可能拼写错误

我的问题是,我的字典数组中有我知道的单词的输出。我已经验证了单词是否被正确阅读,因此问题归结为使用
b搜索
浏览
sArray
。我不确定代码出了什么问题。下面列出了一些误报的例子

这里有一个指向我的字典的粘贴转储的链接;你应该能够在下面搜索这些单词并找到它们

样本输出:

乌木可能被错卖了
褐辉石可能是错误销售的
霍尔姆冈可能被错卖了
squushy可能是错误的

编辑 我尝试使用

Collections.sort(sArray, String.CASE_INSENSITIVE_ORDER);
结果输出仍然是所有误报

ebracteate可能是卖错了

Phaca可能是错误销售的

霍姆贝里可能是卖错了

sraddha可能是错误销售的

公共类程序2{
私人int mid;
公共计划2(){
mid=0;
}
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
文件inf=新文件(“dictorial.txt”);
ArrayList sArray=新的ArrayList();
Program2 a=新的Program2();
a、 读写(sArray);
集合。排序(sArray);
int correct=0;
int incorrectRec=0;
int-correctW=0;
int不正确w=0;
FileInputStream infO=newfileinputstream(新文件(“oliver.txt”);
煤焦;
字符串str=“”;
int n=0;
而((n=infO.read())!=-1){
let=(char)n;
if(字符集(let)){
str+=Character.toLowerCase(let);
}
if((Character.isWhitespace(let)| | let='-')&&!str.isEmpty()){
//在此处编写代码将str插入到树中
if(a.b搜索(sArray,str,0,sArray.size())>=0){
correcrec++;
}否则{
不正确的C++;
}
str=“”;
}
}
infO.close();
a、 打印(更正、不更正);
}
公共作废打印(内部更正、内部不更正){
System.out.println(“字数合计”+(不正确的字数+正确的字数));
System.out.println(“正确”+正确单词);
System.out.println(“不正确”+不正确的单词);
System.out.println(“递归步骤的总数为”+(correcrec+incorrectRec));
System.out.println(“找到的单词的平均比较次数=“+correcrec/correctWords”);
System.out.println(“未找到单词的平均比较次数=”+不正确/不正确单词);
}
公共无效读写(ArrayList sArray){
试一试{
文件f=新文件(“dictionary.txt”);
扫描仪inf=新扫描仪(f);
while(inf.hasNext()){
sArray.add(inf.nextLine());
}
}捕获(FileNotFoundException ex){
System.out.println(“未找到录音文件”);
}
}
公共int-b搜索(ArrayList-sArray、字符串键、int-lowIndex、int-highIndex){
如果(低索引>高索引){
System.out.println(sArray.get(mid)+“可能是错误的”);
不正确的单词++;
返回记录*-1;
}
中=(低指数+高指数)/2;
if(sArray.get(mid).compareTognoreCase(键)==0){
正确的单词++;
返回记录;
}else if(sArray.get(mid.compareTignoreCase(key)>0){
rec++;
返回b搜索(sArray,key,lowIndex,mid-1);
}否则{
rec++;
返回b搜索(sArray,key,mid+1,highIndex);
}
}
}

要搜索的最高索引是
高索引
还是它之后的索引(不搜索的最低索引)?您确定您的词典中正好有235886个单词吗?您的文件是否包含大小写混合的单词?单词“mispelled”拼错了。highIndex是搜索的最高索引是的,我将它改为sArray.size()以避免混淆。该文件确实包含大小写混合的单词,但我认为我使用compareTignoreCaseTry
Collections.sort(sArray,String.case\u INSENSITIVE\u ORDER)
解决了这个问题。
public class Program2 {

private int mid;

public Program2() {
    mid = 0;
}

public static void main(String[] args) throws FileNotFoundException, IOException {
    File inf = new File("dictonary.txt");
    ArrayList<String> sArray = new ArrayList<>();
    Program2 a = new Program2();
    a.readDictonary(sArray);
  
    Collections.sort(sArray);
   
    int correctRec = 0;
    int incorrectRec = 0;
    int correctW = 0;
    int incorrectW = 0;
   
    FileInputStream infO = new FileInputStream(new File("oliver.txt"));
    char let;
    String str = "";
    int n = 0;
    while ((n = infO.read()) != -1) {
        let = (char) n;

        if (Character.isLetter(let)) {
            str += Character.toLowerCase(let);
        }

        if ((Character.isWhitespace(let) || let == '-') && !str.isEmpty()) {

            // Write code to insert str in to your tree here
            if (a.bSearch(sArray, str, 0, sArray.size()) >= 0) {
                correctRec++;
            } else {
                incorrectRec++;
            }

            str = "";
        }
    }
    infO.close();
    a.print(correctRec, incorrectRec);
}

public void print(int correctRec, int incorrectRec) {
    System.out.println("Out of total words " + (incorrectWords + correctWords));
    System.out.println("Correct " + correctWords);
    System.out.println("Incorrect " + incorrectWords);
    System.out.println("Total number of recursive steps is " + (correctRec + incorrectRec));
    System.out.println("The average number of comparisons for a word found = " + correctRec / correctWords);
    System.out.println("The average number of comparisons for a word not found = " + incorrectRec / incorrectWords);
}

public void readDictonary(ArrayList<String> sArray) {
    try {
        File f = new File("dictionary.txt");
        Scanner inf = new Scanner(f);
        while (inf.hasNext()) {
            sArray.add(inf.nextLine());
        }
    } catch (FileNotFoundException ex) {
        System.out.println("The dictonary file was not found");
    }
}

public int bSearch(ArrayList<String> sArray, String key, int lowIndex, int highIndex) {
    if (lowIndex > highIndex) {
        System.out.println(sArray.get(mid) + " is possibly mispelled");
        incorrectWords++;
        return rec * -1;
    }

    mid = (lowIndex + highIndex) / 2;

    if (sArray.get(mid).compareToIgnoreCase(key) == 0) {
        correctWords++;

        return rec;
    } else if (sArray.get(mid).compareToIgnoreCase(key) > 0) {
        rec++;
        return bSearch(sArray, key, lowIndex, mid - 1);
    } else {
        rec++;
        return bSearch(sArray, key, mid + 1, highIndex);
    }
}
}