728x90

1. 품사 태깅을 위한 준비과정:
- nltk(Natural Language Toolkit), pos_tag, word_tokenize를 임포트한다.
- word_tokenize 함수를 사용하여 텍스트를 단어로 토큰화한다.

2. 태거 다운로드:
- nltk.download('averaged_perceptron_tagger')를 실행하여 품사 태깅에 사용되는 태거를 다운로드 받는다. 이 태거는 사전에 훈련된 모델을 기반으로 텍스트의 단어들에 품사를 부착하는 작업을 수행한다.

3. 품사 태깅:
- "Rachel loves drinking coffee in the morningg"라는 샘플 텍스트 데이터를 변수 text_data에 할당한다.
- pos_tag 함수를 사용하여 word_tokenize로 토큰화한 단어들에 품사 태깅을 수행한다. pos_tag 함수는 주어진 단어들에 대해 품사를 부착하여 (단어, 품사)의 튜플 형태로 반환한다.
- 태깅된 결과를 text_tagged 변수에 할당한다
- print(text_tagged)를 통해 품사 태깅된 결과를 출력한다. 출력되는 결과는 [(단어, 품사), ...]의 형태로 표시된다. 예를 들어, [('Rachel', 'NNP'), ('loves', 'VBZ'), ('drinking', 'VBG'), ('coffee', 'NN'), ('in', 'IN'), ('the', 'DT'), ('morningg', 'NN')]와 같은 형식이다.

 

 

# 품사 태깅
import nltk
from nltk import pos_tag
from nltk import word_tokenize

# 태거 다운로드
nltk.download('averaged_perceptron_tagger')
# 샘플 텍스트 데이터
text_data = "Rachel loves drinking coffee in the morningg"

# 사전 훈련된 품사 태킹을 사용
text_tagged = pos_tag(word_tokenize(text_data))
print(text_tagged)

출력 결과:

[('Rachel', 'NNP'), ('loves', 'VBZ'), ('drinking', 'VBG'), ('coffee', 'NN'), ('in', 'IN'), ('the', 'DT'), ('morningg', 'NN')]

 

728x90

+ Recent posts