본문 바로가기

AI

[NLP] Text Tokenization & Edit Distance

Text Tokenization은 NLP Pipelines에 가장 근본적인 파트라고 할 수 있다.

예를 들어서..

  • Separating words in running text
  • Normalizing word formats (e.g., favorurite = favorite)
  • Segmenting sentences in running text

ex) I do uh.. main.. mainly business data processing.

예문을 보면 중복된 단어(main/mainly), 불완전한 표현들(uh)이 있다. 한가지 더

ex) Seuss's cat in the hat is different from other cats!

예문을 보면 cat과 cats는 표면상 다르지만, Lemma 기준으로 둘 다 cat, 하지만 Wordform 기준으로 다르다.(형태가)

개념 정의 예시
Lemma 의미나 품사가 같고 어근이 같을 때 묶는 단위 "cat"과 "cats" → 같은 lemma
Wordform 표면적으로 보이는 단어 형태 "cat" ≠ "cats" (형태가 다름)

 

Type: 고유한 단어의 종류

Token: 실제 문장 내에서 등장하는 단어의 인스턴스

ex) Alice looked all round the table, but there was nothing on it but tea.

여기서 but은 두번 등장했다. 그렇다면 16 tokens인가? 아니면 15 tokens?

→ 같은 단어라도 반복되면 token 수는 늘어나고, type 수는 그대로라고 생각하면 된다. 그러니까 16 tokens, 15 types 이다. (근데 구두점 , . 의 경우는 세는 모델도 있고, 안세는 모델도 있다.)

 

Tokenization을 하다가 보면 꽤 많은 Issue들이 있다.

  • Contraction: L'ensemble →1 token? or 2 token?
  • Token이 공백으로 구분되지 않을 경우

여러가지가 있지만, 요런 문제를 해결하기 위해 Edit Distance라는 것이 있다.

 

Edit Distance란?

두 문자열 사이의 거리를 측정함으로써, 두 문자열 사이의 어휘 유사도(lexical similarity)를 측정한다.

기본적으로 3가지가 포함된다.

  • 삽입 (Insertion)
  • 삭제 (Deletion)
  • 치환 (Substitution)

 

최소한의 움직임으로 원하는 문자로 바꾸기 위해 DP를 사용해 Edit Distance를 계산한다.

Edit Distance Table

 

'AI' 카테고리의 다른 글

[NLP] Text Preprocessing  (0) 2025.04.17
[NLP] Finite State Dialogue Manager(FSDM)  (0) 2025.04.17
[NLP] Chatbot and NLU  (0) 2025.04.17
[NLP] 시작  (5) 2025.04.16
Transformer  (0) 2024.12.16