본문 바로가기
데이터분석/Python

머신러닝 Example by Python - 품사 분석 시스템 만들기 (문서 분류)

by 버섯도리 2022. 1. 14.
# 스탠포드 품사 태깅 프로그램(zip) 다운로드 경로 : https://nlp.stanford.edu/software/tagger.shtml#Download

import nltk
nltk.download('punkt')


## 1. 품사 태깅하기

from nltk.tag import StanfordPOSTagger
from nltk.tokenize import word_tokenize

STANFORD_POS_MODEL_PATH = 'F:/1_Study/1_BigData/7_FirstML/stanford-postagger-full-2020-11-17/models/english-bidirectional-distsim.tagger'
STANFORD_POS_JAR_PATH = 'F:/1_Study/1_BigData/7_FirstML/stanford-postagger-full-2020-11-17/stanford-postagger-4.2.0.jar'

pos_tagger = StanfordPOSTagger(STANFORD_POS_MODEL_PATH, STANFORD_POS_JAR_PATH)

text = 'One day in November 2016, the two authors of this book, Seungyeon and Youngjoo, had a coffer at Red Back cafe, which is very popular place in Mountain View.'

tokens = word_tokenize(text)
print(tokens) # 쪼개진 토큰을 출력합니다.
print()
print(pos_tagger.tag(tokens)) # 품사 태깅을 하고 그 결과를 출력합니다.

# 동사, 명사만 추출
noun_and_verbs = []
for token in pos_tagger.tag(tokens):
    if token[1].startswith('V') or token[1].startswith('N'):
        noun_and_verbs.append(token[0])
print(', '.join(noun_and_verbs))
 

 

 

 

 
 
출처 : 처음 배우는 머신러닝 : 기초부터 모델링, 실전 예제, 문제 해결까지