-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.py
163 lines (104 loc) · 3.86 KB
/
seed.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
"""Utility file to seed Sentiment_Analysis database"""
import csv
from datetime import datetime
from sqlalchemy import func
from model import User
from model import Quote
from model import Analyses
from model import Sentiment
from model import Classifier
from model import connect_to_db, db
from server import app
def load_sentiments(): # load sentiments first because other tables depend on this data
"""Load sentiments into database."""
sentiment1 = 'pos'
sentiment2 = 'neg'
#create instances of Sentiment class and pass table columns the data.
pos = Sentiment(sentiment=sentiment1)
neg = Sentiment(sentiment=sentiment2)
# # Adding data to the session
db.session.add(pos)
db.session.add(neg)
# Commiting data to database
db.session.commit()
def load_classifier():
"""Load classifier info into database."""
data_file = open('seed_data/classifier.csv', 'rU')
csv_file = csv.reader(data_file)
for row in csv_file:
tweet_content=row[0]
test_or_train=row[1]
sentiment_id=row[2]
classifier = Classifier(tweet_content=tweet_content, test_or_train=test_or_train, sentiment_id=sentiment_id)
# Adding data to the session
db.session.add(classifier)
# Commiting data to database
db.session.commit()
def load_quotes():
"""Load quote information into database."""
data_file = open('seed_data/quotes_2.csv', 'rU')
csv_file = csv.reader(data_file)
for row in csv_file:
content = row[0]
img_url = row[1]
author = row[2]
sentiment_id = row[3]
quote = Quote(content=content, img_url=img_url, author=author, sentiment_id=sentiment_id)
# Adding data to the session
db.session.add(quote)
# Commiting data to database
db.session.commit()
def load_users(): ### do I need this? No users initially?
"""Load users into database."""
# Read users file and insert data
for text in open("seed_data/users.csv"):
persons = text.split("\r")
for person in persons:
item = person.split(",")
user_name = item[0]
password = item[1]
twitter_handle = item[2]
email = item[3]
phone = item[4]
user = User(user_name=user_name, password=password, twitter_handle=twitter_handle, email=email, phone=phone)
# Adding data to the session
db.session.add(user)
# Commiting data to database
db.session.commit()
def load_analyses(): # no real analyses, since don't have users who have used web app, but adding one record for testing
"""Load analyses into database."""
# Read users file and insert data
data_file = open('seed_data/analyses.csv', 'rU')
csv_file = csv.reader(data_file)
for row in csv_file:
user_id = row[0]
timestamp = row[1]
tweet_sent_id = row[2]
quote_id = row[3]
analysis = Analyses(user_id=user_id, timestamp=timestamp, tweet_sent_id=tweet_sent_id, quote_id=quote_id)
# Adding data to the session
db.session.add(analysis)
# Commiting data to database
db.session.commit()
# def delete_all_ordered():
# # """Delete all rows in tables, so if we need to run this a second time,
# # we won't be trying to add duplicate users"""
# Classifier.query.delete()
# Analyses.query.delete()
# User.query.delete()
# Quote.query.delete()
# Sentiment.query.delete()
# # # Commiting deletions to database
# db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
# # # Delete any past info in tables to reload cleanly
# delete_all_ordered()
# # # In case tables haven't been created, create them
# db.create_all()
# # # Import different types of data
# load_sentiments()
# load_quotes()
# load_users()
# load_analyses()
# load_classifier()