-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment.py
More file actions
57 lines (48 loc) · 1.68 KB
/
sentiment.py
File metadata and controls
57 lines (48 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
## Sentiment.py
## Richard Weaver
## 08/03/2022
## Functions to import positive and negative words lists.
def load_words(filename):
content = open(filename)
lines = content.read().splitlines()
content.close()
return lines
def load_positive_words():
return load_words("pos_words.txt")
def load_negative_words():
return load_words("neg_words.txt")
## This function works out negative or positive score for each individual word.
def sentiment_of_word(word):
score = 0
if word in pos_words:
score += 1
elif word in neg_words:
score -= 1
else:
score = score
return(score)
## This is a personal test for individual words:
# print(sentiment_of_word("breeze"))
## This is the function for calculating sentiment of whole text.
def sentiment_of_text(text):
total_text_score = 0
word = text.split()
## A for loops which goes through the text, calculating sentiment of each word and then adding total_score together.
for sentiment in word:
sentiment = sentiment.strip("!.,!?;:()[]")
score = sentiment_of_word(sentiment)
total_text_score += score
return(total_text_score)
## This is a personal test for text:
# print(sentiment_of_text("summer breeze, makes me feel agile, bless, breeze, bright"))
## The Positive and Negative Words Lists.
pos_words = load_positive_words()
neg_words = load_negative_words()
## Final test for functions
total_score = sentiment_of_text("Hey Camiel, It is very Sunny today, I like the breeze")
if total_score > 0:
print("The text is mostly nice!")
elif total_score < 0:
print("The text talks about mad or bad stuff :(")
else:
print("The text is not opinionated or just messy.")