forked from aahluwal/Where-Are-You-Tweeting-From-
/
twitter_map.py
91 lines (73 loc) · 2.82 KB
/
twitter_map.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
from flask import Flask, flash, render_template, redirect, request, session, url_for,g
#from model import session as db_session, Tweet
from flask.ext.sqlalchemy import SQLAlchemy
#import model
import data
import datetime
from data import cities
import os
import feature_selection
app = Flask(__name__)
app.config.from_object(__name__)
app.secret_key = 'some_key'
@app.route("/")
def index():
tweet_city_dict = data.map_cities_to_tweets()
return render_template("home.html", tweet_city_dict=tweet_city_dict)
@app.route("/about_project")
def project_page():
return render_template("about_project.html")
@app.route("/classify_text", methods=["POST"])
def classify_text():
tweet = request.form['tweet']
start = datetime.datetime.now()
rankings = data.create_ranking(tweet)
end = datetime.datetime.now()
print 'getting city rankings takes: %s' % (end - start)
start = datetime.datetime.now()
top_5_words = feature_selection.top_words_in_tweet(rankings[0][0],tweet)
end = datetime.datetime.now()
print 'getting top 5 words takes: %s' % (end - start)
start = datetime.datetime.now()
cty_corpus_dict = data.city_corpus_dict()
word_count_dict = cty_corpus_dict[rankings[0][0].name]
end = datetime.datetime.now()
print 'getting bogus word count dict takes: %s' % (end - start)
start = datetime.datetime.now()
final_result = []
for word in top_5_words:
final_result.append(word)
names = []
for i in range(0, len(rankings)):
city_name = rankings[i][0].name
names.append(city_name)
end = datetime.datetime.now()
print 'generating lists takes: %s' % (end - start)
return render_template("map.html", tweet=tweet, names=names, rankings=rankings, final_result=final_result)
@app.route("/classify_text", methods=["GET"])
def classify():
return redirect(url_for("index"))
@app.route("/top_words", methods=["GET"])
def feature_select():
cities = data.cities
return render_template("features.html", cities=cities)
@app.route("/city/<city_name>", methods=["GET"])
def list_features(city_name):
cities = data.cities
city = None
for i in range(0, len(cities)):
if cities[i].name == city_name:
feature_list = feature_selection.get_hardcoded_features(cities[i])
city = cities[i]
if city:
latitude = city.lat
longitude = city.lon
city_name = city.name
city_tweet_count = data.create_region_tweet_count(city)
city_word_count = data.find_leng_city_corpus(city)
return render_template("city_words.html", features= feature_list, city_name=city_name, latitude=latitude, longitude=longitude, city_tweet_count=city_tweet_count, city_word_count=city_word_count)
@app.route("/part1/gathering_tweets")
def gathering_tweets_tutorial():
return render_template("gathering_tweets_tutorial.html")
if __name__ == "__main__":
app.run(debug=True)