forked from edbullen/NLPBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_setup.py
99 lines (82 loc) · 3.03 KB
/
database_setup.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
import os
import warnings
warnings.filterwarnings("ignore") # suppress warnings when we try to drop non-existant tabs
import utils # General utils including config params and database connection
conf = utils.get_config()
DBFILE = conf["Sqlite"]["filepath"]
def try_drop(cursor,table_name):
SQL = 'DROP TABLE IF EXISTS ' + table_name
print(SQL)
cursor.execute(SQL)
print("Configuring Tables for database: {0}".format(DBFILE))
print("\n** ALL EXISTING TABLES AND DATA WILL BE LOST **\n")
response = utils.query_yes_no("Continue?")
if response:
print("Connecting to database...", end=" ")
connection = utils.db_connection(DBFILE)
cursor = connection.cursor()
print("connected.")
print("\nCreating words table:")
try:
try_drop(cursor, "words")
SQL = 'CREATE TABLE words ( hashid TEXT UNIQUE, word TEXT UNIQUE )'
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nCreating sentences table:")
try:
try_drop(cursor, "sentences")
SQL = 'CREATE TABLE sentences (hashid TEXT UNIQUE, sentence TEXT, used INT DEFAULT 0 NOT NULL)'
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nCreating statements table:")
try:
try_drop(cursor, "statements")
SQL = 'CREATE TABLE statements (sentence_id TEXT, word_id TEXT, class TEXT)'
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nAdding statements table INDEXES:")
try:
indexes = ['CREATE INDEX statements_sentenceid_idx ON statements(sentence_id)',
'CREATE INDEX statements_wordid_idx ON statements(word_id)']
for index in indexes:
SQL = index
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nCreating associations table:")
try:
try_drop(cursor, "associations")
SQL = 'CREATE TABLE associations (word_id TEXT NOT NULL, sentence_id TEXT NOT NULL, weight REAL NOT NULL)'
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nAdding associations table INDEXES:")
try:
indexes = ['CREATE INDEX associations_wordid_idx ON associations(word_id)',
'CREATE INDEX associations_sentenceid_idx ON associations(sentence_id)']
for index in indexes:
SQL = index
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nCreating results table:")
try:
try_drop(cursor, "results")
SQL = 'CREATE TABLE results (sentence_id TEXT, sentence TEXT, weight REAL)'
print(SQL)
cursor.execute(SQL)
except Exception as e:
print("\n** ERROR **", e)
print("\nDone.")
##############################
else:
exit(0)