QNLP  v1.0
QNLP.io.qnlp_db.qnlp_db Class Reference
Inheritance diagram for QNLP.io.qnlp_db.qnlp_db:
Inheritance graph
Collaboration diagram for QNLP.io.qnlp_db.qnlp_db:
Collaboration graph

Public Member Functions

def __init__ (self, db_name, db_path)
 
def create_table (self, table_name="qnlp")
 
def drop_table (self, table_name="qnlp")
 
def connect_db (self)
 
def close_db (self)
 
def db_insert (self, Dict values, data_type="noun", table_name="qnlp")
 
def db_load (self, data_type="noun", table_name="qnlp", direction="forward")
 
def db_print (self, table_name="qnlp")
 

Data Fields

 db_name
 
 db_path
 
 db_full_path
 
 db_conn
 

Detailed Description

Class for providing DB access and output from the corpus tagging operations.
The resulting file will then be loadable using the C++ QNLP code.

Definition at line 9 of file qnlp_db.py.

Constructor & Destructor Documentation

◆ __init__()

def QNLP.io.qnlp_db.qnlp_db.__init__ (   self,
  db_name,
  db_path 
)

Definition at line 14 of file qnlp_db.py.

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 

Member Function Documentation

◆ close_db()

def QNLP.io.qnlp_db.qnlp_db.close_db (   self)
If there is an active connection, close it.

Definition at line 85 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.db_conn, and QNLP.io.qnlp_db.qnlp_db.db_full_path.

◆ connect_db()

def QNLP.io.qnlp_db.qnlp_db.connect_db (   self)
If there is an active DB connection, return it. If not, create one.

Definition at line 71 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.db_conn, and QNLP.io.qnlp_db.qnlp_db.db_full_path.

Referenced by QNLP.io.qnlp_db.qnlp_db.create_table(), QNLP.proc.DisCoCat.qdb_mixin.create_table_discocat(), QNLP.io.qnlp_db.qnlp_db.db_insert(), QNLP.proc.DisCoCat.qdb_mixin.db_insert_discocat(), QNLP.io.qnlp_db.qnlp_db.db_load(), QNLP.io.qnlp_db.qnlp_db.db_print(), and QNLP.io.qnlp_db.qnlp_db.drop_table().

Here is the caller graph for this function:

◆ create_table()

def QNLP.io.qnlp_db.qnlp_db.create_table (   self,
  table_name = "qnlp" 
)
Create the database table for tagging the required data. The DB has columns
for type (noun, verb, etc.), bin_id (binary string representing the type),
weight (calculable from frequency of occurrence relative to other terms),
and mapping_dir (1 indicates string to bin_id, or -1 bin_id to string mapping).

Definition at line 22 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.connect_db(), QNLP.io.qnlp_db.qnlp_db.create_table(), and QNLP.io.qnlp_db.qnlp_db.drop_table().

Referenced by QNLP.io.qnlp_db.qnlp_db.create_table(), and QNLP.io.qnlp_db.qnlp_db.db_insert().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ db_insert()

def QNLP.io.qnlp_db.qnlp_db.db_insert (   self,
Dict  values,
  data_type = "noun",
  table_name = "qnlp" 
)
Insert the tag to binary encoding mapping into the DB.

values      -- Dict mapping string to binary value, and binary value to string.
data_type   -- String to indicate the type of data to be stored
table_name  -- Name of table to store in DB
The DB insert operation below assumes the value field of a key in DB is a tuple,
containing (binary_id, weight of occurrence), where weight of occurrence cant be
determined by the proximity of the word to other words; essentially a count in the 
simplest case. The mapping checks to see if the index is convertible to a numeric
type. If so, this will imply the reverse mapping (ie qubit result to string val), 
and is indicated by -1. Otherwise, this will be a forward mapping, and given by 1.

Definition at line 94 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.connect_db(), and QNLP.io.qnlp_db.qnlp_db.create_table().

Here is the call graph for this function:

◆ db_load()

def QNLP.io.qnlp_db.qnlp_db.db_load (   self,
  data_type = "noun",
  table_name = "qnlp",
  direction = "forward" 
)
Load the tag to binary encoded mapping into from DB.

data_type   -- Data type to load from the DB (noun, verb, etc)
table_name  -- Database table to load data
direction   -- Direction of the returned mapping (forward: tag -> int, reverse: int -> tag)

Definition at line 128 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.connect_db().

Here is the call graph for this function:

◆ db_print()

def QNLP.io.qnlp_db.qnlp_db.db_print (   self,
  table_name = "qnlp" 
)
Prints all the available data stored in the DB

Definition at line 159 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.connect_db().

Here is the call graph for this function:

◆ drop_table()

def QNLP.io.qnlp_db.qnlp_db.drop_table (   self,
  table_name = "qnlp" 
)
Drops the table if it exists.

Definition at line 57 of file qnlp_db.py.

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 

References QNLP.io.qnlp_db.qnlp_db.connect_db().

Referenced by QNLP.io.qnlp_db.qnlp_db.create_table(), and QNLP.proc.DisCoCat.qdb_mixin.create_table_discocat().

Here is the call graph for this function:
Here is the caller graph for this function:

Field Documentation

◆ db_conn

QNLP.io.qnlp_db.qnlp_db.db_conn

◆ db_full_path

QNLP.io.qnlp_db.qnlp_db.db_full_path

◆ db_name

QNLP.io.qnlp_db.qnlp_db.db_name

Definition at line 15 of file qnlp_db.py.

◆ db_path

QNLP.io.qnlp_db.qnlp_db.db_path

Definition at line 16 of file qnlp_db.py.


The documentation for this class was generated from the following file: