diff --git a/search/keyword_matcher.cpp b/search/keyword_matcher.cpp index 1d1eda7852..cd40926009 100644 --- a/search/keyword_matcher.cpp +++ b/search/keyword_matcher.cpp @@ -23,7 +23,7 @@ KeywordMatcher::KeywordMatcher(strings::UniString const * pKeywords, m_prefixMatchFn(prefixMatchFn), m_minKeywordMatchCost(keywordsCount, m_maxKeywordMatchCost + 1), m_minPrefixMatchCost(m_maxPrefixMatchCost + 1), - m_bestMatchTokenCount(0) + m_bestMatchNamePenalty(-1) { } @@ -35,14 +35,14 @@ void KeywordMatcher::ProcessName(string const & name) void KeywordMatcher::ProcessNameToken(string const & name, strings::UniString const & s) { - uint32_t tokensMatched = 0; + uint32_t matchPenalty = 0; for (size_t i = 0; i < m_minKeywordMatchCost.size(); ++i) { uint32_t const matchCost = m_keywordMatchFn(&m_pKewords[i][0], m_pKewords[i].size(), &s[0], s.size(), m_minKeywordMatchCost[i]); + matchPenalty += matchCost; if (matchCost <= m_maxKeywordMatchCost) { - ++tokensMatched; if (matchCost < m_minKeywordMatchCost[i]) { // LOG(LDEBUG, (matchCost, name)); @@ -51,27 +51,29 @@ void KeywordMatcher::ProcessNameToken(string const & name, strings::UniString co } } + bool bPrefixMatch = false; if (!m_prefix.empty()) { uint32_t const matchCost = m_prefixMatchFn(&m_prefix[0], m_prefix.size(), &s[0], s.size(), m_minPrefixMatchCost); + matchPenalty += matchCost; if (matchCost <= m_maxPrefixMatchCost) { - ++tokensMatched; + bPrefixMatch = true; if (matchCost < m_minPrefixMatchCost) m_minPrefixMatchCost = matchCost; } } else { - ++tokensMatched; + bPrefixMatch = true; m_minPrefixMatchCost = 0; } - if (tokensMatched > m_bestMatchTokenCount) + if (bPrefixMatch && matchPenalty < m_bestMatchNamePenalty) { m_bestMatchName = name; - m_bestMatchTokenCount = tokensMatched; + m_bestMatchNamePenalty = matchPenalty; } } diff --git a/search/keyword_matcher.hpp b/search/keyword_matcher.hpp index 2d3c442616..05debb7364 100644 --- a/search/keyword_matcher.hpp +++ b/search/keyword_matcher.hpp @@ -24,7 +24,7 @@ class KeywordMatcher buffer_vector m_minKeywordMatchCost; uint32_t m_minPrefixMatchCost; string m_bestMatchName; - uint32_t m_bestMatchTokenCount; + uint32_t m_bestMatchNamePenalty; public: KeywordMatcher(strings::UniString const * pKeywords, @@ -52,7 +52,7 @@ public: // Get match score for each keyword. uint32_t const * GetKeywordMatchScores() const { return &m_minKeywordMatchCost[0]; } - string GetBestPrefixMatch() const { return m_bestMatchName; } + string GetBestMatchName() const { return m_bestMatchName; } }; } // namespace search::impl diff --git a/search/query.cpp b/search/query.cpp index 90e7d38602..834963293d 100644 --- a/search/query.cpp +++ b/search/query.cpp @@ -89,7 +89,7 @@ struct FeatureProcessor m_query.AddResult(IntermediateResult(m_query.GetViewport(), feature, - matcher.GetBestPrefixMatch(), + matcher.GetBestMatchName(), matchScore, scaleRange.first)); }