QNLP  v1.0
qnlp_db.py
Go to the documentation of this file.
1 
2 
3 import sqlite3
4 import os
5 from typing import Dict, Tuple
6 
7 
8 
9 class qnlp_db:
10  """
11  Class for providing DB access and output from the corpus tagging operations.
12  The resulting file will then be loadable using the C++ QNLP code.
13  """
14  def __init__(self, db_name, db_path):
15  self.db_name = db_name
16  self.db_path = db_path
17  self.db_full_path = os.path.join(db_path, db_name)
18  self.db_conn = None
19 
20 
21 
22  def create_table(self, table_name="qnlp"):
23  """
24  Create the database table for tagging the required data. The DB has columns
25  for type (noun, verb, etc.), bin_id (binary string representing the type),
26  weight (calculable from frequency of occurrence relative to other terms),
27  and mapping_dir (1 indicates string to bin_id, or -1 bin_id to string mapping).
28  """
29  cr_tbl = """CREATE TABLE {}(
30  id INTEGER PRIMARY KEY, type TEXT,
31  name TEXT, bin_id INTEGER,
32  weight REAL, mapping_dir INTEGER
33  );""".format(table_name)
34 
35  conn = self.connect_db()
36  c = conn.cursor()
37 
38  try:
39  c.execute(cr_tbl)
40 
41  except sqlite3.OperationalError as oe:
42  remove_db = input("Table '{}' already exists. Remove? y/n: ".format(table_name))
43  if remove_db is "y":
44  self.drop_table(table_name)
45  self.create_table(table_name)
46 
47  except Exception as e:
48  print("SQLITE exception thrown: {0}".format(e), "Exiting program.")
49  exit()
50 
51  finally:
52  conn.commit()
53 
54 
55 
56 
57  def drop_table(self, table_name="qnlp"):
58  """Drops the table if it exists."""
59  dr_tbl = """DROP TABLE if EXISTS {};""".format(table_name)
60  try:
61  conn = self.connect_db()
62  c = conn.cursor()
63  c.execute(dr_tbl)
64  conn.commit()
65 
66  except Exception as e:
67  print("DB access error: {0}".format(e))
68 
69 
70 
71  def connect_db(self):
72  """If there is an active DB connection, return it. If not, create one."""
73  if self.db_conn is not None:
74  return self.db_conn
75  try:
76  self.db_conn = sqlite3.connect(self.db_full_path+".sqlite")
77 
78  return self.db_conn
79  except Exception as e:
80  print("DB access error: {0}".format(e))
81  return None
82 
83 
84 
85  def close_db(self):
86  """If there is an active connection, close it."""
87  if self.db_conn:
88  self.db_conn.close()
89  self.db_conn = None
90  print("Database %s closed" % self.db_full_path)
91 
92 
93 
94  def db_insert(self, values: Dict, data_type="noun", table_name="qnlp"):
95  """
96  Insert the tag to binary encoding mapping into the DB.
97 
98  values -- Dict mapping string to binary value, and binary value to string.
99  data_type -- String to indicate the type of data to be stored
100  table_name -- Name of table to store in DB
101  """
102 
103  #Proposed modification; weight set to 0 currently
104  '''
105  The DB insert operation below assumes the value field of a key in DB is a tuple,
106  containing (binary_id, weight of occurrence), where weight of occurrence cant be
107  determined by the proximity of the word to other words; essentially a count in the
108  simplest case. The mapping checks to see if the index is convertible to a numeric
109  type. If so, this will imply the reverse mapping (ie qubit result to string val),
110  and is indicated by -1. Otherwise, this will be a forward mapping, and given by 1.
111  '''
112  conn = self.connect_db()
113  c = conn.cursor()
114  self.create_table(table_name)
115 
116  for k,v in values.items():
117  if not str(k).isnumeric():
118  c.execute('''INSERT INTO {}
119  (type, name, bin_id, weight ) VALUES(?,?,?,?)'''.format(table_name),
120  (data_type, k, int(v,2), 0 )
121  )
122  print (data_type, k, int(v,2), 0 )
123  conn.commit()
124 
125 
126 
127 
128  def db_load(self, data_type="noun", table_name="qnlp", direction="forward"):
129  """
130  Load the tag to binary encoded mapping into from DB.
131 
132  data_type -- Data type to load from the DB (noun, verb, etc)
133  table_name -- Database table to load data
134  direction -- Direction of the returned mapping (forward: tag -> int, reverse: int -> tag)
135  """
136 
137  conn = self.connect_db()
138  c = conn.cursor()
139 
140  c.execute('''SELECT name, bin_id FROM {} WHERE type=?'''.format(table_name), (data_type,) )
141 
142  all_rows = c.fetchall()
143  dat = {}
144 
145  for r in all_rows:
146  if( direction == "forward" ):
147  dat.update({r[0] : [r[1]]})
148  elif( direction == "reverse" ):
149  #No need to carry second term as list for reverse mapping
150  dat.update({r[1] : r[0]})
151  else:
152  print("Direction not understood. Please try again")
153  exit()
154 
155  return dat
156 
157 
158 
159  def db_print(self, table_name="qnlp"):
160  """Prints all the available data stored in the DB"""
161 
162  conn = self.connect_db()
163  c = conn.cursor()
164 
165  c.execute('''SELECT type, name, bin_id, weight FROM {}'''.format(table_name),
166  # WHERE type=? AND bin_id=? AND mapping=?''',
167  # ()
168  )
169  print("################################################################")
170  print("type\t\tbin_id\t\tweight\t\tmapping_dir")
171  print("################################################################")
172  all_rows = c.fetchall()
173  for row in all_rows:
174  print('{0}\t\t{1}\t\t{2}\t\t{3}'.format(row[0], row[1], row[2], row[3] ))
175 
176 
def db_load(self, data_type="noun", table_name="qnlp", direction="forward")
Definition: qnlp_db.py:128
def drop_table(self, table_name="qnlp")
Definition: qnlp_db.py:57
def db_print(self, table_name="qnlp")
Definition: qnlp_db.py:159
def __init__(self, db_name, db_path)
Definition: qnlp_db.py:14
def connect_db(self)
Definition: qnlp_db.py:71
def db_insert(self, Dict values, data_type="noun", table_name="qnlp")
Definition: qnlp_db.py:94
def create_table(self, table_name="qnlp")
Definition: qnlp_db.py:22
def close_db(self)
Definition: qnlp_db.py:85