-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.py
213 lines (170 loc) · 7.09 KB
/
Engine.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import requests
import nltk
from nltk.stem import *
import Lexicon
import Naive_Bayes
import matplotlib.pyplot as plt
# TODO: Remove Lexicon Method
# TODO: Add Testing IMDB to NB training
# TODO: Add WSJ Annotated Sentiment Articles
# TODO: Implement K-Means 3D Matrix and 2D Matrix
# TODO: DONE2
def stripper(sentence):
# strips sentence of unnecessary POS and Chars
func_pos = ['CD', 'DT', 'EX', 'IN', 'LS', 'POS', 'NNP', 'NNPS', 'PRP', 'PRP$', 'RP', 'TO', 'WDT',
'WP', 'WRB', 'WP$', '.', ',', '(', ')', ':', ';', '"', "'", '$', '#']
sentence = nltk.word_tokenize(sentence)
pos_sent = nltk.pos_tag(sentence)
stripped_sentence = ''
for current in pos_sent:
if(current[1] not in func_pos):
ps = PorterStemmer()
word = ps.stem(current[0])
stripped_sentence += word + " "
return stripped_sentence
# TODO: DONE
def report(company, today, month, year, data, artData):
outputData = ""
outputData += company.title() + '\n'
outputData += "Dates: " + month + "/1/" + year + " to " + month + "/" + today + "/" + year + "\n"
outputData += data
file = open("./Report/" + company + "_report.txt", "w")
file.write(outputData)
file.close()
file = open("./Report/" + company + "_articles.txt", "w")
file.write(artData)
file.close()
# TODO: DONE
def suggestion(lexPOS, lexNEG, nbPOS, nbNEG):
POS = (lexPOS + nbPOS) / 2
NEG = (lexNEG + nbNEG) / 2
TOTAL = (nbPOS + nbNEG + lexNEG + lexPOS) / 2
difference = POS - NEG
if(POS == 0):
POS = 1
if(NEG == 0):
NEG = 1
if(TOTAL > 80):
if (difference == 0):
result = "Neutral Sentiment. Stocks will not change very much."
elif (difference > 0):
percent = POS / NEG
if(percent > 1.2):
result = "Positive Sentiment. Stock prices will increase."
else:
result = "Insufficient Information (Positive not 20% Greater). Can not make suggestion."
else:
percent = NEG / POS
if(percent > 1.2):
result = "Negative Sentiment. Stock prices will decrease."
else:
result = "Insufficient Information (Negative not 20% Greater). Can not make suggestion."
else:
result = "Insufficient Information (Not Enough Articles). Can not make suggestion."
return result
# TODO: DONE
def graph(days, month, year, company, lexPOS, lexNEG, nbPOS, nbNEG):
plt.plot(days, lexPOS, color='g')
plt.plot(days, lexNEG, color='r')
plt.ylim(0, 100)
plt.xlabel('Days of Month: ' + str(month) + ' and Year: ' + str(year))
plt.ylabel('Number of Sentiment Articles')
plt.title("Lexicon Sentiment Score for " + company.title())
plt.show()
plt.plot(days, nbPOS, color='g')
plt.plot(days, nbNEG, color='r')
plt.ylim(0, 100)
plt.xlabel('Days of Month: ' + str(month) + ' and Year: ' + str(year))
plt.ylabel('Number of Sentiment Articles')
plt.title("Naive Bayes Sentiment Score for " + company.title())
plt.show()
# TODO: DONE
def newsArticles(company, year, month, day):
# collects all news articles of a given company from the first of the month to given date
# api_key = "77e78b0d43684afca35a38bd91559742"
url = ('https://newsapi.org/v2/'
'everything?'
'sources=the-wall-street-journal, bbc-news, bloomberg, business-insider, abc-news, cbs-news, fortune, '
'cnn, nbc-news, the-economist, the-new-york-times, the-washington-post, '
'techradar, mashable, engadget, techcrunch, wired, recode, the-verge'
'&q=' + company +
'&language=en'
'&from=' + year + '-' + month + '-' + day +
'&to=' + year + '-' + month + '-' + day +
'&pageSize=100'
'&apiKey=77e78b0d43684afca35a38bd91559742')
response = requests.get(url)
all_articles = response.json()["articles"]
article_db = []
for article in all_articles:
# article data: title, source, description, url, publishedAt
description = stripper(article['description'])
article_db.append(description)
return article_db
# TODO: DONE
def runLexicon(articles, sentLex):
# generate dictionary containing articles and their scores
articleScores = Lexicon.allScores(articles, sentLex)
# get the count of POS/NEG/NEUt articles
POS, NEG, NEUT = Lexicon.categorizeScores(articleScores)
return POS, NEG, NEUT
# TODO: DONE
def runNaiveBayes(articles, freqPOS, freqNEG, countPOS, countNEG, countPOSNEG):
POS, NEG, NEUT = Naive_Bayes.categorizeScores(articles, freqPOS, freqNEG, countPOS, countNEG, countPOSNEG)
return POS, NEG, NEUT
# TODO: DONE
def engine():
###########################################
# EDIT HERE: Input company and dates to search
###########################################
company = 'facebook'
year = '2018'
month = '05'
today = 11
# gather lexicon/naive bayes trained data
sentLex = Lexicon.sentLexicon()
freqPOS, freqNEG = Naive_Bayes.frequency()
countPOS, countNEG, countPOSNEG = Naive_Bayes.count(freqPOS, freqNEG)
# reporting data
data = ""
artData = ""
# graphing data
days = []
lexPOSGraph = []
lexNEGGraph = []
nbPOSGraph = []
nbNEGGraph = []
# gather articles and analyze sentiment for entire month up to date
for day in range(1, today + 1):
day = str(day)
articles = newsArticles(company, year, month, day)
# article data for report
artData += 'Company: {}, Month: {}, Day: {}, Year: {}\n\n'.format(company, month, day, year)
for article in articles:
artData += article + "\n\n"
lexPOS, lexNEG, lexNEUT = runLexicon(articles, sentLex)
nbPOS, nbNEG, nbNEUT = runNaiveBayes(articles, freqPOS, freqNEG, countPOS, countNEG, countPOSNEG)
result = suggestion(lexPOS, lexNEG, nbPOS, nbNEG)
# graphing data
days.append(day)
lexPOSGraph.append(lexPOS)
lexNEGGraph.append(lexNEG)
nbPOSGraph.append(nbPOS)
nbNEGGraph.append(nbNEG)
# display data
print('Company: {}, Month: {}, Day: {}, Year: {}'.format(company, month, day, year))
print("Lexicon - POS: {}, NEG: {}, NEUT: {}".format(lexPOS, lexNEG, lexNEUT))
print("Naive Bayes - POS: {}, NEG: {}, NEUT: {}".format(nbPOS, nbNEG, nbNEUT))
print("Results: " + result)
print("______________________________________________________")
# sentiment data for report
data += '\nMonth: {}, Day: {}, Year: {}\n'.format(month, day, year)
data += "Results: " + result + "\n"
data += "Lexicon - POS: {},\tNEG: {},\tNEUT: {}\n".format(lexPOS, lexNEG, lexNEUT)
data += "Naive Bayes - POS: {},\tNEG: {},\tNEUT: {}\n\n".format(nbPOS, nbNEG, nbNEUT)
data += "______________________________________________________\n"
graph(days, month, year, company, lexPOSGraph, lexNEGGraph, nbPOSGraph, nbNEGGraph)
print("Graphs generated...")
report(company, str(today), month, year, data, artData)
print("Report generated...")
engine()