From dc9e92e1e66f7f7bfe144f8b3a5251ca89faeab0 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sat, 23 Jan 2010 12:42:08 +0000 Subject: [PATCH] + factorisation of the code, creation of the kiwix::Searcher class --- src/common/kiwix/searcher.cpp | 102 ++++++++++++++++++++++++++++++++++ src/common/kiwix/searcher.h | 45 +++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/common/kiwix/searcher.cpp create mode 100644 src/common/kiwix/searcher.h diff --git a/src/common/kiwix/searcher.cpp b/src/common/kiwix/searcher.cpp new file mode 100644 index 000000000..965986b61 --- /dev/null +++ b/src/common/kiwix/searcher.cpp @@ -0,0 +1,102 @@ +#include "searcher.h" + +namespace kiwix { + + /* Constructor */ + Searcher::Searcher(const string &xapianDirectoryPath) + : stemmer(Xapian::Stem("english")) { + this->openDatabase(xapianDirectoryPath); + } + + /* Destructor */ + Searcher::~Searcher() { + } + + /* Open Xapian readable database */ + void Searcher::openDatabase(const string &directoryPath) { + this->readableDatabase = Xapian::Database(directoryPath); + } + + /* Close Xapian writable database */ + void Searcher::closeDatabase() { + return; + } + + /* Search strings in the database */ + void Searcher::search(const string &search, const unsigned int resultsCount) { + /* Reset the results */ + this->results.clear(); + this->resultOffset = this->results.begin(); + + /* Create the enquire object */ + Xapian::Enquire enquire(this->readableDatabase); + + /* Create the query term vector */ + std::vector queryTerms = split(removeAccents(search.c_str()), " #@%$0/\\_-*()[]{},;:"); + + /* Create query object */ + Xapian::Query query(Xapian::Query::OP_OR, queryTerms.begin(), queryTerms.end()); + + /* Set the query in the enquire object */ + enquire.set_query(query); + + cout << "Performing query `" << + query.get_description() << "'" << endl; + + /* Get the results */ + Xapian::MSet matches = enquire.get_mset(0, resultsCount); + + Xapian::MSetIterator i; + for (i = matches.begin(); i != matches.end(); ++i) { + Xapian::Document doc = i.get_document(); + + Result result; + result.url = doc.get_data(); + result.title = doc.get_value(0); + result.score = i.get_percent(); + + this->results.push_back(result); + + cout << "Document ID " << *i << " \t"; + cout << i.get_percent() << "% "; + cout << "\t[" << doc.get_data() << "] - " << doc.get_value(0) << endl; + } + + /* Set the cursor to the begining */ + this->resultOffset = this->results.begin(); + + return; + } + + /* Reset the results */ + void Searcher::reset() { + this->results.clear(); + this->resultOffset = this->results.begin(); + return; + } + + /* Get next result */ + bool Searcher::getNextResult(string &url, string &title, unsigned int &score) { + bool retVal = false; + + if (this->resultOffset != this->results.end()) { + + /* url */ + url = this->resultOffset->url; + + /* title */ + title = this->resultOffset->title; + + /* score */ + score = this->resultOffset->score; + + /* increment the cursor for the next call */ + this->resultOffset++; + + retVal = true; + } + + return retVal; + } + +} diff --git a/src/common/kiwix/searcher.h b/src/common/kiwix/searcher.h new file mode 100644 index 000000000..7a035d9f7 --- /dev/null +++ b/src/common/kiwix/searcher.h @@ -0,0 +1,45 @@ +#ifndef KIWIX_SEARCHER_H +#define KIWIX_SEARCHER_H + +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Result +{ + string url; + string title; + int score; +}; + +namespace kiwix { + + class Searcher { + + public: + Searcher(const string &xapianDirectoryPath); + ~Searcher(); + + Xapian::Database readableDatabase; + Xapian::Stem stemmer; + std::vector results; + std::vector::iterator resultOffset; + + void search(const string &search, const unsigned int resultsCount); + bool getNextResult(string &url, string &title, unsigned int &score); + void closeDatabase(); + void reset(); + + protected: + void openDatabase(const string &xapianDirectoryPath); + }; + +} + +#endif