-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary_db.py
114 lines (101 loc) · 3.5 KB
/
summary_db.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
import time
import sqlite3
import pandas as pd
# Set up sqllite3 db class.
class SummaryDB:
'''
A neat little DB class so we know it disconnects because __del__
'''
db_file = 'summaries.db'
table_name = 'summaries'
dt_pattern = '%Y-%m-%d %H:%M:%S'
def __init__(self):
'''
Get a connection to self.db_file and create a cursor
'''
self.connection = sqlite3.connect(self.db_file, check_same_thread=False)
self.cursor = self.connection.cursor()
self.get_or_create()
def list_tables(self):
qry = """
SELECT name FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE type='table'
ORDER BY name;
"""
return [x for x in self.cursor.execute(qry)]
def get_or_create(self):
tables = self.list_tables()
if len(tables) < 1:
self.create_table()
def save(self):
'''
Have to do this after everything apparently.
'''
self.connection.commit()
def create_table(self):
'''
Creates a table named self.table_name: default-> autolector.
'''
qry = f'''CREATE TABLE {self.table_name}
( date text,
article_id text,
article_text text,
summary text,
entry_src text,
rouge real,
meteor real
)'''
self.cursor.execute(qry)
def insert(self, article_id, article_text, summary):
'''
Insert question, context, answer, and timestamp into the table.
'''
date = time.strftime(self.dt_pattern)
qry = f"INSERT INTO {self.table_name} VALUES (?, ?, ?, ?, ?, ?, ?)"
entry_src = 'COMPUTER'
rouge, meteor = None, None
entry = (date, article_id, article_text, summary, entry_src, rouge, meteor)
self.cursor.execute(qry, entry)
self.save()
def update(self, article_id, article_text, summary, rouge, meteor):
'''
'''
date = time.strftime(self.dt_pattern)
qry = f"INSERT INTO {self.table_name} VALUES (?, ?, ?, ?, ?, ?, ?)"
entry_src = 'HUMAN'
entry = (date, article_id, article_text, summary, entry_src, rouge, meteor)
self.cursor.execute(qry, entry)
self.save()
def get_computer_summary(self, article_id):
qry = f"SELECT article_text, summary FROM {self.table_name} WHERE article_id='{article_id}' AND entry_src='COMPUTER' LIMIT 1"
row = [x for x in self.cursor.execute(qry)].pop()
return row
def get_all(self):
'''
Return all the rows in the self.table_name table.
'''
qry = f'SELECT * FROM {self.table_name}'
rows = [x for x in self.cursor.execute(qry)]
return rows
def get_df(self):
'''
Return a pandas DataFrame of the self.table_name table.
'''
qry = f'SELECT * FROM {self.table_name}'
df = pd.read_sql(qry, self.connection)
df['date'] = pd.to_datetime(df['date'])
return df.sort_values('date')
def drop_table(self):
'''
Drop table self.table_name from database.
'''
qry = f"DROP TABLE {self.table_name}"
self.cursor.execute(qry)
def __del__(self):
'''
The whole reason we are using OOP. Close connection automatically.
'''
self.save()
self.connection.close()