Advertisement
Python253

nlp_sentiment_intensity_analyzer_nltk

Mar 8th, 2024 (edited)
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: nlp_sentiment_intensity_analyzer_nltk.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This script analyzes the sentiment intensity of a given text using NLTK's SentimentIntensityAnalyzer.
  8.  
  9. Requirements:
  10. - Python 3
  11. - NLTK library
  12. - 'vader_lexicon' NLTK resource
  13.  
  14. Usage:
  15. - Run the script, and it will print the sentiment score of the provided example text.
  16.  
  17. Example:
  18.  
  19. Input:
  20. Text: 'Natural Language Processing is a fascinating field. It involves the use of computers to understand and process human language.'
  21.  
  22. Output:
  23. Sentiment Score: {'neg': 0.0, 'neu': 0.727, 'pos': 0.273, 'compound': 0.7184}
  24. """
  25.  
  26. import nltk
  27. from nltk.sentiment import SentimentIntensityAnalyzer
  28.  
  29. # Download the 'vader_lexicon' resource
  30. nltk.download('vader_lexicon')
  31.  
  32. # Create a SentimentIntensityAnalyzer object
  33. sia = SentimentIntensityAnalyzer()
  34.  
  35. # Sample text
  36. text = "Natural Language Processing is a fascinating field. It involves the use of computers to understand and process human language."
  37.  
  38. # Get the sentiment score
  39. sentiment_score = sia.polarity_scores(text)
  40. print("Sentiment Score:", sentiment_score)
  41.  
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement