[search] Factor out SplitAndNormalizeAndSimplifyString().

This commit is contained in:
Yury Melnichek 2011-05-29 16:35:06 +02:00 committed by Alex Zolotarev
parent 390726131b
commit 2929b89ced
4 changed files with 40 additions and 31 deletions

View file

@ -1,6 +1,8 @@
#include "keyword_matcher.hpp"
#include "delimiters.hpp"
#include "string_match.hpp"
#include "../base/string_utils.hpp"
#include "../std/bind.hpp"
#include "../std/numeric.hpp"
namespace search
@ -25,36 +27,36 @@ KeywordMatcher::KeywordMatcher(strings::UniString * pKeywords,
void KeywordMatcher::ProcessName(string const & name)
{
search::Delimiters delims;
for (strings::TokenizeIterator<search::Delimiters> iter(name, delims); iter; ++iter)
SplitAndNormalizeAndSimplifyString(
name, bind(&KeywordMatcher::ProcessNameToken, this, cref(name), _1), Delimiters());
}
void KeywordMatcher::ProcessNameToken(string const & name, strings::UniString const & s)
{
for (size_t i = 0; i < m_minKeywordMatchCost.size(); ++i)
{
strings::UniString const s = strings::MakeLowerCase(iter.GetUniString());
m_minKeywordMatchCost[i] = min(m_minKeywordMatchCost[i],
m_keywordMatchFn(&m_pKewords[i][0], m_pKewords[i].size(),
&s[0], s.size(),
m_minKeywordMatchCost[i]));
}
for (size_t i = 0; i < m_minKeywordMatchCost.size(); ++i)
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)
{
m_minKeywordMatchCost[i] = min(m_minKeywordMatchCost[i],
m_keywordMatchFn(&m_pKewords[i][0], m_pKewords[i].size(),
&s[0], s.size(),
m_minKeywordMatchCost[i]));
m_bestPrefixMatch = name;
m_minPrefixMatchCost = matchCost;
}
if (!m_prefix.empty())
}
else
{
if (m_bestPrefixMatch.empty())
{
uint32_t const matchCost = m_prefixMatchFn(&m_prefix[0], m_prefix.size(),
&s[0], s.size(), m_minPrefixMatchCost);
if (matchCost < m_minPrefixMatchCost)
{
m_bestPrefixMatch = name;
m_minPrefixMatchCost = matchCost;
}
}
else
{
if (m_bestPrefixMatch.empty())
{
m_bestPrefixMatch = name;
m_minPrefixMatchCost = 0;
}
m_bestPrefixMatch = name;
m_minPrefixMatchCost = 0;
}
}
}

View file

@ -33,6 +33,7 @@ public:
StringMatchFn keywordMatchFn, StringMatchFn prefixMatchFn);
void ProcessName(string const & name);
void ProcessNameToken(string const & name, strings::UniString const & token);
// Useful for FeatureType.ForEachName(), calls ProcessName() and always returns true.
bool operator () (int /*lang*/, string const & name)

View file

@ -2,6 +2,7 @@
#include "delimiters.hpp"
#include "keyword_matcher.hpp"
#include "string_match.hpp"
#include "../base/stl_add.hpp"
namespace search
{
@ -43,12 +44,11 @@ Query::Query(string const & query, m2::RectD const & rect, IndexType const * pIn
: m_queryText(query), m_rect(rect), m_pIndex(pIndex)
{
search::Delimiters delims;
for (strings::TokenizeIterator<search::Delimiters> iter(query, delims); iter; ++iter)
SplitAndNormalizeAndSimplifyString(query, MakeBackInsertFunctor(m_keywords), delims);
if (!m_keywords.empty() && !delims(strings::LastUniChar(query)))
{
if (iter.IsLast() && !delims(strings::LastUniChar(query)))
m_prefix = strings::MakeLowerCase(iter.GetUniString());
else
m_keywords.push_back(strings::MakeLowerCase(iter.GetUniString()));
m_prefix.swap(m_keywords.back());
m_keywords.pop_back();
}
}

View file

@ -1,5 +1,4 @@
#pragma once
#include "../base/base.hpp"
#include "../base/buffer_vector.hpp"
#include "../base/string_utils.hpp"
@ -13,6 +12,13 @@ using strings::UniChar;
namespace impl
{
template <class DelimsT, typename F>
void SplitAndNormalizeAndSimplifyString(string const & s, F f, DelimsT const & delims)
{
for (strings::TokenizeIterator<DelimsT> iter(s, delims); iter; ++iter)
f(strings::MakeLowerCase(iter.GetUniString()));
}
struct MatchCostData
{
uint32_t m_A, m_B;