简单 java 拼写检查器/检查单词是否存在于字典中
Simple java spell checker / check if the word exists in the dictionary
我正在编写一个简单的拼写检查程序。只是为了检查用户文本的拼写与小词典文件的拼写并进行比较。
'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
我看到的问题是 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);
}
}
}
我已经测试了下面的代码,它按预期工作。每当您在字典中找到一个单词时,对于每个单词,您都可以将标志设置为 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");
}
}
}
在 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");
}
...
试试这个。
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
我正在编写一个简单的拼写检查程序。只是为了检查用户文本的拼写与小词典文件的拼写并进行比较。
'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
我看到的问题是 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);
}
}
}
我已经测试了下面的代码,它按预期工作。每当您在字典中找到一个单词时,对于每个单词,您都可以将标志设置为 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");
}
}
}
在 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");
}
...
试试这个。
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