5 from typing
import Dict, Tuple
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. 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). 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)
41 except sqlite3.OperationalError
as oe:
42 remove_db = input(
"Table '{}' already exists. Remove? y/n: ".format(table_name))
47 except Exception
as e:
48 print(
"SQLITE exception thrown: {0}".format(e),
"Exiting program.")
58 """Drops the table if it exists.""" 59 dr_tbl =
"""DROP TABLE if EXISTS {};""".format(table_name)
66 except Exception
as e:
67 print(
"DB access error: {0}".format(e))
72 """If there is an active DB connection, return it. If not, create one.""" 79 except Exception
as e:
80 print(
"DB access error: {0}".format(e))
86 """If there is an active connection, close it.""" 94 def db_insert(self, values: Dict, data_type=
"noun", table_name=
"qnlp"):
96 Insert the tag to binary encoding mapping into the DB. 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 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. 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 )
122 print (data_type, k, int(v,2), 0 )
128 def db_load(self, data_type="noun", table_name="qnlp", direction="forward"):
130 Load the tag to binary encoded mapping into from DB. 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) 140 c.execute(
'''SELECT name, bin_id FROM {} WHERE type=?'''.format(table_name), (data_type,) )
142 all_rows = c.fetchall()
146 if( direction ==
"forward" ):
147 dat.update({r[0] : [r[1]]})
148 elif( direction ==
"reverse" ):
150 dat.update({r[1] : r[0]})
152 print(
"Direction not understood. Please try again")
160 """Prints all the available data stored in the DB""" 165 c.execute(
'''SELECT type, name, bin_id, weight FROM {}'''.format(table_name),
169 print(
"################################################################")
170 print(
"type\t\tbin_id\t\tweight\t\tmapping_dir")
171 print(
"################################################################")
172 all_rows = c.fetchall()
174 print(
'{0}\t\t{1}\t\t{2}\t\t{3}'.format(row[0], row[1], row[2], row[3] ))
def db_load(self, data_type="noun", table_name="qnlp", direction="forward")
def drop_table(self, table_name="qnlp")
def db_print(self, table_name="qnlp")
def __init__(self, db_name, db_path)
def db_insert(self, Dict values, data_type="noun", table_name="qnlp")
def create_table(self, table_name="qnlp")