-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment analysis.py
More file actions
63 lines (52 loc) · 1.47 KB
/
Copy pathsentiment analysis.py
File metadata and controls
63 lines (52 loc) · 1.47 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
61
62
63
import sys
try:
from textblob import TextBlob
except Exception:
print(
"Missing dependency 'textblob'. Install it with: python -m pip install textblob",
file=sys.stderr,
)
sys.exit(1)
try:
from newspaper import Article
except Exception:
print(
"Missing dependency 'newspaper3k' or one of its extras.\n"
"Install with: python -m pip install newspaper3k lxml_html_clean",
file=sys.stderr,
)
sys.exit(1)
def analyze_url(url: str) -> int:
"""Download article at `url`, print a summary and sentiment polarity.
Returns 0 on success, non-zero on failure.
"""
try:
article = Article(url)
article.download()
article.parse()
# NLP step (may fail if article is empty or network issues)
try:
article.nlp()
except Exception:
# newspaper3k's nlp can fail silently; continue with parsed text
pass
text = article.summary or article.text or ""
if not text.strip():
print("No summary or article text could be extracted.")
return 2
print("Article Summary:")
print(text)
print("-" * 20) # Separator
blob = TextBlob(text)
sentiment = blob.sentiment.polarity # Value between -1 and 1
print(f"Sentiment Polarity: {sentiment}")
return 0
except Exception as exc:
print(f"Error while processing the article: {exc}", file=sys.stderr)
return 1
if __name__ == "__main__":
# Default URL (change as needed)
url = (
"https://www.dinamalar.com/news/india-tamil-news/aiadmks-brahmastra-is-a-double-leaf-says-rajini-/3788417"
)
sys.exit(analyze_url(url))