Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ranking_utils.hpp « v2 « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d0d1d2c65f730fa4d42e467e9c0003dba9bb5b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once

#include "search/query_params.hpp"
#include "search/v2/search_model.hpp"

#include "indexer/search_delimiters.hpp"
#include "indexer/search_string_utils.hpp"

#include "base/stl_add.hpp"
#include "base/string_utils.hpp"

#include "std/cstdint.hpp"
#include "std/limits.hpp"
#include "std/string.hpp"
#include "std/vector.hpp"

namespace search
{
struct QueryParams;

namespace v2
{
namespace impl
{
bool Match(vector<strings::UniString> const & tokens, strings::UniString const & token);

bool PrefixMatch(vector<strings::UniString> const & prefixes, strings::UniString const & token);
}  // namespace impl

// The order and numeric values are important here.  Please, check all
// use-cases before changing this enum.
enum NameScore
{
  NAME_SCORE_ZERO = 0,
  NAME_SCORE_SUBSTRING_PREFIX = 1,
  NAME_SCORE_SUBSTRING = 2,
  NAME_SCORE_FULL_MATCH_PREFIX = 3,
  NAME_SCORE_FULL_MATCH = 4,

  NAME_SCORE_COUNT
};

template <typename TSlice>
NameScore GetNameScore(string const & name, TSlice const & slice)
{
  if (slice.Empty())
    return NAME_SCORE_ZERO;

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

template <typename TSlice>
NameScore GetNameScore(vector<strings::UniString> const & tokens, TSlice 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 (int offset = 0; offset + m <= n; ++offset)
  {
    bool match = true;
    for (int i = 0; i < m - 1 && match; ++i)
      match = match && impl::Match(slice.Get(i), tokens[offset + i]);
    if (!match)
      continue;

    if (impl::Match(slice.Get(m - 1), tokens[offset + m - 1]))
    {
      if (m == n)
        return NAME_SCORE_FULL_MATCH;
      score = max(score, NAME_SCORE_SUBSTRING);
    }
    if (lastTokenIsPrefix && impl::PrefixMatch(slice.Get(m - 1), tokens[offset + m - 1]))
    {
      if (m == n)
        return NAME_SCORE_FULL_MATCH_PREFIX;
      score = max(score, NAME_SCORE_SUBSTRING_PREFIX);
    }
  }
  return score;
}

string DebugPrint(NameScore score);
}  // namespace v2
}  // namespace search