-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_handling.py
55 lines (46 loc) · 2.04 KB
/
database_handling.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
# database_handling.py code:
import sqlite3
class FinalDatabase:
def __init__(self, db_name=":memory:"):
self.conn = sqlite3.connect(db_name)
self.cursor = self.conn.cursor()
self.create_tables()
def create_tables(self):
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS feedback_history (
id INTEGER PRIMARY KEY,
iteration INTEGER NOT NULL,
feedback TEXT NOT NULL,
selected_prompt TEXT NOT NULL,
added TEXT,
removed TEXT,
retained TEXT
)
''')
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS prompt_versions (
id INTEGER PRIMARY KEY,
iteration INTEGER NOT NULL,
prompt TEXT NOT NULL
)
''')
def store_feedback(self, iteration, feedback, selected_prompt):
self.cursor.execute("INSERT INTO feedback_history (iteration, feedback, selected_prompt) VALUES (?, ?, ?)",
(iteration, feedback, selected_prompt))
self.conn.commit()
def store_detailed_feedback(self, iteration, feedback, selected_prompt, added, removed, retained):
self.cursor.execute(
"INSERT INTO feedback_history (iteration, feedback, selected_prompt, added, removed, retained) VALUES (?, ?, ?, ?, ?, ?)",
(iteration, feedback, selected_prompt, added, removed, retained))
self.conn.commit()
def retrieve_word_scores(self):
self.cursor.execute("SELECT added, removed, retained FROM feedback_history")
data = self.cursor.fetchall()
word_scores = {}
for added, removed, retained in data:
for word in added.split(','):
word_scores[word] = word_scores.get(word, 0) + 1
for word in removed.split(','):
word_scores[word] = word_scores.get(word, 0) - 1
# Retained words can also have their scores modified if needed
return word_scores