From 8de4dd9ce0f2ea16cbd47b0730b9b1b4e84fbe83 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sat, 25 Aug 2012 15:36:41 +0000 Subject: [PATCH] + move improved suggestion system (check also variations of the prefix) to reader.cpp --- src/common/kiwix/reader.cpp | 40 ++++++++++++++++++++++++++++--------- src/common/kiwix/reader.h | 6 ++++-- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/common/kiwix/reader.cpp b/src/common/kiwix/reader.cpp index b5060974f..6b45e49d0 100644 --- a/src/common/kiwix/reader.cpp +++ b/src/common/kiwix/reader.cpp @@ -366,22 +366,27 @@ namespace kiwix { return retVal; } - /* Search titles by prefix*/ - bool Reader::searchSuggestions(const string &prefix, unsigned int suggestionsCount) { - bool retVal = true; + /* Search titles by prefix */ + bool Reader::searchSuggestions(const string &prefix, unsigned int suggestionsCount, const bool reset) { + bool retVal = false; /* Reset the suggestions */ - this->suggestions.clear(); - + if (reset) { + this->suggestions.clear(); + } + if (prefix.size()) { for (zim::File::const_iterator it = zimFileHandler->findByTitle('A', prefix); - it != zimFileHandler->end() && it->getTitle().compare(0, prefix.size(), prefix) == 0 - && this->suggestions.size() < suggestionsCount ; ++it) { + it != zimFileHandler->end() && + it->getTitle().compare(0, prefix.size(), prefix) == 0 && + this->suggestions.size() < suggestionsCount ; + ++it) { this->suggestions.push_back(it->getTitle()); + + /* Suggestions where found */ + retVal = true; } - } else { - retVal = false; } /* Set the cursor to the begining */ @@ -390,6 +395,23 @@ namespace kiwix { return retVal; } + /* Try also a few variations of the prefix to have better results */ + bool Reader::searchSuggestionsSmart(const string &prefix, unsigned int suggestionsCount) { + std::string myPrefix = prefix; + /* Normal suggestion request */ + bool retVal = this->searchSuggestions(prefix, suggestionsCount, true); + + /* Trye with first letter uppercase */ + myPrefix = kiwix::ucFirst(myPrefix); + this->searchSuggestions(myPrefix, suggestionsCount, false); + + /* Trye with first letter lowercase */ + myPrefix = kiwix::lcFirst(myPrefix); + this->searchSuggestions(myPrefix, suggestionsCount, false); + + return retVal; + } + /* Get next suggestion */ bool Reader::getNextSuggestion(string &title) { if (this->suggestionsOffset != this->suggestions.end()) { diff --git a/src/common/kiwix/reader.h b/src/common/kiwix/reader.h index a0829b5f6..338a2142d 100644 --- a/src/common/kiwix/reader.h +++ b/src/common/kiwix/reader.h @@ -30,7 +30,8 @@ #include #include #include "time.h" -#include "../pathTools.h" +#include +#include using namespace std; @@ -59,7 +60,8 @@ namespace kiwix { bool getFavicon(string &content, string &mimeType); bool getPageUrlFromTitle(const string &title, string &url); bool getContentByUrl(const string &url, string &content, unsigned int &contentLength, string &contentType); - bool searchSuggestions(const string &prefix, unsigned int suggestionsCount); + bool searchSuggestions(const string &prefix, unsigned int suggestionsCount, const bool reset = true); + bool searchSuggestionsSmart(const string &prefix, unsigned int suggestionsCount); bool getNextSuggestion(string &title); bool canCheckIntegrity(); bool isCorrupted();