-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
113 lines (103 loc) · 4.08 KB
/
server.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
# TEAM #SENTIMENT - EECS 498 - Information Retrieval
# Main web server for the project.
# Used to serve up the interface and act as data controller for other modules.
#
# Web interface provided as JS and CSS in /webfiles
#
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer import ThreadingMixIn
from bing2URL import bing_search
from linksToCorpus import URLtoCorpus
from text2alchemy import giveMeSentiment
from twitterizer import twitterizer
import json
from emotionClass import getEmotion
twit = twitterizer()
# handle first response here
# return string (or otherwise)
def feelings(s):
print 'feeling module'
res = dict()
res['focus'] = giveMeSentiment(s)['focus']
emotion = getEmotion(s)
type = 'feeling'
res['emotion'] = formatOutput(emotion, 'feeling')
res['image'] = bing_search( res['focus'] ,'Image')
return res
def twitter(s):
print 'twitter module'
res = dict()
tweets = twit.top10Tweets( [s] )
s = ''.join(tweets)
emotion = getEmotion(s)
type = 'twitter'
res['emotion'] = formatOutput(emotion, 'twitter')
res['tweets'] = list()
for tweet in tweets:
res['tweets'].append( (tweet, getEmotion(tweet),) )
return res
def bing(s):
print 'bing module'
corpus = URLtoCorpus()
links = bing_search( s ,'Web')
corpus.numToRead = len(links)
corpus.openURLsfromlist(links)
s = corpus.wordsToString()
res = dict()
sentiment = giveMeSentiment(s)
res['score'] = sentiment['sentiment']['docSentiment']['score']
return res
'''
#love OR #attached OR #devotion', '#happy OR #elated',
'#amused OR #excited OR #firedup',
'#blessed OR #grateful', '#sad OR #depressed OR #heartbroken',
'#angry OR #mad OR #infuriated', '#afraid OR #scared OR #terrified',
'#humiliating OR #embarrassing OR #ashamed'''
def formatOutput(emotion, type):
if type == 'feeling':
if emotion == '#love OR #attached OR #devotion':
return ' love '
elif emotion == '#happy OR #elated':
return "'re happy about "
elif emotion == '#amused OR #excited OR #firedup':
return "'re excited about "
elif emotion == '#blessed OR #grateful':
return "'re grateful for "
elif emotion == '#sad OR #depressed OR #heartbroken':
return "'re sad about "
elif emotion == '#angry OR #mad OR #infuriated':
return "'re angry with "
elif emotion == '#afraid OR #scared OR #terrified':
return "'re scared of "
elif emotion == '#humiliating OR #embarrassing OR #ashamed':
return "'re embarrassed about "
else:
if emotion == '#love OR #attached OR #devotion':
return ' loves '
elif emotion == '#happy OR #elated':
return " is happy with "
elif emotion == '#amused OR #excited OR #firedup':
return " is excited about "
elif emotion == '#blessed OR #grateful':
return " is grateful for "
elif emotion == '#sad OR #depressed OR #heartbroken':
return " is sad about "
elif emotion == '#angry OR #mad OR #infuriated':
return " is angry with "
elif emotion == '#afraid OR #scared OR #terrified':
return " is scared of "
elif emotion == '#humiliating OR #embarrassing OR #ashamed':
return " is embarrassed about "
class Handler(SimpleHTTPRequestHandler):
def do_POST(self):
s = self.rfile.read( int(self.headers.getheader('content-length')) )
if self.path == '/feelings':
self.wfile.write( json.dumps( feelings(s)) )
elif self.path == '/twitter':
self.wfile.write( json.dumps( twitter(s) ))
elif self.path == '/bing':
self.wfile.write( json.dumps( bing(s) ))
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
ThreadedHTTPServer(('0.0.0.0', 3000), Handler).serve_forever()