diff options
author | Yury Melnichek <melnichek@gmail.com> | 2011-05-23 19:44:32 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:18:05 +0300 |
commit | bd48d71ceb9587241e66d2907ce17b1b8afa37e9 (patch) | |
tree | 888d5c126a02240370d1b1099da581e69ae376d4 /search/keyword_matcher.cpp | |
parent | 35978479ec6a51c0dd5efc1ba789abb166eecdaa (diff) |
Refactored and improved search: approximate matching and splitting feature names into tokens.
Diffstat (limited to 'search/keyword_matcher.cpp')
-rw-r--r-- | search/keyword_matcher.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/search/keyword_matcher.cpp b/search/keyword_matcher.cpp new file mode 100644 index 0000000000..54553c7a39 --- /dev/null +++ b/search/keyword_matcher.cpp @@ -0,0 +1,60 @@ +#include "keyword_matcher.hpp" +#include "delimiters.hpp" +#include "../base/string_utils.hpp" +#include "../std/numeric.hpp" + +namespace search +{ +namespace impl +{ + +KeywordMatcher::KeywordMatcher(strings::UniString * pKeywords, + size_t keywordsCount, + strings::UniString const & prefix, + uint32_t maxKeywordMatchCost, uint32_t maxPrefixMatchCost, + StringMatchFn keywordMatchFn, StringMatchFn prefixMatchFn) + : m_pKewords(pKeywords), m_prefix(prefix), + m_maxKeywordMatchCost(maxKeywordMatchCost), + m_maxPrefixMatchCost(maxPrefixMatchCost), + m_keywordMatchFn(keywordMatchFn), + m_prefixMatchFn(prefixMatchFn), + m_minKeywordMatchCost(keywordsCount, m_maxKeywordMatchCost + 1), + m_minPrefixMatchCost(m_maxPrefixMatchCost + 1) +{ +} + +void KeywordMatcher::ProcessName(string const & name) +{ + search::Delimiters delims; + for (strings::TokenizeIterator<search::Delimiters> iter(name, delims); iter; ++iter) + { + strings::UniString const s = iter.GetUniString(); + + 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])); + } + + if (!m_prefix.empty()) + { + m_minPrefixMatchCost = min(m_minPrefixMatchCost, + m_prefixMatchFn(&m_prefix[0], m_prefix.size(), + &s[0], s.size(), + m_minPrefixMatchCost)); + } + else + m_minPrefixMatchCost = 0; + } +} + +uint32_t KeywordMatcher::GetMatchScore() const +{ + return accumulate(m_minKeywordMatchCost.begin(), m_minKeywordMatchCost.end(), + m_minPrefixMatchCost); +} + +} // namespace search::impl +} // namespace search |