Skip to content

Commit 82b1b4c

Browse files
Implemented getTermCompletions()
1 parent 5264c0d commit 82b1b4c

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

src/suggestion.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,45 @@ struct TermWithFreq
297297
}
298298
};
299299

300-
std::vector<TermWithFreq> getTermCompletions(const SuggestionDataBase& db,
301-
const std::string& termPrefix)
300+
typedef std::vector<TermWithFreq> TermCollection;
301+
302+
bool termShouldBeIncludedInAutoCompletions(const std::string& term) {
303+
// XXX: implement properly (e.g. omit prefixed terms)
304+
return true;
305+
}
306+
307+
TermCollection getAllTerms(const SuggestionDataBase& db) {
308+
TermCollection allTerms;
309+
310+
const Xapian::Database& titleDb = db.m_database;
311+
for (auto it = titleDb.allterms_begin(); it != titleDb.allterms_end(); ++it) {
312+
if ( termShouldBeIncludedInAutoCompletions(*it) ) {
313+
allTerms.push_back(TermWithFreq{*it, it.get_termfreq()});
314+
}
315+
}
316+
317+
std::sort(allTerms.begin(), allTerms.end(), TermWithFreq::dictionaryPred);
318+
return allTerms;
319+
}
320+
321+
TermCollection getTermCompletions(const SuggestionDataBase& db,
322+
const std::string& termPrefix)
302323
{
303-
// XXX: implement properly
304-
return {{"daily", 1}, {"data", 2}, {"date", 7}, { "day", 4}};
324+
if ( !db.hasDatabase() ) {
325+
return TermCollection();
326+
}
327+
328+
const TermCollection allTerms = getAllTerms(db);
329+
auto it = std::lower_bound(allTerms.begin(), allTerms.end(),
330+
TermWithFreq{termPrefix, 0},
331+
TermWithFreq::dictionaryPred);
332+
333+
TermCollection result;
334+
for ( ; it != allTerms.end() && startsWith(it->term, termPrefix); ++it ) {
335+
result.push_back(*it);
336+
}
337+
338+
return result;
305339
}
306340

307341
} // unnamed namespace

src/tools.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ namespace zim {
5252
**/
5353
uint32_t LIBZIM_PRIVATE_API randomNumber(uint32_t max);
5454

55+
inline bool startsWith(const std::string& str, const std::string& prefix)
56+
{
57+
return prefix.length() <= str.length()
58+
&& std::equal(prefix.begin(), prefix.end(), str.begin());
59+
}
60+
5561
std::vector<std::string> split(const std::string & str,
5662
const std::string & delims=" *-");
5763

test/suggestion.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,25 @@ TEST(Suggestion, autoCompletionAndSpellingCorrection) {
783783
{"Birth date of John Smith", "xx/xx/xx", "<b>Birth</b> date of John Smith"},
784784
}));
785785

786+
// Since the count of title suggestions will exceed the specified limit,
787+
// autocompletion suggestions should be returned instead
788+
EXPECT_SUGGESTION_RESULTS(archive, "bi", 4, ({
789+
{"", "", "<b>big</b>" },
790+
{"", "", "<b>birth</b>" },
791+
}));
792+
793+
EXPECT_SUGGESTION_RESULTS(archive, "date bi", 10, ({
794+
{"Date of my birth", "long_ago", "<b>Date</b> of my <b>birth</b>"},
795+
{"J. Wales' birth date", "1966/08/07", "J. Wales' <b>birth</b> <b>date</b>"},
796+
{"Birth date of J. Christ", "-1/12/25" , "<b>Birth</b> <b>date</b> of J. Christ"},
797+
{"Birth date of John Smith", "xx/xx/xx", "<b>Birth</b> <b>date</b> of John Smith"},
798+
}));
799+
800+
EXPECT_SUGGESTION_RESULTS(archive, "date bi", 3, ({
801+
{"", "", "date <b>big</b>" },
802+
{"", "", "date <b>birth</b>" },
803+
}));
804+
786805
EXPECT_SUGGESTION_RESULTS(archive, "da", 20, ({
787806
{"Date palm" , "Date_palm" , "<b>Date</b> palm" },
788807
{"Date, Fukushima" , "Date_(city)", "<b>Date</b>, Fukushima" },

0 commit comments

Comments
 (0)