Main Content

철자 교정을 위한 확장 사전 만들기

이 예제에서는 철자 교정을 위한 Hunspell 확장 사전을 만드는 방법을 보여줍니다.

correctSpelling 함수를 사용하면 이 함수로 철자가 올바른 단어를 업데이트할 수 있습니다. 알려진 단어의 목록을 제공하려면 알려진 단어로 구성된 string형 배열에 KnownWords 옵션을 사용합니다. 또는 알려진 단어와 금지어, 접사 규칙이 적용된 단어의 목록을 지정하는 Hunspell 확장 사전(개인 사전)을 지정할 수도 있습니다.

알려진 단어 지정하기

토큰화된 문서로 구성된 배열을 만듭니다.

str = [
    "Use MATLAB to correct spelling of words."
    "Correctly spelled worrds are important for lemmatizing."
    "Text Analytics Toolbox providesfunctions for spelling correction."];
documents = tokenizedDocument(str);

correctSpelling 함수를 사용하여 문서 철자를 교정합니다.

updatedDocuments = correctSpelling(documents)
updatedDocuments = 
  3x1 tokenizedDocument:

    9 tokens: Use MAT LAB to correct spelling of words .
    8 tokens: Correctly spelled words are important for legitimatizing .
    9 tokens: Text Analytic Toolbox provides functions for spelling correction .

이 함수는 단어 "worrds"와 "providesfunctions"의 철자를 교정했습니다. 하지만 다음과 같이 철자가 올바른 단어도 일부 수정했습니다.

  • 입력 단어 "MATLAB"이 두 단어 "MAT"와 "LAB"으로 분할되었습니다.

  • 입력 단어 "lemmatizing"이 "legitimatizing"으로 변경되었습니다.

  • 입력 단어 "Analytics"가 "Analytic"으로 변경되었습니다.

알려진 단어 목록이 포함된 Hunspell 확장 사전을 만들려면, 해당 단어들이 한 줄에 하나씩 포함되어 있는 .dic 파일을 만듭니다. 단어 "MATLAB", "lemmatization", "Analytics"가 포함된 확장 사전 knownWords.dic 파일을 만듭니다.

MATLAB
Analytics
lemmatizing

문서에서 다시 철자 교정을 수행하고 확장 사전 knownWords.dic를 지정합니다.

updatedDocuments = correctSpelling(documents,ExtensionDictionary="knownWords.dic")
updatedDocuments = 
  3x1 tokenizedDocument:

    8 tokens: Use MATLAB to correct spelling of words .
    8 tokens: Correctly spelled words are important for lemmatizing .
    9 tokens: Text Analytics Toolbox provides functions for spelling correction .

접사 규칙 지정하기

예를 들어 "lemmatize", "lemmatizer", "lemmatized"처럼 동일한 원형을 갖는 단어를 여러 개 지정할 때 접사 규칙으로 나타내는 것이 더 쉬울 수 있습니다. 동일한 단어를 여러 다른 접사를 사용하여 여러 번 지정하는 대신, 접사 규칙을 상속하는 특정 단어를 지정할 수 있습니다.

예를 들어 토큰화된 문서로 구성된 배열을 만들고 correctSpelling 함수를 사용합니다.

str = [
    "A lemmatizer reduces words to their dictionary forms."
    "To lemmatize words, use the normalizeWords function."
    "Before lemmatizing, add part of speech details to the text."
    "Display lemmatized words in a word cloud."];
documents = tokenizedDocument(str);
updatedDocuments = correctSpelling(documents)
updatedDocuments = 
  4x1 tokenizedDocument:

     9 tokens: A legitimatize reduces words to their dictionary forms .
    10 tokens: To legitimatize words , use the normalize Words function .
    12 tokens: Before legitimatizing , add part of speech details to the text .
     8 tokens: Display legitimatized words in a word cloud .

