-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.py
71 lines (65 loc) · 3.35 KB
/
worker.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
import sys
import os
sys.path.append("/app/app/emotion_tensorflow/")
import predict as emotionalRecognizer
sys.path.append("/app/app/twitterExtraction/")
import retrieveTwitterData as sentimentScorer
sys.path.append("/app/app/priceAnalysis/")
import retrievePriceData as priceAnalyzer
sys.path.append("/app/app/knnPredictor/")
def blockPrint():
sys.stdout = open(os.devnull, 'w')
sys.stderr = open(os.devnull, 'w')
blockPrint()
import knnPredictor as Predictor
from ffmpy import FFmpeg
# Restore
def enablePrint():
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
def extractVideo(video_f, directory):
ff = FFmpeg(
inputs={video_f: None},
outputs={directory: ['-r', '2']}
)
print "parsing video..."
blockPrint()
ff.run(stdout=sys.stdout, stderr=sys.stdout)
enablePrint()
if __name__ =="__main__":
enablePrint()
if len(sys.argv) < 2:
print "usage: python worker.py SYM_YYYY-MM-DD.mp4"
sys.exit()
if sys.argv[1] == "--help":
print "pass in a .mp4 video file int the following format SYM_YYYY-MM-DD.mp4"
print "worker will then parse the video file into its frames (2 fps by default)"
print "retrieveTwitterData module is then called and returns a twitter sentiment score [-1, 1] based the SYM and date (looks at seven day period starting from YYYY-MM-DD)"
print "emotionRecognizer module is then called and returns an emotional score [-1, 1] based on the frames found in the folder companies/SYM_YYYY-MM-DD"
print "knnPredictor module is then called which takes the previously trained data in results.csv, sentiment, and emotional score as inputs, and returns a class prediction and probability of correctness"
print "priceAnalyzer module is then called and takes as input the SYM and YYYY-MM-DD to determine the bullishness/bearishness of a stock over the 5-7 day period"
print "to recieve additional information on any particular module, please navigate to the folder and examine the descriptions in the individual ReadMe"
video_f = sys.argv[1]
company_sym = video_f.partition("_")[0]
date = video_f.partition("_")[2][:-4]
directory = "/app/app/emotion_tensorflow/companies/" + video_f[:-4]
try:
os.makedirs(directory)
except OSError as e:
print "{0} already exists".format(directory)
raise
extractVideo(video_f, directory + "/" + company_sym + "_image_sequence%06d.png")
enablePrint() #ensure that we can print
print "determing sentiment score for {0} during the week {1}...".format(company_sym, date)
sentiment_score = sentimentScorer.main(company_sym, date)
print "sentiment score: {0}".format(sentiment_score)
print "determining emotional score for frames found in {0}...".format(directory)
emotional_score = emotionalRecognizer.predict("companies/" + video_f[:-4])
print "emotional score: {0}".format(emotional_score)
prediction, probability = Predictor.KNN(sentiment_score, emotional_score)
print "you should {0} with probability {1}".format("BUY" if prediction == 1 else "NOT BUY", probability)
# determine actual value
#returns CompanyInfo object (defined in priceAnalyzer)
print "determing actual results..."
company = priceAnalyzer.analyzeTrends(company_sym, date)
print "actual result is to {0}".format("BUY" if company.getBinaryResult() == 1 else "NOT BUY")