-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotion.py
More file actions
84 lines (61 loc) · 2.47 KB
/
emotion.py
File metadata and controls
84 lines (61 loc) · 2.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from textblob import TextBlob #used for sentiment analysis, spelling correction, part-of-speech tagging, and translation.
import nltk
from newspaper import Article #used to extract and analyze the article
print(''' CHOOSE FROM THE MENU:
1. Analyze the sentiment of the text file
2. Analyze the sentiment of a news article from a URL
''')
option_choosen = int(input("Enter the option: "))
if option_choosen == 1:
print("You have choosen to Analyze the sentiment of the text file")
elif option_choosen == 2:
print("You have choosen to Analyze the sentiment of a news article from a URL")
else:
print("Invalid option choosen")
exit()
def sentiment_analysis_from_text_file():
#checking sentiment for text files
#writing the text in the file
with open("myfile.txt" , 'w+') as f:
data = input("enter the text: ")
f.write(data)
#reading the text from the file
with open("myfile.txt" , 'r') as f:
text = f.read()
if(text == ""):
print("The file is empty","\n")
exit()
print("The text in the file is: ", text,"\n")
blob = TextBlob(text) #TextBlob allows easy text processing, including sentiment analysis.
sentiment = blob.sentiment.polarity #-1 to 1 (negative to positive)
print("The sentiment score is ", sentiment,"\n")
if(sentiment>0):
print("The sentiments in the file are Positive","\n")
elif(sentiment<0):
print("The sentiments in the file are Negative", "\n")
else:
print("The sentiments in the file are Neutral", "\n")
def sentiment_analysis_from_article():
url = input("Enter the url of the article: ")
if(url== ""):
print("The url is not provided","\n")
exit()
article = Article(url)
article.download()
article.parse()
article.nlp()
text = article.summary
print("The text in the file is ", text,"\n")
blob = TextBlob(text) #TextBlob allows easy text processing, including sentiment analysis.
sentiment = blob.sentiment.polarity #-1 to 1 (negative to positive)
print("The sentiment score is ", sentiment,"\n")
if(sentiment>0):
print("The sentiments in the article are Positive","\n")
elif(sentiment<0):
print("The sentiments in the article are Negative", "\n")
else:
print("The sentiments in the article are Neutral", "\n")
if(option_choosen == 1):
sentiment_analysis_from_text_file()
elif(option_choosen == 2):
sentiment_analysis_from_article()