단어 "normalizeWords"와 "lemmatize"의 변형이 올바르게 업데이트되지 않았음을 알 수 있습니다.

단어 "normalizeWords"와 "lemmatize"가 포함된 확장 사전 knownWordsWithAffixes.dic 파일을 만듭니다. 단어 "lemmatize"의 경우 "/" 기호를 사용하여 단어 "equalize"의 유효한 접사도 포함하도록 지정합니다.

normalizeWords
lemmatize/equalize

문서에서 다시 철자 교정을 수행하고 확장 사전 knownWordsWithAffixes.dic를 지정합니다.

updatedDocuments = correctSpelling(documents,ExtensionDictionary="knownWordsWithAffixes.dic")
updatedDocuments = 
  4x1 tokenizedDocument:

     9 tokens: A lemmatizer reduces words to their dictionary forms .
     9 tokens: To lemmatize words , use the normalizeWords function .
    12 tokens: Before lemmatizing , add part of speech details to the text .
     8 tokens: Display lemmatized words in a word cloud .

"lemmatize"의 변형이 바뀌지 않았음을 알 수 있습니다. 디폴트 사전은 단어 "equalize"를 포함하고 있으며, 접미사 "-r"과 "-d"를 통해 단어 "equalizer"와 "equalized"를 각각 인식합니다. 항목 "lemmatize/equalize"를 지정하면 단어 "lemmatize"를 인식할 뿐 아니라, "equalize"에 해당되는 접사를 다른 단어에도 확장 적용하여 인식할 수 있습니다. 예를 들어 단어 "lemmatizer"와 "lemmatized"를 인식합니다.

금지어 지정하기

correctSpelling 함수를 사용할 때 이 함수는 사전에 더 적합한 단어가 있는 경우에도 부적합한 단어를 출력할 수 있습니다. 예를 들어 입력 단어 "Decrese"에 대해 correctSpelling 함수는 단어 "Decrees"를 출력할 수 있습니다. 특정 단어가 출력값에 나타나지 않도록 하기 위해 확장 사전에 금지어를 지정할 수 있습니다.

예를 들어 토큰화된 문서로 구성된 배열을 만들고 확장 사전 knownWords.dic를 사용하여 철자를 교정해 보겠습니다. 이 사전에는 단어 "MATLAB"이 들어 있습니다.

str = [
    "Analyze text data using MATLAB."
    "Decrese the number of typos using an extension dictionary."];
documents = tokenizedDocument(str);
updatedDocuments = correctSpelling(documents,ExtensionDictionary="knownWords.dic")
updatedDocuments = 
  2x1 tokenizedDocument:

     6 tokens: Analyze text data using MATLAB .
    10 tokens: Decrees the number of typos using an extension dictionary .

단어 "decrease"가 사전에 있어도 이 함수는 여전히 다른 단어를 일치 항목으로 선택할 수 있습니다. 이 경우 함수에서는 단어 "decrees"를 선택합니다.

단어 "MATLAB"이 포함된 확장 사전 knownWordsWithForbiddenWords.dic 파일을 만들고, "*" 기호를 사용하여 금지어 "decree"도 지정합니다. 금지어를 지정하는 경우에는 단어 원형을 지정해야 합니다. 예를 들어 함수에서 복수형 "decrees"를 출력하지 않도록 하려면 단어의 원형인 "decree"를 지정하십시오.

MATLAB
*decree

확장 사전 knownWordsWithForbiddenWords.dic를 사용하여 문서 철자를 교정합니다.

updatedDocuments = correctSpelling(documents,ExtensionDictionary="knownWordsWithForbiddenWords.dic")
updatedDocuments = 
  2x1 tokenizedDocument:

     6 tokens: Analyze text data using MATLAB .
    10 tokens: Decrease the number of typos using an extension dictionary .

여기서는 단어 "Decrese"가 "Decrease"로 교정되었습니다.

참고 항목

|

관련 항목