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>
17 #include <opentrep/config/opentrep-paths.hpp>
58 typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
61 const boost::char_separator<char> lSepatorList(
" .,;:|+-*/_=!@#$%`~^&(){}[]?'<>\"");
64 Tokeniser_T lTokens (iPhrase, lSepatorList);
65 for (Tokeniser_T::const_iterator tok_iter = lTokens.begin();
66 tok_iter != lTokens.end(); ++tok_iter) {
67 const std::string& lTerm = *tok_iter;
68 ioWordList.push_back (lTerm);
74 std::ostringstream oStr;
76 unsigned short idx = iWordList.size();
77 for (WordList_T::const_iterator itWord = iWordList.begin();
78 itWord != iWordList.end(); ++itWord, --idx) {
79 const std::string& lWord = *itWord;
92 template<
class T> std::ostream&
operator<< (std::ostream& os,
93 const std::vector<T>& v) {
94 std::copy (v.begin(), v.end(), std::ostream_iterator<T> (std::cout,
" "));
103 unsigned short& ioSpellingErrorDistance,
104 std::string& ioQueryString,
105 std::string& ioXapianDBFilepath,
106 std::string& ioSQLiteDBFilepath,
107 std::string& ioLogFilename,
108 unsigned short& ioSearchType) {
111 if (ioQueryString.empty() ==
true) {
120 boost::program_options::options_description
generic (
"Generic options");
121 generic.add_options()
122 (
"prefix",
"print installation prefix")
123 (
"version,v",
"print version string")
124 (
"help,h",
"produce help message");
128 boost::program_options::options_description config (
"Configuration");
132 "Spelling error distance (e.g., 3)")
135 "Xapian database filepath (e.g., /tmp/opentrep/traveldb)")
138 "SQLite3 database filepath (e.g., ~/tmp/opentrep/traveldb/ori_por_public.db)")
141 "Filepath for the logs")
144 "Type of search request (0 = full text, 1 = coordinates)")
146 boost::program_options::value< WordList_T >(&lWordList)->multitoken(),
147 "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)")
152 boost::program_options::options_description hidden (
"Hidden options");
155 boost::program_options::value< std::vector<std::string> >(),
156 "Show the copyright (license)");
158 boost::program_options::options_description cmdline_options;
159 cmdline_options.add(
generic).add(config).add(hidden);
161 boost::program_options::options_description config_file_options;
162 config_file_options.add(config).add(hidden);
164 boost::program_options::options_description visible (
"Allowed options");
165 visible.add(
generic).add(config);
167 boost::program_options::positional_options_description p;
168 p.add (
"copyright", -1);
170 boost::program_options::variables_map vm;
171 boost::program_options::
172 store (boost::program_options::command_line_parser (argc, argv).
173 options (cmdline_options).positional(p).run(), vm);
175 std::ifstream ifs (
"opentrep-searcher.cfg");
176 boost::program_options::store (parse_config_file (ifs, config_file_options),
178 boost::program_options::notify (vm);
180 if (vm.count (
"help")) {
181 std::cout << visible << std::endl;
185 if (vm.count (
"version")) {
186 std::cout << PACKAGE_NAME <<
", version " << PACKAGE_VERSION << std::endl;
190 if (vm.count (
"prefix")) {
191 std::cout <<
"Installation prefix: " << PREFIXDIR << std::endl;
195 if (vm.count (
"xapiandb")) {
196 ioXapianDBFilepath = vm[
"xapiandb"].as< std::string >();
197 std::cout <<
"Xapian database filepath is: " << ioXapianDBFilepath
201 if (vm.count (
"sqlitedb")) {
202 ioSQLiteDBFilepath = vm[
"sqlitedb"].as< std::string >();
203 std::cout <<
"SQLite3 database filepath is: " << ioSQLiteDBFilepath
207 if (vm.count (
"log")) {
208 ioLogFilename = vm[
"log"].as< std::string >();
209 std::cout <<
"Log filename is: " << ioLogFilename << std::endl;
212 std::cout <<
"The type of search is: " << ioSearchType << std::endl;
214 std::cout <<
"The spelling error distance is: " << ioSpellingErrorDistance
218 std::cout <<
"The travel query string is: " << ioQueryString << std::endl;
228 std::ostringstream oStr;
235 lNonMatchedWordList);
237 oStr << nbOfMatches <<
" (geographical) location(s) have been found "
238 <<
"matching your query (`" << iTravelQuery <<
"'). "
239 << lNonMatchedWordList.size() <<
" words were left unmatched."
242 if (nbOfMatches != 0) {
244 for (OPENTREP::LocationList_T::const_iterator itLocation =
245 lLocationList.begin();
246 itLocation != lLocationList.end(); ++itLocation, ++idx) {
248 oStr <<
" [" << idx <<
"]: " << lLocation << std::endl;
252 if (lNonMatchedWordList.empty() ==
false) {
253 oStr <<
"List of unmatched words:" << std::endl;
256 for (OPENTREP::WordList_T::const_iterator itWord =
257 lNonMatchedWordList.begin();
258 itWord != lNonMatchedWordList.end(); ++itWord, ++idx) {
260 oStr <<
" [" << idx <<
"]: " << lWord << std::endl;
268 int main (
int argc,
char* argv[]) {
294 std::string lLogFilename;
297 std::string lXapianDBNameStr;
300 std::string lSQLiteDBFilePathStr;
303 unsigned short lSearchType;
306 unsigned short lSpellingErrorDistance;
309 const int lOptionParserStatus =
311 lXapianDBNameStr, lSQLiteDBFilePathStr,
312 lLogFilename, lSearchType);
319 std::ofstream logOutputFile;
321 logOutputFile.open (lLogFilename.c_str());
322 logOutputFile.clear();
324 if (lSearchType == 0) {
329 lXapianDBName, lSQLiteDBFilePath);
332 const std::string& lOutput =
parseQuery (opentrepService, lTravelQuery);
333 std::cout << lOutput;
336 std::cout <<
"Finding the airports closest to: " << lTravelQuery
341 logOutputFile.close();
std::vector< std::string > WordList_T
std::string createStringFromWordList(const WordList_T &iWordList)
unsigned short NbOfMatches_T
int readConfiguration(int argc, char *argv[], unsigned short &ioSpellingErrorDistance, std::string &ioQueryString, std::string &ioXapianDBFilepath, std::string &ioSQLiteDBFilepath, std::string &ioLogFilename, unsigned short &ioSearchType)
Structure modelling a (geographical) location.
const std::string DEFAULT_OPENTREP_SQLITE_DB_FILEPATH
int main(int argc, char *argv[])
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 std::string K_OPENTREP_DEFAULT_LOG_FILENAME("opentrep-searcher.log")
const unsigned short K_OPENTREP_DEFAULT_SEARCH_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")
const int K_OPENTREP_EARLY_RETURN_STATUS
NbOfMatches_T interpretTravelRequest(const std::string &iTravelQuery, LocationList_T &, WordList_T &)
const std::string DEFAULT_OPENTREP_XAPIAN_DB_FILEPATH
std::string TravelQuery_T
std::ostream & operator<<(std::ostream &os, const std::vector< T > &v)
const unsigned short K_OPENTREP_DEFAULT_SPELLING_ERROR_DISTANCE