9 #include <boost/date_time/posix_time/posix_time.hpp>
10 #include <boost/date_time/gregorian/gregorian.hpp>
11 #include <boost/tokenizer.hpp>
12 #include <boost/program_options.hpp>
20 #include <opentrep/config/opentrep-paths.hpp>
61 typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
64 const boost::char_separator<char> lSepatorList(
" .,;:|+-*/_=!@#$%`~^&(){}[]?'<>\"");
67 Tokeniser_T lTokens (iPhrase, lSepatorList);
68 for (Tokeniser_T::const_iterator tok_iter = lTokens.begin();
69 tok_iter != lTokens.end(); ++tok_iter) {
70 const std::string& lTerm = *tok_iter;
71 ioWordList.push_back (lTerm);
77 std::ostringstream oStr;
79 unsigned short idx = iWordList.size();
80 for (WordList_T::const_iterator itWord = iWordList.begin();
81 itWord != iWordList.end(); ++itWord, --idx) {
82 const std::string& lWord = *itWord;
95 template<
class T> std::ostream&
operator<< (std::ostream& os,
96 const std::vector<T>& v) {
97 std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout,
" "));
106 unsigned short& ioSpellingErrorDistance,
107 std::string& ioQueryString,
108 std::string& ioXapianDBFilepath,
109 std::string& ioSQLDBTypeString,
110 std::string& ioSQLDBConnectionString,
111 unsigned short& ioDeploymentNumber,
112 std::string& ioLogFilename,
113 unsigned short& ioSearchType) {
116 if (ioQueryString.empty() ==
true) {
125 boost::program_options::options_description
generic (
"Generic options");
126 generic.add_options()
127 (
"prefix",
"print installation prefix")
128 (
"version,v",
"print version string")
129 (
"help,h",
"produce help message");
133 boost::program_options::options_description config (
"Configuration");
137 "Spelling error distance (e.g., 3)")
140 "Xapian database filepath (e.g., /tmp/opentrep/xapian_traveldb)")
143 "SQL database type (e.g., nodb for no SQL database, sqlite for SQLite, mysql for MariaDB/MySQL)")
145 boost::program_options::value< std::string >(&ioSQLDBConnectionString),
146 "SQL database connection string (e.g., ~/tmp/opentrep/sqlite_travel.db for SQLite, \"db=trep_trep user=trep password=trep\" for MariaDB/MySQL)")
149 "Deployment number (from to N, where N=1 normally)")
152 "Filepath for the logs")
155 "Type of search request (0 = full text, 1 = coordinates)")
157 boost::program_options::value< WordList_T >(&lWordList)->multitoken(),
158 "Travel query word list (e.g. sna francicso rio de janero lso anglese reykyavki), which sould be located at the end of the command line (otherwise, the other options would be interpreted as part of that travel query word list)")
163 boost::program_options::options_description hidden (
"Hidden options");
166 boost::program_options::value< std::vector<std::string> >(),
167 "Show the copyright (license)");
169 boost::program_options::options_description cmdline_options;
170 cmdline_options.add(
generic).add(config).add(hidden);
172 boost::program_options::options_description config_file_options;
173 config_file_options.add(config).add(hidden);
175 boost::program_options::options_description visible (
"Allowed options");
176 visible.add(
generic).add(config);
178 boost::program_options::positional_options_description p;
179 p.add (
"copyright", -1);
181 boost::program_options::variables_map vm;
182 boost::program_options::
183 store (boost::program_options::command_line_parser (argc, argv).
184 options (cmdline_options).positional(p).run(), vm);
186 std::ifstream ifs (
"opentrep-searcher.cfg");
187 boost::program_options::store (parse_config_file (ifs, config_file_options),
189 boost::program_options::notify (vm);
191 if (vm.count (
"help")) {
192 std::cout << visible << std::endl;
196 if (vm.count (
"version")) {
197 std::cout << PACKAGE_NAME <<
", version " << PACKAGE_VERSION << std::endl;
201 if (vm.count (
"prefix")) {
202 std::cout <<
"Installation prefix: " << PREFIXDIR << std::endl;
206 if (vm.count (
"deploymentnb")) {
207 ioDeploymentNumber = vm[
"deploymentnb"].as<
unsigned short >();
208 std::cout <<
"Deployment number " << ioDeploymentNumber << std::endl;
211 if (vm.count (
"xapiandb")) {
212 ioXapianDBFilepath = vm[
"xapiandb"].as< std::string >();
213 std::cout <<
"Xapian database filepath is: " << ioXapianDBFilepath
214 << ioDeploymentNumber << std::endl;
217 if (vm.count (
"sqldbtype")) {
218 ioSQLDBTypeString = vm[
"sqldbtype"].as< std::string >();
219 std::cout <<
"SQL database type is: " << ioSQLDBTypeString
226 ioSQLDBConnectionString =
"";
236 if (vm.count (
"sqldbconx")) {
237 ioSQLDBConnectionString = vm[
"sqldbconx"].as< std::string >();
243 const std::string& lSQLDBConnString =
245 ioSQLDBConnectionString,
248 std::cout <<
"SQL database connection string is: " << lSQLDBConnString
252 if (vm.count (
"log")) {
253 ioLogFilename = vm[
"log"].as< std::string >();
254 std::cout <<
"Log filename is: " << ioLogFilename << std::endl;
257 std::cout <<
"The type of search is: " << ioSearchType << std::endl;
259 std::cout <<
"The spelling error distance is: " << ioSpellingErrorDistance
263 std::cout <<
"The travel query string is: " << ioQueryString << std::endl;
273 std::ostringstream oStr;
280 lNonMatchedWordList);
282 oStr << nbOfMatches <<
" (geographical) location(s) have been found "
283 <<
"matching your query (`" << iTravelQuery <<
"'). "
284 << lNonMatchedWordList.size() <<
" words were left unmatched."
287 if (nbOfMatches != 0) {
289 for (OPENTREP::LocationList_T::const_iterator itLocation =
290 lLocationList.begin();
291 itLocation != lLocationList.end(); ++itLocation, ++idx) {
293 oStr <<
" [" << idx <<
"]: " << lLocation << std::endl;
297 if (lNonMatchedWordList.empty() ==
false) {
298 oStr <<
"List of unmatched words:" << std::endl;
301 for (OPENTREP::WordList_T::const_iterator itWord =
302 lNonMatchedWordList.begin();
303 itWord != lNonMatchedWordList.end(); ++itWord, ++idx) {
305 oStr <<
" [" << idx <<
"]: " << lWord << std::endl;
313 int main (
int argc,
char* argv[]) {
339 std::string lLogFilename;
342 std::string lXapianDBNameStr;
345 unsigned short lSearchType;
348 unsigned short lSpellingErrorDistance;
351 std::string lSQLDBTypeStr;
354 std::string lSQLDBConnectionStr;
360 const int lOptionParserStatus =
362 lXapianDBNameStr, lSQLDBTypeStr, lSQLDBConnectionStr,
363 lDeploymentNumber, lLogFilename, lSearchType);
370 std::ofstream logOutputFile;
372 logOutputFile.open (lLogFilename.c_str());
373 logOutputFile.clear();
375 if (lSearchType == 0) {
381 lDBType, lSQLDBConnStr,
385 const std::string& lOutput =
parseQuery (opentrepService, lTravelQuery);
386 std::cout << lOutput;
389 std::cout <<
"Finding the airports closest to: " << lTravelQuery
394 logOutputFile.close();
int main(int argc, char *argv[])
const std::string K_OPENTREP_DEFAULT_LOG_FILENAME("opentrep-indexer.log")
const unsigned short DEFAULT_OPENTREP_DEPLOYMENT_NUMBER
std::string createStringFromWordList(const WordList_T &iWordList)
unsigned short NbOfMatches_T
std::string parseAndDisplayConnectionString(const DBType &iDBType, const std::string &iSQLDBConnStr, const DeploymentNumber_T &iDeploymentNumber)
int readConfiguration(int argc, char *argv[], std::string &ioPORFilepath, std::string &ioXapianDBFilepath, std::string &ioSQLDBTypeString, std::string &ioSQLDBConnectionString, unsigned short &ioDeploymentNumber, bool &ioIncludeNonIATAPOR, bool &ioIndexPORInXapian, bool &ioAddPORInDB, std::string &ioLogFilename)
Structure modelling a (geographical) location.
const std::string DEFAULT_OPENTREP_MYSQL_CONN_STRING
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &ioOut, const OPENTREP::StructAbstract &iStruct)
const std::string DEFAULT_OPENTREP_SQLITE_DB_FILEPATH
Interface for the OPENTREP Services.
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
std::vector< std::string > WordList_T
std::list< Word_T > WordList_T
const unsigned short K_OPENTREP_DEFAULT_SEARCH_TYPE
Enumeration of database types.
const std::string DEFAULT_OPENTREP_SQL_DB_TYPE
std::string parseQuery(OPENTREP::OPENTREP_Service &ioOpentrepService, const OPENTREP::TravelQuery_T &iTravelQuery)
std::list< Location > LocationList_T
const std::string K_OPENTREP_DEFAULT_QUERY_STRING("sna francicso rio de janero lso angles reykyavki")
NbOfMatches_T interpretTravelRequest(const std::string &iTravelQuery, LocationList_T &, WordList_T &)
const std::string DEFAULT_OPENTREP_XAPIAN_DB_FILEPATH
std::string TravelQuery_T
const int K_OPENTREP_EARLY_RETURN_STATUS
unsigned short DeploymentNumber_T
const unsigned short K_OPENTREP_DEFAULT_SPELLING_ERROR_DISTANCE