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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "string_search_helper.h"
#include "dfa_fuzzy_matcher.h"
#include "i_enum_store_dictionary.h"
#include <vespa/searchlib/query/query_term_ucs4.h>
#include <vespa/vespalib/text/lowercase.h>
#include <vespa/vespalib/text/utf8.h>
#include <vespa/vespalib/fuzzy/fuzzy_matcher.h>
namespace search::attribute {
using FMA = vespalib::FuzzyMatchingAlgorithm;
using LDT = vespalib::fuzzy::LevenshteinDfa::DfaType;
namespace {
LDT
to_dfa_type(FMA algorithm)
{
switch (algorithm) {
case FMA::DfaImplicit:
return LDT::Implicit;
case FMA::DfaExplicit:
return LDT::Explicit;
case FMA::DfaTable:
return LDT::Table;
default:
return LDT::Implicit;
}
}
}
StringSearchHelper::StringSearchHelper(QueryTermUCS4 & term, bool cased, vespalib::FuzzyMatchingAlgorithm fuzzy_matching_algorithm)
: _regex(),
_fuzzyMatcher(),
_dfa_fuzzy_matcher(),
_term(),
_termLen(),
_isPrefix(term.isPrefix()),
_isRegex(term.isRegex()),
_isCased(cased),
_isFuzzy(term.isFuzzy())
{
if (isRegex()) {
_regex = (isCased())
? vespalib::Regex::from_pattern(term.getTerm(), vespalib::Regex::Options::None)
: vespalib::Regex::from_pattern(term.getTerm(), vespalib::Regex::Options::IgnoreCase);
} else if (isFuzzy()) {
const auto max_edit_dist = term.fuzzy_max_edit_distance();
_fuzzyMatcher = std::make_unique<vespalib::FuzzyMatcher>(term.getTerm(),
max_edit_dist,
term.fuzzy_prefix_lock_length(),
isCased(),
term.fuzzy_prefix_match());
if ((fuzzy_matching_algorithm != FMA::BruteForce) &&
(max_edit_dist > 0 && max_edit_dist <= 2)) {
_dfa_fuzzy_matcher = std::make_unique<DfaFuzzyMatcher>(term.getTerm(),
max_edit_dist,
term.fuzzy_prefix_lock_length(),
isCased(),
term.fuzzy_prefix_match(),
to_dfa_type(fuzzy_matching_algorithm));
}
} else if (isCased()) {
_term = term.getTerm();
_termLen = strlen(_term);
} else {
_ucs4 = term.asUcs4();
}
}
StringSearchHelper::StringSearchHelper(StringSearchHelper&&) noexcept = default;
StringSearchHelper::~StringSearchHelper() = default;
bool
StringSearchHelper::isMatch(const char *src) const noexcept {
if (__builtin_expect(isRegex(), false)) {
return getRegex().valid() && getRegex().partial_match(std::string_view(src));
}
if (__builtin_expect(isFuzzy(), false)) {
return _dfa_fuzzy_matcher ? _dfa_fuzzy_matcher->is_match(std::string_view(src))
: getFuzzyMatcher().isMatch(std::string_view(src));
}
if (__builtin_expect(isCased(), false)) {
int res = strncmp(_term, src, _termLen);
return (res == 0) && (src[_termLen] == 0 || isPrefix());
}
vespalib::Utf8ReaderForZTS u8reader(src);
uint32_t j = 0;
uint32_t val;
for (;; ++j) {
val = u8reader.getChar();
val = vespalib::LowerCase::convert(val);
if (_ucs4[j] == 0 || _ucs4[j] != val) {
break;
}
}
return (_ucs4[j] == 0 && (val == 0 || isPrefix()));
}
template <typename DictionaryConstIteratorType>
bool
StringSearchHelper::is_fuzzy_match(const char* word, DictionaryConstIteratorType& itr, const DfaStringComparator::DataStoreType& data_store) const
{
if (_dfa_fuzzy_matcher) {
return _dfa_fuzzy_matcher->is_match(word, itr, data_store);
} else {
if (_fuzzyMatcher->isMatch(word)) {
return true;
}
++itr;
return false;
}
}
template
bool
StringSearchHelper::is_fuzzy_match(const char*, EnumPostingTree::ConstIterator&, const DfaStringComparator::DataStoreType&) const;
template
bool
StringSearchHelper::is_fuzzy_match(const char*, EnumTree::ConstIterator&, const DfaStringComparator::DataStoreType&) const;
}
|