-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
486 lines (447 loc) · 13.8 KB
/
main.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import sys
import feedparser
import json
import pymongo
import subprocess
import time
import string
import urllib
import csv
import os
import itertools
import random
import mechanize
import datetime
from datetime import date, timedelta
from stem import Signal
from stem.control import Controller
import socks
import socket
from multiprocessing import Pool, Manager
from string import punctuation
from shutil import copyfile
import nltk
import os.path
import datetime
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from string import punctuation
from pandas import date_range
from pandas import bdate_range
from datetime import datetime
from jdcal import gcal2jd, jd2gcal
from pandas.tseries.offsets import DateOffset, BDay
from yahoo_finance import Share
try:
client = pymongo.MongoClient(connect = False)
cdb = client.Companydb
cdb.createCollection( "Companydb", { } )
except:
print "Company database already exists"
try:
ddb = client.Dictdb
ddb.createCollection( "Dictionarydb", { } )
except:
print "Dictionary database already exists"
def printfeeds (RSSfeeds):
for feed in RSSfeeds:
print feed.url
for entry in feed.entries:
print feed.url,
print entry.title,
print entry.author,
print entry.summary,
print entry.link,
print entry.published,
print time.time(),
print getsentiment(entry.summary)
return
def getimportantsentiment (statement, importantwords):
words = ''.join(c for c in statement if c not in punctuation).lower().split()
numwords = 0
x = 0
for word in words:
if ddb.dictionary.find({"word": word}).count() != 0:
numwords += 1
dictword = ddb.dictionary.find_one({"word": word.encode('utf-8')})
temp = 1
for importantword in importantwords:
if word == importantword:
temp = 2
x += temp*float(int(dictword["positive"])-int(dictword["negative"]))/int(dictword["seen"])
if numwords != 0:
x /= numwords
return x
def getsentiment (statement):
words = ''.join(c for c in statement if c not in punctuation).lower().split()
numwords = 0
x = 0
for word in words:
if ddb.dictionary.find({"word": word}).count() != 0:
numwords += 1
dictword = ddb.dictionary.find_one({"word": word.encode('utf-8')})
x += float(int(dictword["positive"])-int(dictword["negative"]))/int(dictword["seen"])
if numwords != 0:
x /= numwords
return x
def listsentiments ():
companies = getcompanies()
company = random.choice(companies)
print company[1]
articles = getarticles(company[1])
for article in articles:
print article + "\n" + str(getsentiment(article))
def printdictionary ():
for document in ddb.dictionary.find():
print document["word"]
print ddb.dictionary.find().count()
def resetdictionary ():
client.drop_database('Dictdb')
try:
ddb = client.Dictdb
ddb.createCollection( "Dictionarydb", { } )
except:
i = 0
# print "still fucked m80"
sentimentdictionary = open('sentimentdictionary', 'a')
sentimentdictionary.close()
sentimentdictionary = open('sentimentdictionary', 'r+')
lines = sentimentdictionary.readlines()
dictwords = []
for line in lines:
temp = line.rstrip('\n').split(',')
dictwords.append(temp)
sentimentdictionary.close()
for dictword in dictwords:
if ddb.dictionary.find({"word": dictword}).count() == 0:
post = {
"word": dictword[0],
"seen": dictword[1],
"positive": dictword[2],
"negative": dictword[3],
}
ddb.dictionary.insert_one(post)
return
def autotrain ():
gettime = time.time()
companies = getcompanies()
pool = Pool(processes=50)
client.close()
pool.imap_unordered(autotrainer, companies)
pool.close()
pool.join()
gettime = time.time() - gettime
print "Total time to complete: " + str(gettime)
print "now testing database"
for company in companies:
articles = getarticles(company[1])
for article in articles:
print article + "\n" + str(getsentiment(article))
def autotrainer (company):
client = pymongo.MongoClient()
ddb = client.Dictdb
articles = getarticles(company[1])
for article in articles:
articlesentiment = getsentiment(article)
if articlesentiment >= 1.0/3:
sentiment = 'p'
elif articlesentiment > -1.0/3:
sentiment = 'a'
else:
sentiment = 'n'
for word in article.split():
if ddb.dictionary.find({"word": word.encode('utf-8','ignore')}).count() == 0:
if sentiment == 'p':
post = {
"word": word.encode('utf-8','ignore'),
"seen": 1,
"positive": 1,
"negative": 0,
}
elif sentiment == 'n':
post = {
"word": word.encode('utf-8','ignore'),
"seen": 1,
"positive": 0,
"negative": 1,
}
else:
post = {
"word": word.encode('utf-8','ignore'),
"seen": 1,
"positive": 0,
"negative": 0,
}
ddb.dictionary.insert_one(post)
else:
dictword = json.load(ddb.dictionary.find_one({"word": word.encode('utf-8','ignore')}))
if sentiment == 'p':
ddb.dictionary.update_one({"word": word}, {"$set": {"seen": dictword.seen+1, "positive": dictword.positive+1, "negative": dictword.negative}})
elif sentiment == 'n':
ddb.dictionary.update_one({"word": word}, {"$set": {"seen": dictword.seen+1, "positive": dictword.positive, "negative": dictword.negative+1}})
else:
ddb.dictionary.update_one({"word": word}, {"$set": {"seen": dictword.seen+1, "positive": dictword.positive, "negative": dictword.negative}})
client.close()
return
def trainer ():
sentimentdictionary = open('sentimentdictionary', 'a')
sentimentdictionary.close()
sentimentdictionary = open('sentimentdictionary', 'r+')
lines = sentimentdictionary.readlines()
dictwords = []
for line in lines:
temp = line.rstrip('\n').split(',')
dictwords.append(temp)
companies = getcompanies()
random.shuffle(companies)
for company in companies:
articles = getarticles(company[1])
for article in articles:
sentiment = raw_input("\"" + article.encode("utf8","ignore") + "\"\n")
if sentiment == 'e':
break
for word in article.split():
exists = False
for dictword in dictwords:
if word.encode('utf-8','ignore') == dictword[0]:
exists = True
break
else:
continue
if exists == True:
dictword[1] = int(dictword[1]) + 1
if sentiment == 'p':
dictword[2] = int(dictword[2]) + 1
elif sentiment == 'n':
dictword[3] = int(dictword[3]) + 1
else:
if sentiment == 'p':
dictwords.append([word.encode('utf-8','ignore'), 1, 1, 0])
elif sentiment == 'n':
dictwords.append([word.encode('utf-8','ignore'), 1, 0, 1])
elif sentiment == 'a':
dictwords.append([word.encode('utf-8','ignore'), 1, 0, 0])
if sentiment == 'e':
break
sentimentdictionary.close()
os.remove('sentimentdictionary')
sentimentdictionary = open('sentimentdictionary', 'w')
for dictword in dictwords:
if ddb.dictionary.find({"word": dictword}).count() == 0:
post = {
"word": dictword[0],
"seen": dictword[1],
"positive": dictword[2],
"negative": dictword[3],
}
ddb.dictionary.insert_one(post)
else:
dictword = json.load(ddb.dictionary.find_one({"word": word.encode('utf-8','ignore')}))
if sentiment == 'p':
ddb.dictionary.update_one({"word": word}, {"$set": {"seen": dictword.seen+1, "positive": dictword.positive+1, "negative": dictword.negative}})
elif sentiment == 'n':
ddb.dictionary.update_one({"word": word}, {"$set": {"seen": dictword.seen+1, "positive": dictword.positive, "negative": dictword.negative+1}})
else:
ddb.dictionary.update_one({"word": word}, {"$set": {"seen": dictword.seen+1, "positive": dictword.positive, "negative": dictword.negative}})
print>>sentimentdictionary, dictword[0]+","+str(dictword[1])+","+str(dictword[2])+","+str(dictword[3])
sentimentdictionary.close()
return
def importantwords (ticker):
words = getwordsfordate(ticker, date.today().strftime('%Y-%m-%d'))
fst=''
stop_words = set(stopwords.words("english"))
filtered_words = [w for w in words if not w in stop_words]
return filtered_words
def deldb ():
client.drop_database('Companydb')
client.drop_database('Dictdb')
return
def getrating (ticker):
words = getwordsfordate(ticker, date.today().strftime('%Y-%m-%d'))
rating = 0
length = len(words)
imprtntwrds = importantwords(ticker)
rating += getimportantsentiment(" ".join(words),imprtntwrds);
if (length != 0):
rating = rating/length
return rating
def getstockchange (ticker):
yesterday = (date.today() - timedelta(1)).strftime('%Y-%m-%d')
try:
opn = Share(ticker).get_historical(yesterday, yesterday)[0][u'Open']
cls = Share(ticker).get_historical(yesterday, yesterday)[0][u'Close']
print opn
print cls
except:
return 0
return (float(clse)-float(opn))/float(opn);
def companyupdate (company):
client2 = pymongo.MongoClient(connect=False)
cdb = client2.Companydb
ddb = client2.Dictdb
if cdb.companies.find({"company": company[0]}).count() == 0:
rating = getrating(company[1])
post = {
"company": company[0],
"symbol": company[1],
"sector": company[2],
"industry": company[3],
"rating": rating,
"updated": time.time(),
}
cdb.companies.insert_one(post)
print "\"" + company[0] + "\" added. rating = " + str(rating)
else:
rating = getrating(company[1])
result = cdb.companies.update_one({"company": company}, {"$set": {"rating": rating, "updated": time.time()}})
print "\"" + company[0] + "\" already exists. New rating = " + str(rating)
client2.close()
def batchcompanyupdate ():
gettime = time.time()
pool = Pool(processes=50)
companies = []
companies = getcompanies()
print "A total of " + str(len(companies)) + " companies to update"
client.close()
pool.imap_unordered(companyupdate, companies)
gettime = time.time() - gettime
print "Total time to complete: " + str(gettime)
def getcompanies ():
companies = []
tempdir = r'./temp'
if not os.path.exists(tempdir):
os.makedirs(tempdir)
NASDAQlistings = open('nasdaq', 'r')
lines = NASDAQlistings.readlines()
for line in lines:
urllib.urlretrieve (line, "./temp/templistings.csv")
with open("./temp/templistings.csv", 'r') as doc:
reader = csv.reader(doc)
reader. next() #skip first line
for row in reader:
company = []
company.append(row[1]) #company name
company.append(row[0]) #company ticker
company.append(row[6]) #company sector
company.append(row[7]) #company industry
companies.append(company)
os.remove("./temp/templistings.csv")
return companies
def printcompanyinfo (name):
cursor = cdb.companies.find({"symbol": name})
for document in cursor:
print document
return
def gettext (ticker, js):
browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.addheaders= [('User-agent','Chrome')]
return browser.open("https://www.googleapis.com/customsearch/v1element?key=AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY&rsz=filtered_cse&num=10&hl=en&prettyPrint=false&source=gcsc&gss=.com&sig=432dd570d1a386253361f581254f9ca1&cx=004415538554621685521:vgwa9iznfuo&q=stocks:"+ticker+"%20daterange%3A"+js+"%2D"+js+"&googlehost=www.google.com&callback=google.search.Search.apiary9284&nocache=1459649736099").read()
def getarticlesfordate(ticker, day):
day=day.split('-')
year=day[0]
month=day[1]
day=day[2]
jday1= gcal2jd(year,month,day)
jday1= jday1[0]+jday1[1]+0.5
jday1= int(jday1)
js=str(jday1)
temp = socket.socket
socket.socket = socks.socksocket
txt = gettext(ticker, js)
socket.socket = temp
txt = txt[48:]
l= len(txt)
txt = txt[:l-2]
articles = []
for result in json.loads(txt)[u'results']:
article = []
article.append(result[u'titleNoFormatting'])
article.append(result[u'contentNoFormatting'])
articles.append(article)
return articles
def getwordsfordate(ticker, day):
day=day.split('-')
year=day[0]
month=day[1]
day=day[2]
jday1= gcal2jd(year,month,day)
jday1= jday1[0]+jday1[1]+0.5
jday1= int(jday1)
js=str(jday1)
temp = socket.socket
socket.socket = socks.socksocket
txt = gettext(ticker, js)
socket.socket = temp
txt = txt[48:]
l= len(txt)
txt = txt[:l-2]
words = ""
for result in json.loads(txt)[u'results']:
words += result[u'contentNoFormatting'] + " " + result[u'titleNoFormatting']
words =''.join(c for c in words if c not in punctuation+'-').lower()
return words.rstrip('\n').split()
def getarticles(ticker):
day=date.today().strftime('%Y-%m-%d').split('-')
year=day[0]
month=day[1]
day=day[2]
jday1= gcal2jd(year,month,day)
jday1= jday1[0]+jday1[1]+0.5
jday1= int(jday1)
js=str(jday1)
temp = socket.socket
socket.socket = socks.socksocket
txt = gettext(ticker, js)
socket.socket = temp
txt = txt[48:]
l= len(txt)
txt = txt[:l-2]
articles = []
for result in json.loads(txt)[u'results']:
article = result[u'contentNoFormatting'] + " " + result[u'titleNoFormatting']
article = ''.join(c.rstrip('\n') for c in article if c not in punctuation+'-').lower()
articles.append(article)
return articles
def getclosingpricefordate(ticker, day):
try:
close = Share(ticker).get_historical(day, day)[0][u'Close']
except:
close = 0
return close
def mainmenu ():
while True:
# try:
option = input( "Options:\n1: Recreate Database\n2: List feed urls\n3: reset dictionary\n4: Let the sentiment analyzer train itself\n5: Train sentiment analyzer\n6: Get info on a company\n7: Show sentiments\n8: Show current dictionary\n9: Update all the companies data\n10: Exit\n")
if option == 1:
deldb()
elif option == 2:
listfeeds()
elif option == 3:
resetdictionary()
elif option == 4:
autotrain()
elif option == 5:
trainer()
elif option == 6:
name = raw_input("Enter Company ticker: ")
printcompanyinfo(name)
elif option == 7:
listsentiments()
elif option == 8:
printdictionary()
elif option == 9:
batchcompanyupdate()
elif option == 10:
return
# except:
# print "error"
return
with Controller.from_port(port = 9051) as controller:
controller.authenticate()
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
# socket.socket = socks.socksocket
#initsystem()
mainmenu()
#for company in getcompanies():
# companyupdate (company)