+ fix duplicate suggestion under certain conditions

This commit is contained in:
kelson42 2013-05-11 20:34:47 +02:00
parent 610257e4fd
commit 8b4941b55a
1 changed files with 35 additions and 12 deletions

View File

@ -384,6 +384,9 @@ namespace kiwix {
/* Search titles by prefix */
bool Reader::searchSuggestions(const string &prefix, unsigned int suggestionsCount, const bool reset) {
bool retVal = false;
zim::File::const_iterator articleItr;
std::vector<std::string>::iterator suggestionItr;
int result;
/* Reset the suggestions */
if (reset) {
@ -391,16 +394,35 @@ namespace kiwix {
}
if (prefix.size()) {
for (zim::File::const_iterator it = zimFileHandler->findByTitle('A', prefix);
it != zimFileHandler->end() &&
it->getTitle().compare(0, prefix.size(), prefix) == 0 &&
for (articleItr = zimFileHandler->findByTitle('A', prefix);
articleItr != zimFileHandler->end() &&
articleItr->getTitle().compare(0, prefix.size(), prefix) == 0 &&
this->suggestions.size() < suggestionsCount ;
++it) {
++articleItr) {
this->suggestions.push_back(it->getTitle());
if (this->suggestions.size() == 0) {
this->suggestions.push_back(articleItr->getTitle());
} else {
for (suggestionItr = this->suggestions.begin() ;
suggestionItr != this->suggestions.end();
++suggestionItr) {
/* Suggestions where found */
retVal = true;
result = articleItr->getTitle().compare(*suggestionItr);
if (result < 0) {
this->suggestions.insert(suggestionItr, articleItr->getTitle());
break;
} else if (result == 0) {
break;
}
}
if (suggestionItr == this->suggestions.end()) {
this->suggestions.push_back(articleItr->getTitle());
}
}
/* Suggestions where found */
retVal = true;
}
}
@ -413,14 +435,15 @@ namespace kiwix {
/* 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 */
/* Try with first letter uppercase */
myPrefix = kiwix::ucFirst(myPrefix);
this->searchSuggestions(myPrefix, suggestionsCount, false);
/* Trye with first letter lowercase */
/* Try with first letter lowercase */
myPrefix = kiwix::lcFirst(myPrefix);
this->searchSuggestions(myPrefix, suggestionsCount, false);