【问题标题】:Simple java spell checker / check if the word exists in the dictionary简单的java拼写检查器/检查字典中是否存在单词
【发布时间】:2020-08-08 10:48:45
【问题描述】:

我正在编写一个简单的拼写检查器。只是为了检查用户文本的拼写与一个小字典文件的拼写并进行比较。

“dictionary.txt”文件包含:

my
name
is

这是我检查用户文本并将其与字典进行比较的代码:

import java.util.Scanner;
import java.io.File;

public class SpellChecker
{
    public static void main(String[] args) throws Exception
    {
        Scanner write = new Scanner(System.in);

        System.out.println("Type a sentence and I will check your spelling/correct words :)");
        String sentence = write.nextLine();
        String[] splitSentence = sentence.split(" ");

        for(int i = 0; i < splitSentence.length; i++)
        {
            Scanner read = new Scanner(new File("dictionary.txt"));

            while(read.hasNextLine())
            {
                String compare = read.nextLine();

                if(compare.equalsIgnoreCase(splitSentence[i]))
                {
                    System.out.println(splitSentence[i] + " : correct");
                }
                else
                {
                    System.out.println(splitSentence[i] + " : incorrect");
                }
            }
        }
    }
}

这是我得到的输出。

Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
Heyo : incorrect
Heyo : incorrect
my : correct
my : incorrect
my : incorrect
name : incorrect
name : correct
name : incorrect
is : incorrect
is : incorrect
is : correct
Ivam : incorrect
Ivam : incorrect
Ivam : incorrect

以下是我预期的输出:

Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
my : correct
name : correct
is : correct
Ivam : incorrect

【问题讨论】:

  • 你的字典文件是不是每行只有一个单词?
  • 是的。它有 3 行 'my'、'name' 和 'is'
  • 这是因为您的 while 循环逻辑。您实际上是在使用单词检查每一行并将其打印为 correct/incorrect 以进行每行检查。
  • 您的字典可能有“空格”或不可打印的控制字符,例如回车或换行。您应该从行中解析出字符串。

标签: java loops file io


【解决方案1】:

试试这个。

public static void main(String[] args) throws Exception {
    Set<String> dictionary = new HashSet<>(Files.readAllLines(Paths.get("dictionary.txt")));
    java.util.Scanner write = new Scanner(System.in);
    System.out.println("Type a sentence and I will check your spelling/correct words :)");
    String sentence = write.nextLine();
    String[] splitSentence = sentence.split(" ");
    for (String word : splitSentence)
        if (dictionary.contains(word))
            System.out.println(word + " : correct");
        else
            System.out.println(word + " : incorrect");
}

输出

Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
my : correct
name : correct
is : correct
Ivam : incorrect

【讨论】:

    【解决方案2】:

    while 循环之外有一个boolean 标志,并在while 之外添加一个检查,如下所示

    ...
    boolean isCorrect = false;
    while(read.hasNextLine()) {
      String compare = read.nextLine();
      if (compare.equalsIgnoreCase(splitSentence[i])) {
        isCorrect = true;
        break;
      }
    }
    
    if (isCorrect) {
      System.out.println(splitSentence[i] + " : correct");
    } else {
      System.out.println(splitSentence[i] + " : incorrect");
    }
    ...
    

    【讨论】:

      【解决方案3】:

      我已经测试了下面的代码,它按预期工作。每当您在字典中找到一个单词时,对于每个单词,您都可以将标志设置为 true 并打破循环。如果最后没有找到一个单词,可以检查这个标志并打印它是不正确的。

      import java.util.Scanner;
      import java.io.File;
      
      public class test
      {
          public static void main(String[] args) throws Exception
          {
              Scanner write = new Scanner(System.in);
      
              System.out.println("Type a sentence and I will check your spelling/correct words :)");
              String sentence = write.nextLine();
              String[] splitSentence = sentence.split(" ");
              
              for(int i = 0; i < splitSentence.length; i++)
              {
                  Scanner read = new Scanner(new File("dictionary.txt"));
                  boolean found = false;
                  while(read.hasNextLine())
                  {
                      String compare = read.nextLine();
      
                      if(compare.equalsIgnoreCase(splitSentence[i]))
                      {
                          System.out.println(splitSentence[i] + " : correct");
                          found=true;
                          break;
                      }
                  }
                  if(!found)
                     System.out.println(splitSentence[i] + " : incorrect");
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        我看到的问题是,while 循环将运行特定单词与字典中的每个单词进行比较,因此返回多个结果。一种解决方案是,如果正确找到该单词,则在控制台打印出它是正确的,否则,当且仅当它已遍历所有字典并且没有找到匹配项时,才打印出它不正确.

        在这种情况下,它通过将字符串设置为不正确的默认值来解决,并且只有在找不到匹配项时才更改为正确。然后在循环结束时它只打印最新的结果字符串。我还在循环外初始化了变量,所以它不是每次都完成,因为它是不必要的。

        以下是解决方案的工作原理:

        import java.util.Scanner;
        import java.io.File;
        
        public class SpellChecker
        {
            public static void main(String[] args) throws Exception
            {
                Scanner write = new Scanner(System.in);
        
                System.out.println("Type a sentence and I will check your spelling/correct words :)");
                String sentence = write.nextLine();
                String[] splitSentence = sentence.split(" ");
        
                
                String result = null;
                String compare = null;
                Scanner read = null;
        
                for(int i = 0; i < splitSentence.length; i++)
                {
                    read = new Scanner(new File("dictionary.txt"));
        
                    result = "incorrect";
                    
                    while(read.hasNextLine())
                    {
                        compare = read.nextLine();
        
                        if(compare.equalsIgnoreCase(splitSentence[i]))
                        {
                            result = "correct";
                            break;
                        }
                        
                    }
                    
                    System.out.println(splitSentence[i] + " : " + result);
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2018-03-22
          • 1970-01-01
          • 2011-05-30
          • 2011-05-23
          • 1970-01-01
          • 2016-07-13
          • 2017-12-11
          • 1970-01-01
          • 2011-05-28
          相关资源
          最近更新 更多