+ move improved suggestion system (check also variations of the prefix) to reader.cpp

This commit is contained in:
kelson42 2012-08-25 15:36:41 +00:00
parent f3bdf98a51
commit 8de4dd9ce0
2 changed files with 35 additions and 11 deletions

View File

@ -366,22 +366,27 @@ namespace kiwix {
return retVal; return retVal;
} }
/* Search titles by prefix*/ /* Search titles by prefix */
bool Reader::searchSuggestions(const string &prefix, unsigned int suggestionsCount) { bool Reader::searchSuggestions(const string &prefix, unsigned int suggestionsCount, const bool reset) {
bool retVal = true; bool retVal = false;
/* Reset the suggestions */ /* Reset the suggestions */
this->suggestions.clear(); if (reset) {
this->suggestions.clear();
}
if (prefix.size()) { if (prefix.size()) {
for (zim::File::const_iterator it = zimFileHandler->findByTitle('A', prefix); for (zim::File::const_iterator it = zimFileHandler->findByTitle('A', prefix);
it != zimFileHandler->end() && it->getTitle().compare(0, prefix.size(), prefix) == 0 it != zimFileHandler->end() &&
&& this->suggestions.size() < suggestionsCount ; ++it) { it->getTitle().compare(0, prefix.size(), prefix) == 0 &&
this->suggestions.size() < suggestionsCount ;
++it) {
this->suggestions.push_back(it->getTitle()); this->suggestions.push_back(it->getTitle());
/* Suggestions where found */
retVal = true;
} }
} else {
retVal = false;
} }
/* Set the cursor to the begining */ /* Set the cursor to the begining */
@ -390,6 +395,23 @@ namespace kiwix {
return retVal; 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 */ /* Get next suggestion */
bool Reader::getNextSuggestion(string &title) { bool Reader::getNextSuggestion(string &title) {
if (this->suggestionsOffset != this->suggestions.end()) { if (this->suggestionsOffset != this->suggestions.end()) {

View File

@ -30,7 +30,8 @@
#include <sstream> #include <sstream>
#include <map> #include <map>
#include "time.h" #include "time.h"
#include "../pathTools.h" #include <pathTools.h>
#include <stringTools.h>
using namespace std; using namespace std;
@ -59,7 +60,8 @@ namespace kiwix {
bool getFavicon(string &content, string &mimeType); bool getFavicon(string &content, string &mimeType);
bool getPageUrlFromTitle(const string &title, string &url); bool getPageUrlFromTitle(const string &title, string &url);
bool getContentByUrl(const string &url, string &content, unsigned int &contentLength, string &contentType); 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 getNextSuggestion(string &title);
bool canCheckIntegrity(); bool canCheckIntegrity();
bool isCorrupted(); bool isCorrupted();