본 과제의 목적은 자연어처리(NLP)에서 널리 사용되는 세 가지 서브워드 토크나이저(BPE, WordPiece, Unigram)를 직접 학습하고 비교 분석하는 것이다. 각 모델을 동일한 학습 데이터로 훈련시키고, 토큰화 결과를 비교하여 장단점과 차이점을 도출하였다.
re.sub(r'[^a-zA-Z0-9\s]', '', text)text.lower()review_clean_text_half.txt 파일 생성데이터는 라인 단위로 학습 및 테스트에 사용하였다. 라인 단위 처리는 리뷰 간 의미 단위 경계를 보존하고, 데이터가 너무 길어지는 것을 방지하여 학습 속도 및 토큰 품질에 긍정적 영향을 주었다.
merges 딕셔너리와 vocab 생성Ġ 기호로 공백을 구분하여 GPT-2 스타일로 구현vocab = ['<PAD>', '<UNK>', 'Ġ', 'a', 'b', 'c', 'Ġt', 'th', 'er', ...]
## 기호를 붙여 중간 이후를 나타냄vocab = ['[PAD]', '[UNK]', '[CLS]', '[SEP]', '[MASK]', 'a', 'b', '##tion', '##ed', ...]
vocab = ['a', 'b', 't', 'ing', 'tion', 'ed', 'ly', ...]
i have bought several of the vitality canned dog food products and have found them all to be of good quality the product looks more like a stew than a processed meat and it smells better my labrador is finicky and she appreciates this product better than most
| 모델 | 토큰화 결과 | 토큰 수 |
|---|---|---|
| BPE | i Ġ h a v e Ġ b o u g h t Ġ s e v e r a l... | 59 |
| WordPiece | i have bought several of the v ##it ##ality can ##ned... | 44 |
| Unigram (HuggingFace) | i have bought several of the vitality canned dog food... | 48 |
| Unigram (직접 구현) | i have bought several of the v it a l it y canned... | 60 |
product arrived labeled as jumbo salted peanutsthe peanuts were actually small sized unsalted not sure if this was an error or if the vendor intended to represent the product as jumbo
| 모델 | 토큰화 결과 | 토큰 수 |
|---|---|---|
| BPE | p r o d u c t Ġ a r r i v e d Ġ l a b e l e d... | 60 |
| WordPiece | product arr ##ived l ##abel ##ed as ju ##m ##bo... | 41 |
| Unigram (HuggingFace) | product arrived labeled as jumbo salted peanuts the peanuts were... | 50 |
| Unigram (직접 구현) | product arrived label e d as j u m b o salt e d peanut s... | 62 |

본 실습을 통해 BPE, WordPiece, Unigram 토크나이저를 동일한 데이터와 환경에서 학습하고 테스트하였다.
간단한 스터디 결과, WordPiece 모델을 추천하며, 특히 영어와 같은 언어에서는 효율적인 학습이 가능할 것으로 판단된다. 다만, 한국어와 같은 형태소 중심 언어에서는 Unigram(HuggingFace 버전)의 장점이 부각될 수 있으므로 추가 실험이 필요하다.