Skip to content

Commit

Permalink
Abstracted out settings to a settings file. For now, some of the R co…
Browse files Browse the repository at this point in the history
…de lives while I debat where it should live.
  • Loading branch information
alexhenning committed Aug 4, 2011
1 parent 995e317 commit f4bd470
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
36 changes: 14 additions & 22 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
from rpy2 import robjects
from datetime import datetime

import settings as S

### Load R Libraries
r = robjects.r

# Executes `R-setup.R' in our local R environment
with file("R-setup.R") as f:
r(f.read())
with file(S.r_setup) as f: r(f.read())

### Twitter Interface
api = tweepy.API()
Expand All @@ -34,15 +35,9 @@ def getSentimentHist(queries):
print "Calculating histogram for: %s"%(queries,)
path = "images/"+str(datetime.now())+".png"
variables = [calcSentimentScores(query) for query in queries]
r_query = """\
allscores <- rbind(%(variables.scores)s)
ggplot(data=allscores) + geom_bar(mapping=aes(x=score, fill=Project),\
binwidth=1) + facet_grid(Project~.) + theme_bw()\
+ scale_fill_brewer()
ggsave(file="%(path)s")
dev.off()
"""%{"variables.scores": ", ".join([i+".scores" for i in variables]),
"path": path}
r_query = S.r_generate_graph%{"variables.scores": \
", ".join([i+".scores" for i in variables]),
"path": path}
print r_query
r(r_query)
return path
Expand All @@ -52,11 +47,9 @@ def calcSentimentScores(search):
varName = getFreeRName()
tweets = [tweet.text for tweet in get_tweets(search)]
tweets = robjects.StrVector(tweets if tweets else ["Neutral"])
r_query = """\
%(var)s.text <- %(tweet_text)s
%(var)s.scores <- score.sentiment(%(var)s.text, pos.words, neg.words)
%(var)s.scores$Project = "%(search)s"
"""%{"var": varName, "tweet_text": tweets.r_repr(), "search": search}
r_query = S.r_calculate_sentiment%{"var": varName,
"tweet_text": tweets.r_repr(),
"search": search}
print r_query
r(r_query)
return varName
Expand All @@ -71,16 +64,16 @@ def getFreeRName():
app = Bottle()

@app.route("/")
@view("twitter-sentiment-query.html")
@view(S.mainTemplate)
def twitterSentimentQuery():
"Returns the main page and handle form dat submits"
q = request.GET.get("q", None)
if q:
print "Getting graph"
graph = getSentimentHist([s.strip() for s in q.split(",")])
print "Path to graph: %s"%(graph)
return {"q": q if q else "happy, sad",
"graph": graph if q else "images/example.png"}
return {"q": q if q else S.defaultSearch,
"graph": graph if q else S.defaultImage}

@app.route("/static/:path")
def serveStaticMedia(path):
Expand All @@ -90,7 +83,6 @@ def serveStaticMedia(path):
@app.route("/images/:image")
def serveImage(image):
"Serves images when requested"
return static_file(image, root="images/")

return static_file(image, root=S.imagePath)
if __name__ == "__main__":
run(app, host="192.168.1.3", port=8348)
run(app, host=S.host, port=S.port)
38 changes: 38 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
### Server Settings ############################################################
# The host and port to bind the server to
host = "192.168.1.3"
port = 8348

### Application Settings #######################################################
# The path to save images in
imagePath = "images/"

# The default search to display in the textbox
defaultImage = "happy, sad"
# The default image to display when the showing the page with no query
defaultSearch = "images/example.png"

# The file containing the html template for the app
mainTemplate = "twitter-sentiment-query.html"


### R Code & Settings ###########################################################
# R file containing the setup code (load libraries and define score.sentiment)
r_setup = "R-setup.R"

# The R code for creating the list of sentiment scores from the tweets
r_calculate_sentiment = """\
%(var)s.text <- %(tweet_text)s
%(var)s.scores <- score.sentiment(%(var)s.text, pos.words, neg.words)
%(var)s.scores$Project = "%(search)s"
"""

# The R code for creating the histogram of the resulting scores
r_generate_graph = """\
allscores <- rbind(%(variables.scores)s)
ggplot(data=allscores) + geom_bar(mapping=aes(x=score, fill=Project),\
binwidth=1) + facet_grid(Project~.) + theme_bw()\
+ scale_fill_brewer()
ggsave(file="%(path)s")
dev.off()
"""

0 comments on commit f4bd470

Please sign in to comment.