-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analysis.py
More file actions
60 lines (47 loc) · 1.56 KB
/
Copy pathsentiment_analysis.py
File metadata and controls
60 lines (47 loc) · 1.56 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
58
59
60
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
# from pysentimiento import create_analyzer
try:
nltk.data.find("sentiment/vader_lexicon.zip")
except LookupError:
nltk.download("vader_lexicon")
# NLP sentiment analysis - Only call it once
analyser_en = SentimentIntensityAnalyzer()
def vibe_score_en(news_title: str, news_description: str):
"""
Sentiment analysis the english text (both title and description) using
the package nltk.
Parameters:
- news_title (str): Title news
- news_description (str): Descriptive text of the news
Output:
- Sentiment score as an emojis
"""
# Add text together
news_text = f"{news_title}: {news_description}"
# Calculate the polarity scores
result = analyser_en.polarity_scores(news_text)
# Define the news sentiment
if result["compound"] > 0.05:
vibe = "🟢"
elif result["compound"] < -0.05:
vibe = "🔴"
else:
vibe = "🟡"
return vibe
### Removed because it is too heavy in the server
# def vibe_score_pt(news_title: str, news_description: str):
# # NLP sentiment analysis
# analyser_pt = create_analyzer(task="sentiment", lang="pt")
# # Add text together
# news_text = f"{news_title}: {news_description}"
# # Calculate the polarity scores
# result = analyser_pt.predict(news_text)
# # Define the news sentiment
# if result.output == "POS":
# vibe = "🟢"
# elif result.output == "NEG":
# vibe = "🔴"
# else:
# vibe = "🟡"
# return vibe