Skip to content

Commit b506f19

Browse files
committed
add global and word by word sentiment
1 parent 10cbbb6 commit b506f19

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

__pycache__/menu.cpython-310.pyc

53 Bytes
Binary file not shown.
254 Bytes
Binary file not shown.

main.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,26 @@ def main():
4949
print(f"The new text is: \"{new_text}\"\n\n")
5050

5151
elif choice == "3":
52-
print("Selected: 3 - Extract text sentiment")
52+
print("Selected: 3 - Extract global text sentiment")
5353
print("Insert text: ")
5454
text = input()
55-
test(text)
55+
result = global_sentiment(text)
5656

57+
print(f"\nThe global text sentiment is: \"{result}\"\n\n")
5758

59+
elif choice == "4":
60+
print("Selected: 4 - Extract sentiment of each word")
61+
print("Insert text: ")
62+
text = input()
63+
result = sentiment_word_list(text)
64+
65+
neg = result["negative"]
66+
neu = result["neutral"]
67+
pos = result["positive"]
5868

59-
# print(f"The new text is: \"{new_text}\"\n\n")
69+
print(f"\nThe negative words are: \"{neg}\"\n")
70+
print(f"The neutral words are: \"{neu}\"\n")
71+
print(f"The positive words are: \"{pos}\"\n")
6072

6173
elif choice == "quit":
6274
break

menu.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ def choose_func():
33
print("0 - Count appearances of a character in a manually inputed text")
44
print("1 - Total number of characters in a manually inputed text")
55
print("2 - Swap words in a text")
6-
print("3 - Extract text sentiment")
6+
print("3 - Extract global text sentiment")
7+
print("4 - Extract sentiment of each word")
78
print("quit - Finish the app")
89

910
choice = input()

sentiment.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,37 @@
33
nltk.download('punkt')
44
nltk.download('vader_lexicon')
55

6-
def test(test_subset):
6+
def global_sentiment(sentence, optimism_rate=0.75):
77

8-
tokens = nltk.word_tokenize(test_subset)
8+
sid = SentimentIntensityAnalyzer()
9+
polarity = sid.polarity_scores(sentence)
10+
11+
if polarity["compound"] >= (1-optimism_rate):
12+
return "Positive"
13+
elif polarity["compound"] <= -(1-optimism_rate):
14+
return "Negative"
15+
else:
16+
return "Neutral"
17+
18+
19+
def sentiment_word_list(sentence, optimism_rate=0.75):
20+
21+
tokens = nltk.word_tokenize(sentence)
922

1023
sid = SentimentIntensityAnalyzer()
24+
1125
pos_word_list=[]
1226
neu_word_list=[]
1327
neg_word_list=[]
1428

1529
for word in tokens:
16-
if (sid.polarity_scores(word)['compound']) >= 0.25:
30+
if (sid.polarity_scores(word)['compound']) >= (1-optimism_rate):
1731
pos_word_list.append(word)
18-
elif (sid.polarity_scores(word)['compound']) <= -0.25:
32+
elif (sid.polarity_scores(word)['compound']) <= -(1-optimism_rate):
1933
neg_word_list.append(word)
2034
else:
21-
neu_word_list.append(word)
35+
neu_word_list.append(word)
2236

23-
print('Positive :',pos_word_list)
24-
print('Neutral :',neu_word_list)
25-
print('Negative :',neg_word_list)
26-
27-
print(sid.polarity_scores(test_subset))
37+
result = {"negative": neg_word_list, "neutral": neu_word_list, "positive": pos_word_list}
38+
39+
return result

0 commit comments

Comments
 (0)