Skip to content

[7.x][ML] Fix off-by-one error in usurped categories #1123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/model/CTokenListDataCategorizerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,8 @@ class MODEL_EXPORT CTokenListDataCategorizerBase : public CDataCategorizer {
TTokenListCategoryVec m_Categories;

//! List of match count/index into category vector in descending order of
//! match count
//! match count. Note that the second element is an index into m_Categories,
//! not a category ID.
TSizeSizePrVec m_CategoriesByCount;

//! Used for looking up tokens to a unique ID
Expand Down
20 changes: 12 additions & 8 deletions lib/model/CTokenListDataCategorizerBase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ bool CTokenListDataCategorizerBase::createReverseSearch(int categoryId,
std::string& part2,
std::size_t& maxMatchingLength,
bool& wasCached) {
wasCached = false;
maxMatchingLength = 0;

if (m_ReverseSearchCreator == nullptr) {
LOG_ERROR(<< "Cannot create reverse search - no reverse search creator");

Expand Down Expand Up @@ -207,8 +210,8 @@ bool CTokenListDataCategorizerBase::createReverseSearch(int categoryId,
maxMatchingLength = category.maxMatchingStringLen();

// If we can retrieve cached reverse search terms we'll save a lot of time
if (category.cachedReverseSearch(part1, part2) == true) {
wasCached = true;
wasCached = category.cachedReverseSearch(part1, part2);
if (wasCached) {
return true;
}

Expand Down Expand Up @@ -634,27 +637,28 @@ CDataCategorizer::TIntVec CTokenListDataCategorizerBase::usurpedCategories(int c
}
auto iter = std::find_if(m_CategoriesByCount.begin(), m_CategoriesByCount.end(),
[categoryId](const TSizeSizePr& pr) {
return pr.second == static_cast<std::size_t>(categoryId);
return pr.second ==
static_cast<std::size_t>(categoryId - 1);
});
if (iter == m_CategoriesByCount.end()) {
LOG_WARN(<< "Could not find category definition for category: " << categoryId);
return usurped;
}
++iter;

const CTokenListCategory& category{m_Categories[categoryId - 1]};
for (; iter != m_CategoriesByCount.end(); ++iter) {
const CTokenListCategory& lessFrequentCategory{
m_Categories[static_cast<int>(iter->second) - 1]};
for (++iter; iter != m_CategoriesByCount.end(); ++iter) {
const CTokenListCategory& lessFrequentCategory{m_Categories[iter->second]};
bool matchesSearch{category.maxMatchingStringLen() >=
lessFrequentCategory.maxMatchingStringLen() &&
category.isMissingCommonTokenWeightZero(
lessFrequentCategory.commonUniqueTokenIds()) &&
category.containsCommonInOrderTokensInOrder(
lessFrequentCategory.baseTokenIds())};
if (matchesSearch) {
usurped.emplace_back(static_cast<int>(iter->second));
usurped.emplace_back(1 + static_cast<int>(iter->second));
}
}
std::sort(usurped.begin(), usurped.end());
return usurped;
}

Expand Down
13 changes: 7 additions & 6 deletions lib/model/unittest/CTokenListDataCategorizerTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

#include <core/CContainerPrinter.h>
#include <core/CLogger.h>
#include <core/CRapidXmlParser.h>
#include <core/CRapidXmlStatePersistInserter.h>
Expand Down Expand Up @@ -542,13 +543,13 @@ BOOST_FIXTURE_TEST_CASE(testUsurpedCategories, CTestFixture) {
500));

BOOST_REQUIRE_EQUAL(2, categorizer.numMatches(1));
std::vector<int> expected{2, 3, 4, 5, 6};
std::vector<int> actual = categorizer.usurpedCategories(1);

BOOST_REQUIRE_EQUAL(expected.size(), actual.size());
for (std::size_t i = 0; i < actual.size(); i++) {
BOOST_REQUIRE_EQUAL(expected[i], actual[i]);
}
using TIntVec = std::vector<int>;
TIntVec expected{2, 3, 4, 5, 6, 7};
TIntVec actual{categorizer.usurpedCategories(1)};

BOOST_REQUIRE_EQUAL(ml::core::CContainerPrinter::print(expected),
ml::core::CContainerPrinter::print(actual));
checkMemoryUsageInstrumentation(categorizer);
}

Expand Down