-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tuning bm25, adding sort order, remove lavenshtein
Modify suggestion mode flow add unit tests
- Loading branch information
Showing
6 changed files
with
200 additions
and
88 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
/* | ||
* Copyright (C) 2021 Maneesh P M <manu.pm55@gmail.com> | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and | ||
* NON-INFRINGEMENT. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
*/ | ||
|
||
#include <zim/zim.h> | ||
#include <zim/archive.h> | ||
#include <zim/search.h> | ||
#include <zim/writer/creator.h> | ||
#include <zim/writer/item.h> | ||
#include <zim/writer/contentProvider.h> | ||
|
||
#include "tools.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace { | ||
|
||
class TestItem : public zim::writer::BasicItem { | ||
public: | ||
TestItem(const std::string& path, const std::string& mimetype, const std::string& title) | ||
: BasicItem(path, mimetype, title) {} | ||
|
||
virtual std::unique_ptr<zim::writer::ContentProvider> getContentProvider() const { | ||
return std::unique_ptr<zim::writer::ContentProvider>(new zim::writer::StringProvider("")); | ||
} | ||
}; | ||
|
||
std::string createTestZim(std::vector<std::string> titles) { | ||
zim::unittests::TempFile temp("testZim"); | ||
auto tempPath = temp.path() + ".zim"; | ||
|
||
zim::writer::Creator creator; | ||
creator.configIndexing(true, "eng"); | ||
creator.startZimCreation(tempPath); | ||
|
||
// add dummy items with given titles | ||
for (auto title : titles) { | ||
std::string path = "dummyPath" + title; | ||
auto item = std::make_shared<TestItem>(path, "plain/text", title); | ||
creator.addItem(item); | ||
} | ||
|
||
creator.addMetadata("Title", "This is a title"); | ||
creator.finishZimCreation(); | ||
|
||
return tempPath; | ||
} | ||
|
||
std::vector<std::string> getSuggestions(const zim::Archive archive, std::string query, int range) { | ||
zim::Search search(archive); | ||
search.set_suggestion_mode(true); | ||
search.set_query(query); | ||
search.set_range(0, range); | ||
search.set_verbose(true); | ||
|
||
std::vector<std::string> result; | ||
for (auto entry = search.begin();entry!=search.end();entry++) { | ||
result.push_back((*entry).getTitle()); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
TEST(Suggestion, emptyQuery) { | ||
std::vector<std::string> titles = { | ||
"fooland", | ||
"berlin wall", | ||
"hotel berlin, berlin", | ||
"again berlin", | ||
"berlin", | ||
"not berlin", | ||
}; | ||
|
||
std::string path = createTestZim(titles); | ||
const zim::Archive archive(path); | ||
std::vector<std::string> resultSet = getSuggestions(archive, "", archive.getEntryCount()); | ||
|
||
ASSERT_EQ(resultSet.size(), 0); | ||
} | ||
|
||
TEST(Suggestion, noResult) { | ||
std::vector<std::string> titles = { | ||
"fooland" | ||
"berlin wall", | ||
"hotel berlin, berlin", | ||
"again berlin", | ||
"berlin", | ||
"not berlin", | ||
}; | ||
|
||
std::string path = createTestZim(titles); | ||
const zim::Archive archive(path); | ||
std::vector<std::string> resultSet = getSuggestions(archive, "none", archive.getEntryCount()); | ||
|
||
ASSERT_EQ(resultSet.size(), 0); | ||
} | ||
|
||
TEST(Suggestion, singleTermOrder) { | ||
std::vector<std::string> titles = { | ||
"fooland", | ||
"berlin wall", | ||
"hotel berlin, berlin", | ||
"again berlin", | ||
"berlin", | ||
"not berlin", | ||
}; | ||
|
||
std::vector<std::string> expectedResult = { | ||
"berlin", | ||
"hotel berlin, berlin", | ||
"not berlin", | ||
"berlin wall", | ||
"again berlin", | ||
}; | ||
|
||
std::string path = createTestZim(titles); | ||
const zim::Archive archive(path); | ||
std::vector<std::string> resultSet = getSuggestions(archive, "berlin", archive.getEntryCount()); | ||
|
||
ASSERT_EQ(expectedResult.size() , resultSet.size()); | ||
|
||
for(int i = 0;(unsigned int)i < resultSet.size();i++){ | ||
std::cout<<"Expected: "<<expectedResult[i]<<", Actual: "<<resultSet[i]<<std::endl; | ||
ASSERT_EQ(expectedResult[i], resultSet[i]); | ||
} | ||
} | ||
|
||
TEST(Suggestion, resultsGreaterThanLimit) { | ||
std::vector<std::string> titles = { | ||
"foobar b", | ||
"foobar a", | ||
"foobar c", | ||
"foobar e", | ||
"foobar d", | ||
}; | ||
|
||
std::string path = createTestZim(titles); | ||
const zim::Archive archive(path); | ||
std::vector<std::string> resultSet = getSuggestions(archive, "foobar", 2); | ||
|
||
ASSERT_EQ(2 , resultSet.size()); | ||
} | ||
|
||
TEST(Suggestion, phraseOrder) { | ||
std::vector<std::string> titles = { | ||
"Summer in Berlin", | ||
"In Summer", | ||
"Shivers in summer", | ||
"Summer in Paradise", | ||
"In mid Summer", | ||
}; | ||
|
||
// TODO improve the relative order of terms | ||
std::vector<std::string> expectedResult = { | ||
"In Summer", | ||
"Summer in Paradise", | ||
"Summer in Berlin", | ||
"Shivers in summer", | ||
"In mid Summer", | ||
}; | ||
|
||
|
||
std::string path = createTestZim(titles); | ||
const zim::Archive archive(path); | ||
std::vector<std::string> resultSet = getSuggestions(archive, "summer in", archive.getEntryCount()); | ||
|
||
ASSERT_EQ(expectedResult.size(), resultSet.size()); | ||
for(int i = 0;(unsigned int)i < resultSet.size();i++){ | ||
std::cout<<"Expected: "<<expectedResult[i]<<", Actual: "<<resultSet[i]<<std::endl; | ||
ASSERT_EQ(expectedResult[i], resultSet[i]); | ||
} | ||
} | ||
} |