-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
217 lines (143 loc) · 6.87 KB
/
model.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
"""Models for twitter election analysis project"""
from flask_sqlalchemy import SQLAlchemy
import datetime
db = SQLAlchemy()
################################################################################
#class definitions
class User(db.Model):
"""Twitter user"""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
handle = db.Column(db.String(80), nullable=False, unique=True)
################################################################################
class Candidate(db.Model):
"""Table for each candidate (both for president and vp)"""
__tablename__ = "candidates"
candidate_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String(15), nullable=False, unique=True)
full_name = db.Column(db.String(25), nullable=False)
position = db.Column(db.String(2), nullable=False)
party_affiliation = db.Column(db.String(10), nullable=False)
################################################################################
class Tweet(db.Model):
"""Individual tweet"""
__tablename__ = "tweets"
# Primary Key
tweet_id = db.Column(db.String(25),
unique=True,
primary_key=True)
# Foreign Key - User
user_id = db.Column(db.Integer,
db.ForeignKey(User.user_id),
nullable=False)
# Foreign Key - Candidate
referenced_candidate = db.Column(db.String(10),
db.ForeignKey(Candidate.name),
nullable=False)
# Additional attributes of each tweet
text = db.Column(db.String(400), nullable=False)
timestamp = db.Column(db.DateTime, nullable=False)
naive_bayes = db.Column(db.String(5), nullable=True)
profile_location = db.Column(db.String(100), nullable=True)
place_id = db.Column(db.String(30), nullable=True)
# Defining relationship to user (one user to many tweets)
user = db.relationship("User", backref=db.backref("tweets", order_by=tweet_id))
# Defining relationship to candidate (one candidate to many tweets)
candidate = db.relationship("Candidate", backref=db.backref("candidate", order_by=tweet_id))
# Defining relationship with keywords through association table (many-to-many)
keywords = db.relationship("Keyword",
secondary="tweet_keywords",
backref="keywords")
def __repr__(self):
return "<Tweet id={}>".format(tweet_id)
################################################################################
class Keyword(db.Model):
"""Specific campaign specific keywords with designated word affiliations"""
__tablename__ = "keywords"
keyword_id = db.Column(db.Integer,
autoincrement=True,
primary_key=True)
keyword = db.Column(db.String(30), nullable=False)
related_candidate = db.Column(db.String(10),
db.ForeignKey(Candidate.name),
nullable=False)
connotation = db.Column(db.String(10), nullable=True)
candidate = db.relationship("Candidate", backref=db.backref("keywords"))
################################################################################
class TweetKeyword(db.Model):
"""Association table joining tweets and their related keywords"""
__tablename__ = "tweet_keywords"
tweet_key_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
tweet_id = db.Column(db.String(25), db.ForeignKey(Tweet.tweet_id), nullable=False)
keyword_id = db.Column(db.Integer, db.ForeignKey(Keyword.keyword_id), nullable=False)
################################################################################
def example_data():
"""Create sample data for testing purposes"""
TweetKeyword.query.delete()
Keyword.query.delete()
Tweet.query.delete()
User.query.delete()
Candidate.query.delete()
# Creating users
user1 = User(handle='@generictrumpsupporter',
user_id=1)
user2 = User(handle='@genericclintonsupporter',
user_id=2)
user3 = User(handle='@mythicalswingvoter',
user_id=3)
# Creating candidates
clinton = Candidate(name='Clinton',
full_name='Hillary Clinton',
position='P',
party_affiliation='Democrat')
trump = Candidate(name='Trump',
full_name='Donald Trump',
position='P',
party_affiliation='Republican')
both = Candidate(name='Both',
full_name='Both',
position='P',
party_affiliation='Both')
# Creating tweets for each user
tweet1 = Tweet(tweet_id='1',
user_id=user1.user_id,
referenced_candidate='Trump',
text='Yay Trump! #MAGA',
timestamp=datetime.datetime.now(),
naive_bayes='pos',
profile_location='Jackson, Mississippi',
place_id=None)
tweet2 = Tweet(tweet_id='2',
user_id=user2.user_id,
referenced_candidate='Clinton',
text='So proud of our first woman nominee, Hillary Clinton! #HillYes',
timestamp=datetime.datetime.now(),
naive_bayes='pos',
profile_location='Manhattan, NY',
place_id=None)
tweet3 = Tweet(tweet_id='3',
user_id=user3.user_id,
referenced_candidate='Both',
text='I hate both of the candidates #CrookedHillary #DumpTrump',
timestamp=datetime.datetime.now(),
naive_bayes='neg',
profile_location='Denver, CO',
place_id=None)
db.session.add_all([user1, user2, user3, clinton, trump, both, tweet1, tweet2, tweet3])
db.session.commit()
################################################################################
def connect_to_db(app, database='postgresql:///sentiments'):
"""Connect the database to our Flask app."""
# Configure to use PostgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = database
db.app = app
db.init_app(app)
################################################################################
# TO IMPLEMENT: Table that checks when the last time you collected data would be
## if it has been long enough, starts collecting data
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print "Connected to DB."