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
|
#pragma once
#include "search/model.hpp"
#include "search/query_params.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
{
class QueryParams;
namespace impl
{
bool FullMatch(QueryParams::Token const & token, strings::UniString const & text);
bool PrefixMatch(QueryParams::Token const & token, strings::UniString const & text);
} // 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 (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;
if (impl::FullMatch(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 search
|