update code
Browse files
code.py
CHANGED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import nltk
|
2 |
+
from nltk.sentiment import SentimentIntensityAnalyzer
|
3 |
+
|
4 |
+
# Download NLTK resources (only need to run once)
|
5 |
+
nltk.download('vader_lexicon')
|
6 |
+
# Sample text for sentiment analysis
|
7 |
+
with open("lks.txt", 'r') as file:
|
8 |
+
fl = file.read()
|
9 |
+
|
10 |
+
contactId = fl.split("|")[0]
|
11 |
+
transcript=fl.split("|")[1]
|
12 |
+
transcript=transcript.replace("'",'')
|
13 |
+
# Initialize the sentiment analyzer
|
14 |
+
sia = SentimentIntensityAnalyzer()
|
15 |
+
print(transcript)
|
16 |
+
# Analyze sentiment
|
17 |
+
sentiment_score = sia.polarity_scores(transcript)
|
18 |
+
# Initialize dictionary to store tone counts
|
19 |
+
tones = {
|
20 |
+
'analytical': 0,
|
21 |
+
'anger': 0,
|
22 |
+
'confident': 0,
|
23 |
+
'fear': 0,
|
24 |
+
'joy': 0,
|
25 |
+
'sadness': 0,
|
26 |
+
'tentative': 0
|
27 |
+
}
|
28 |
+
# Apply thresholds and count tones
|
29 |
+
if sentiment_score['compound'] >= 0.05: # Threshold for positive sentiment
|
30 |
+
tones['joy'] += 1
|
31 |
+
elif sentiment_score['compound'] <= -0.05: # Threshold for negative sentiment
|
32 |
+
tones['anger'] += 1
|
33 |
+
elif sentiment_score['neg'] >= 0.5: # Threshold for high negativity
|
34 |
+
tones['sadness'] += 1
|
35 |
+
elif sentiment_score['pos'] <= 0.2: # Threshold for low positivity
|
36 |
+
tones['fear'] += 1
|
37 |
+
elif sentiment_score['neu'] >= 0.5: # Threshold for high neutrality
|
38 |
+
tones['tentative'] += 1
|
39 |
+
else: # Otherwise, consider it analytical or confident
|
40 |
+
tones['analytical'] += 1
|
41 |
+
tones['confident'] += 1
|
42 |
+
# Print tone counts
|
43 |
+
print("Tone Counts:", tones)
|
44 |
+
|
45 |
+
|
46 |
+
# sample output
|
47 |
+
#Tone Counts: {'analytical': 0, 'anger': 0, 'confident': 0, 'fear': 0, 'joy': 1, 'sadness': 0, 'tentative': 0}
|