areum

[NLP] 형태소 분석하기(feat. KoNLPy) 본문

Programming/NLP

[NLP] 형태소 분석하기(feat. KoNLPy)

armmy 2023. 3. 30. 16:17
728x90

Okt이용하여 형태소 분석해 보기 ( 형태소 빈도 측정)


1. 기본세팅

from collections import Counter 

import numpy as np
import pandas as pd

from tqdm import tqdm

# konlpy
from konlpy.tag import Okt
okt = Okt()

# 데이터 불러오기
df = pd.read_csv('/content/ratings_train.txt',sep='\t')
# 데이터 양이 너무 많아 200행까지만 실행해보았습니다.
df=df.iloc[0:200,0:2]

2. Okt이용하여 형태소 나눠주기

sentences = []
for cp in tqdm(df.document.dropna()):
    sentences.append(okt.pos(cp))

3. 단어 분류하기

words = []
for sentence in tqdm(sentences):
  for word, tag in sentence:
        if tag in ['Noun']: 
            words.append(word)

counts = Counter(words)
tags = counts.most_common(10)
tags

4. dataframe으로 만들어주기

df3 = pd.DataFrame(tags, columns=['단어', '빈도'])
df3