nltk swntiment tone nlp en region:us
---
language:
- en
tags:
- nltk
- swntiment
- tone
- nlp
---
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

# Download NLTK resources (only need to run once)
nltk.download('vader_lexicon')

# Sample text for sentiment analysis
with open("lks.txt", 'r') as file:
            fl = file.read()
        
contactId = fl.split("|")[0]
transcript=fl.split("|")[1]
transcript=transcript.replace("'",'')
# Initialize the sentiment analyzer
sia = SentimentIntensityAnalyzer()
print(transcript)

# Analyze sentiment
sentiment_score = sia.polarity_scores(transcript)

# Initialize dictionary to store tone counts
tones = {
    'analytical': 0,
    'anger': 0,
    'confident': 0,
    'fear': 0,
    'joy': 0,
    'sadness': 0,
    'tentative': 0
}

# Apply thresholds and count tones
if sentiment_score['compound'] >= 0.05:  # Threshold for positive sentiment
    tones['joy'] += 1
elif sentiment_score['compound'] <= -0.05:  # Threshold for negative sentiment
    tones['anger'] += 1
elif sentiment_score['neg'] >= 0.5:  # Threshold for high negativity
    tones['sadness'] += 1
elif sentiment_score['pos'] <= 0.2:  # Threshold for low positivity
    tones['fear'] += 1
elif sentiment_score['neu'] >= 0.5:  # Threshold for high neutrality
    tones['tentative'] += 1
else:  # Otherwise, consider it analytical or confident
    tones['analytical'] += 1
    tones['confident'] += 1

# Print tone counts
print("Tone Counts:", tones)


# sample output
#Tone Counts: {'analytical': 0, 'anger': 0, 'confident': 0, 'fear': 0, 'joy': 1, 'sadness': 0, 'tentative': 0}