[python] NLTK를 사용해 문장의 긍정/부정 점수를 계산하는 방법은 무엇인가요?

NLTK(Natural Language Toolkit)는 자연어 처리를 위한 파이썬 라이브러리입니다. NLTK를 사용하면 문장의 긍정 점수와 부정 점수를 계산할 수 있습니다. 이를 위해 다음과 같은 단계를 따릅니다:

  1. 감정 점수 계산을 위해 SentimentIntensityAnalyzer 클래스를 임포트합니다:
     from nltk.sentiment import SentimentIntensityAnalyzer
    
  2. SentimentIntensityAnalyzer 객체를 생성합니다:
     sid = SentimentIntensityAnalyzer()
    
  3. 문장의 긍정/부정 점수를 계산하려는 문장을 입력합니다:
     sentence = "이 영화는 정말로 멋지고 흥미로워요!"
    
  4. 문장의 점수를 계산합니다:
     scores = sid.polarity_scores(sentence)
    
  5. scores 딕셔너리에서 ‘pos’ 키를 사용해 긍정 점수를 추출합니다:
     positive_score = scores['pos']
    
  6. scores 딕셔너리에서 ‘neg’ 키를 사용해 부정 점수를 추출합니다:
     negative_score = scores['neg']
    
  7. 최종적으로, 긍정 점수와 부정 점수를 출력합니다:
     print("긍정 점수:", positive_score)
     print("부정 점수:", negative_score)
    

위의 코드를 실행하면 문장의 긍정/부정 점수를 계산하여 출력할 수 있습니다. 이를 통해 자연어 처리를 기반으로 문장의 감정을 분석할 수 있습니다.

NLTK와 SentimentIntensityAnalyzer에 대한 더 자세한 정보는 다음 링크를 참고하세요: