diff --git a/search/keyword_matcher.cpp b/search/keyword_matcher.cpp index 6a3840646c..1d1eda7852 100644 --- a/search/keyword_matcher.cpp +++ b/search/keyword_matcher.cpp @@ -1,6 +1,7 @@ #include "keyword_matcher.hpp" #include "delimiters.hpp" #include "string_match.hpp" +#include "../base/logging.hpp" #include "../base/string_utils.hpp" #include "../std/bind.hpp" #include "../std/numeric.hpp" @@ -21,7 +22,8 @@ KeywordMatcher::KeywordMatcher(strings::UniString const * pKeywords, m_keywordMatchFn(keywordMatchFn), m_prefixMatchFn(prefixMatchFn), m_minKeywordMatchCost(keywordsCount, m_maxKeywordMatchCost + 1), - m_minPrefixMatchCost(m_maxPrefixMatchCost + 1) + m_minPrefixMatchCost(m_maxPrefixMatchCost + 1), + m_bestMatchTokenCount(0) { } @@ -33,31 +35,43 @@ void KeywordMatcher::ProcessName(string const & name) void KeywordMatcher::ProcessNameToken(string const & name, strings::UniString const & s) { + uint32_t tokensMatched = 0; for (size_t i = 0; i < m_minKeywordMatchCost.size(); ++i) { - m_minKeywordMatchCost[i] = min(m_minKeywordMatchCost[i], - m_keywordMatchFn(&m_pKewords[i][0], m_pKewords[i].size(), - &s[0], s.size(), - m_minKeywordMatchCost[i])); + uint32_t const matchCost = m_keywordMatchFn(&m_pKewords[i][0], m_pKewords[i].size(), + &s[0], s.size(), m_minKeywordMatchCost[i]); + if (matchCost <= m_maxKeywordMatchCost) + { + ++tokensMatched; + if (matchCost < m_minKeywordMatchCost[i]) + { + // LOG(LDEBUG, (matchCost, name)); + m_minKeywordMatchCost[i] = matchCost; + } + } } if (!m_prefix.empty()) { uint32_t const matchCost = m_prefixMatchFn(&m_prefix[0], m_prefix.size(), &s[0], s.size(), m_minPrefixMatchCost); - if (matchCost < m_minPrefixMatchCost) + if (matchCost <= m_maxPrefixMatchCost) { - m_bestPrefixMatch = name; - m_minPrefixMatchCost = matchCost; + ++tokensMatched; + if (matchCost < m_minPrefixMatchCost) + m_minPrefixMatchCost = matchCost; } } else { - if (m_bestPrefixMatch.empty()) - { - m_bestPrefixMatch = name; - m_minPrefixMatchCost = 0; - } + ++tokensMatched; + m_minPrefixMatchCost = 0; + } + + if (tokensMatched > m_bestMatchTokenCount) + { + m_bestMatchName = name; + m_bestMatchTokenCount = tokensMatched; } } diff --git a/search/keyword_matcher.hpp b/search/keyword_matcher.hpp index 434b0ea4aa..2d3c442616 100644 --- a/search/keyword_matcher.hpp +++ b/search/keyword_matcher.hpp @@ -23,7 +23,8 @@ class KeywordMatcher StringMatchFn m_keywordMatchFn, m_prefixMatchFn; buffer_vector m_minKeywordMatchCost; uint32_t m_minPrefixMatchCost; - string m_bestPrefixMatch; + string m_bestMatchName; + uint32_t m_bestMatchTokenCount; public: KeywordMatcher(strings::UniString const * pKeywords, @@ -51,7 +52,7 @@ public: // Get match score for each keyword. uint32_t const * GetKeywordMatchScores() const { return &m_minKeywordMatchCost[0]; } - string GetBestPrefixMatch() const { return m_bestPrefixMatch; } + string GetBestPrefixMatch() const { return m_bestMatchName; } }; } // namespace search::impl