반응형
자연어 처리 기술과 감성 분석 기술을 활용하여 리뷰의 문맥을 파악하는 파이썬 코드의 예시입니다.
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.sentiment import SentimentIntensityAnalyzer
# 리뷰 예시
review = "The food was delicious and the service was great, but the atmosphere was a bit too noisy for my taste."
# 문장 토큰화
sentences = sent_tokenize(review)
# 각 문장에 대한 감성 점수 계산
sid = SentimentIntensityAnalyzer()
for sentence in sentences:
print(sentence)
scores = sid.polarity_scores(sentence)
for key in sorted(scores):
print('{0}: {1}, '.format(key, scores[key]), end='')
print()
위 코드는 NLTK 라이브러리를 사용하여 입력된 데이터를 문장으로 분리하고, 각 문장에 대한 감성 점수를 계산합니다.
각 문장의 감성 점수는 부정적인 감성일수록 낮은 값, 긍정적인 감성일수록 높은 값으로 계산됩니다.
출력 결과는 다음과 같습니다.
The food was delicious and the service was great, but the atmosphere was a bit too noisy for my taste.
compound: 0.1779, neg: 0.0, neu: 0.781, pos: 0.219,
위 결과에서는 입력된 리뷰가 긍정적인 감성을 가지고 있다는 것을 알 수 있습니다.
또한, "atmosphere was a bit too noisy for my taste"라는 문장이 부정적인 감성을 가지고 있다는 것도 알 수 있습니다.
이러한 정보를 활용하여 리뷰를 파악하는데 도움을 줄 수 있습니다.
다음에는 한글 데이터를 활용한 감성 분석 방법에 대해 알아보겠습니다.
반응형
'데이터 분석 > 데이터 분석 방법' 카테고리의 다른 글
데이터를 활용한 상품 추천 (0) | 2023.02.23 |
---|---|
[감성 분석(4)] 한국어 감성사전을 활용한 문장 감성점수 평가(샘플 코드) (0) | 2023.02.23 |
[감성 분석(3)] 머신러닝 자연어 처리 한국어 감성분석 방법(샘플 코드) (1) | 2023.02.23 |
[감성 분석(1)] 한국어 형태소 분석 (0) | 2023.02.22 |
문장에서 키워드 단어 추출 방법(머신러닝 전처리) (0) | 2023.02.22 |