자바 맞춤법 검사

Sheeraz Gul 2023년10월12일
자바 맞춤법 검사

이 자습서는 Java에서 단어 철자를 검사하는 방법을 보여줍니다.

자바 맞춤법 검사

많은 도서관에서 영어 사전을 사용하여 단어의 철자를 확인할 수 있습니다. 이 라이브러리는 영어 단어의 철자를 검사하기 위해 사전을 만들지 않으려는 경우에 사용할 수 있습니다.

그러나 사전에 대한 파일이 있고 해당 사전을 사용하여 단어의 철자를 검사하는 데 파일을 사용하려는 경우 Java 코드를 사용하여 철자 검사기를 만들 수 있습니다. 맞춤법 검사기를 만들려면 아래 단계를 따르세요.

  • 먼저 사전 파일의 HashSet을 만듭니다.
  • 사용자로부터 입력을 받는 스캐너를 만듭니다. 입력은 문장이 됩니다.
  • 문장을 단어로 분할하여 배열에 넣습니다.
  • for 루프를 사용하여 사전 파일에서 각 단어를 확인하십시오.
  • 파일에 단어가 포함되어 있으면 올바른 것입니다. 그렇지 않으면 잘못된 것입니다.

위의 단계를 기반으로 예제를 구현해 보겠습니다. 우리는 여기에서 84,000개 이상의 단어가 포함된 사전 파일을 다운로드했습니다. 예를 참조하십시오.

package delftstack;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Example {
  public static void main(String[] args) throws Exception {
    Set<String> Demo_Dictionary = new HashSet<>(Files.readAllLines(Paths.get("dictionary.txt")));
    Scanner Demo_Input = new Scanner(System.in);
    System.out.println("Type a sentence to check the spelling/correct words :)");
    String Demo_Sentence = Demo_Input.nextLine();
    String[] Sentence_Words = Demo_Sentence.split(" ");
    for (String Word : Sentence_Words)
      if (Demo_Dictionary.contains(Word))
        System.out.println(Word + " : correct");
      else
        System.out.println(Word + " : incorrect");
  }
}

주어진 사전에서 문장의 철자를 확인해 봅시다. 출력 참조:

Type a sentence to check the spelling/correct words :)
hello this is delftstack the best tutorial site
hello : correct
this : correct
is : correct
delftstack : incorrect
the : correct
best : correct
tutorial : correct
site : correct

위의 문장에서 사전에는 delftstack이라는 단어가 포함되어 있지 않으므로 올바르지 않으며, 사전에 추가하려면 dictionary.txt 파일 끝에 해당 단어를 추가하십시오.

작가: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook