QNLP  v1.0
corpus_utils.cpp
Go to the documentation of this file.
1 
9 #include "corpus_utils.hpp"
10 #include "db_helper.hpp"
11 
12 using namespace QNLP;
13 
14 //If no filename is given, choose the database name from the macro/compiler defined value
16  db_helper(QNLP_DB_FILE), database_filename(QNLP_DB_FILE) {}
17 
18 //Otherwise, take the given string name
19 CorpusUtils::CorpusUtils(const std::string filename) :
20  db_helper(filename), database_filename(filename) {}
21 
24 }
25 
27  return this->name_to_bin;
28 }
29 
31  return this->bin_to_name;
32 }
33 
34 void CorpusUtils::loadData(const std::string data_type, const std::string table){
35  int rc;
36  sqlite3_stmt *select_stmt = NULL;
38 
39  std::string query = "select name, bin_id from " + table + " where type=?";
40  SQL_RETURN_CHECK ( sqlite3_prepare_v2( getDBHelper().getDBRef(), query.c_str(), -1, &select_stmt, NULL ), SQLITE_OK );
41 
42  //Arg 2 is the 1-based index of the variable to replace
43  //Arg 4 is used to give the number of bytes, where -1 takes the length of string
44  //Arg 5 specifies the destructor type, where SQLITE_STATIC causes it to ignore.
45  SQL_RETURN_CHECK( sqlite3_bind_text( select_stmt, 1, data_type.c_str(), -1, SQLITE_STATIC ), SQLITE_OK );
46 
47  //Iterate through the rows of returned values
48  while ( (rc = sqlite3_step(select_stmt)) == SQLITE_ROW) {
49  std::string k( reinterpret_cast<const char*> ( sqlite3_column_text(select_stmt, 0) ) );
50  unsigned int v = (unsigned int) sqlite3_column_int(select_stmt, 1);
51  name_to_bin[data_type][k] = v;
52  bin_to_name[data_type][v] = k;
53  }
54  SQL_RETURN_CHECK( sqlite3_finalize(select_stmt), SQLITE_OK );
55 }
56 
57 void CorpusUtils::printData(const std::string type, const std::string table="corpus"){
58  if(! this->name_to_bin.size()){
59  loadData(type, table);
60  }
61  for(auto& dat : this->name_to_bin[type]){
62  std::cout << dat.first << " : " << dat.second << std::endl;
63  }
64 }
65 
67  return this->database_filename;
68 }
69 
71  this->bin_to_name.clear();
72  this->name_to_bin.clear();
73 }
74 
76  return this->db_helper;
77 }
std::unordered_map< std::string, std::unordered_map< unsigned int, std::string > > BTN
Database access and manipulation functions.
NTB & getNameToBin()
Get the NTB object. This is a map of maps, wherein the first key represents the grammatical type to l...
std::unordered_map< std::string, std::unordered_map< std::string, unsigned int > > NTB
void printData(const std::string type, const std::string table)
Print the data with given type.
#define QNLP_DB_FILE
std::string database_filename
CorpusUtils()
Construct a new Load Corpus object. Database name taken from the macro/compiler defined value.
BTN & getBinToName()
Get the BTN object. This is a map of maps, wherein the first key represents the grammatical type to l...
#define SQL_RETURN_CHECK(x, y)
Definition: db_helper.hpp:19
void loadData(const std::string data_type, const std::string table="corpus")
Loads from the database for a given data type (noun, verb, etc)
const std::string get_database_filename()
Get the database filename object.
int openDB(const std::string filename)
Opens the requested database, and gives the return code of the operation. If the DB pointer has alrea...
Definition: db_helper.cpp:32
void clearData()
Clear all loaded data.
DBHelper & getDBHelper()
Return reference to database object.