Example #1
0
NameScore GetNameScore(std::vector<strings::UniString> const & tokens, Slice const & slice)
{
  if (slice.Empty())
    return NAME_SCORE_ZERO;

  size_t const n = tokens.size();
  size_t const m = slice.Size();

  bool const lastTokenIsPrefix = slice.IsPrefix(m - 1);

  NameScore score = NAME_SCORE_ZERO;
  for (size_t offset = 0; offset + m <= n; ++offset)
  {
    bool match = true;
    for (size_t i = 0; i < m - 1 && match; ++i)
      match = match && impl::FullMatch(slice.Get(i), tokens[offset + i]);
    if (!match)
      continue;

    bool const fullMatch = impl::FullMatch(slice.Get(m - 1), tokens[offset + m - 1]);
    bool const prefixMatch =
        lastTokenIsPrefix && impl::PrefixMatch(slice.Get(m - 1), tokens[offset + m - 1]);
    if (!fullMatch && !prefixMatch)
      continue;

    if (m == n && fullMatch)
      return NAME_SCORE_FULL_MATCH;

    if (offset == 0)
      score = std::max(score, NAME_SCORE_PREFIX);

    score = std::max(score, NAME_SCORE_SUBSTRING);
  }
  return score;
}
Example #2
0
NameScore GetNameScore(std::string const & name, Slice const & slice)
{
  if (slice.Empty())
    return NAME_SCORE_ZERO;

  std::vector<strings::UniString> tokens;
  SplitUniString(NormalizeAndSimplifyString(name), base::MakeBackInsertFunctor(tokens), Delimiters());
  return GetNameScore(tokens, slice);
}