Natural Language Processing  0.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
CorpusQueryGenerator.h
1 
8 #ifndef CORPUSQUERYGENERATOR_H
9 #define CORPUSQUERYGENERATOR_H
10 #include "../../Grammatica/Grammar Structure/Grammar-with-Map/cfgzero.h"
11 #include "../../Grammatica/Syntax-Tree/parser.h"
12 #include "../../Grammatica/Syntax-Tree/syntaxtree.h"
13 #include "../../CONFIG/config.h"
14 #include "../../CONFIG/nlp_exception.h"
15 
16 using namespace NLP;
17 
29 {
30  string mInsertHeader;
31 
32 public:
35  {
36  mInsertHeader = "INSERT INTO ontology ";
37  }
38 
39  string getQueryOf (const SyntaxTree& sentence);
40  string getInsertionQuery(const vector<SyntaxWord> &WordList);
41  string getQuestionQuery (const vector<SyntaxWord> &WordList, const SyntaxObject& SObjBeingAsked);
42 }; /* ----- end of class QueryGenerator ----- */
43 
44 /********************************************
45  * Class Implementation Of QueryGenerator *
46  ********************************************/
54 string QueryGenerator::getQueryOf(const SyntaxTree &sentence)
55 {
57  switch (sentence.getSentenceType ()) {
58  case SentenceType::ST_INVALID:
59  throw invalid_sentence();
60  break;
61  case SentenceType::DECLARATIVE:
62  return getInsertionQuery(sentence.getAll());
63  break;
64  case SentenceType::INTERROGATIVE:
65  return getQuestionQuery (sentence.getAll (), sentence.askingFor ());
66  break;
67  case SentenceType::IMPERATIVE:
68  throw unimplemented_exc();
69  break;
70  default:
71  break;
72  }
73 }
74 
82 string QueryGenerator::getInsertionQuery(const vector<SyntaxWord>& WordList)
83 {
84  set<SyntaxObject> SPO; // SPO components to put on table
85  string columnToFill;
86  string valueToFill;
87 
88  for(SyntaxWord const& w : WordList)
89  {
90  switch (w.getSyntax ()) {
91  case SyntaxObject::SUBJECT:
92  case SyntaxObject::MAINVERB:
93  case SyntaxObject::INDIRECTOBJ:
94  case SyntaxObject::DIRECTOBJ:
95 // case SyntaxObject::ATTRIBUTE: // Should be adjective
96  columnToFill += syntaxDBLookUp[w.getSyntax ()];
97  columnToFill += ",";
98  valueToFill += "'";
99  valueToFill += w.getWord ().getName ();
100  valueToFill += "',";
101  break;
102  default:
103  break;
104  }
105  }
106  columnToFill = columnToFill.substr (0, columnToFill.size () - 1);
107  valueToFill = valueToFill. substr (0, valueToFill.size () - 1);
108  string result = mInsertHeader + "(" + columnToFill + ")" + " VALUES ("
109  + valueToFill + ");";
110  return result;
111 }
112 
119 string QueryGenerator::getQuestionQuery(const vector<SyntaxWord> &WordList, const SyntaxObject &SObjBeingAsked)
120 {
121 // cout << "--DEBUG" << endl;
122 // cout << "syntax encoding : " << SyntaxTree::getSyntaxEncoding (WordList) << endl;
123 // cout << "Being asked : " << syntaxStrEncoding[SObjBeingAsked] << endl;
124 
126  string query = "SELECT " + syntaxDBLookUp[SObjBeingAsked] + " FROM ontology WHERE ";
127  for(SyntaxWord const& sw : WordList) {
128  if(sw.getSyntax () != SObjBeingAsked && sw.getSyntax () != S_INVALID) {
129  query += syntaxDBLookUp[sw.getSyntax()] + " LIKE ";
130  query += ("'" + sw.getWord ().getTokenString () + "%'");
131  query += " AND ";
132  }
133  }
134  query = query.substr (0, query.size () - 5);
135 // cout << " DEBUG : query : " << query << endl;
136  return query;
137 }
138 
139 
140 
141 #endif /* !CORPUSQUERYGENERATOR_H */
SyntaxObject askingFor() const
SyntaxTree::askingFor Gets the syntaxobject the question is asking for, if it is a question...
Definition: syntaxtree.cpp:231
std::vector< SyntaxWord > getAll() const
SyntaxTree::getAll Gets all of the syntax words.
Definition: syntaxtree.cpp:196
string getQueryOf(const SyntaxTree &sentence)
getQueryOf
Definition: CorpusQueryGenerator.h:54
Definition: nlp_exception.h:24
SyntaxObject
Definition: config.h:167
SentenceType getSentenceType() const
SyntaxTree::getSentenceType gets the sentence type.
Definition: syntaxtree.cpp:223
The QueryGenerator class.
Definition: CorpusQueryGenerator.h:28
string getInsertionQuery(const vector< SyntaxWord > &WordList)
see implementation for comment
Definition: CorpusQueryGenerator.h:82
string getQuestionQuery(const vector< SyntaxWord > &WordList, const SyntaxObject &SObjBeingAsked)
QueryGenerator::getQuestionQuery From a structure of wordlist, generate valid query.
Definition: CorpusQueryGenerator.h:119
Definition: nlp_exception.h:16
QueryGenerator()
Empty constructor.
Definition: CorpusQueryGenerator.h:34
The SyntaxWord class Container for a Word and a Syntax Identifier.
Definition: syntaxword.h:10
The SyntaxTree class The Tree specialized for holding syntax and words INHERITS: from Tree...
Definition: syntaxtree.h:68