aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/attribute/dfa_fuzzy_matcher.h
blob: 8e5b3ce0ccdb79e81cd1c1072abe89dec0494126 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "dfa_string_comparator.h"
#include <vespa/vespalib/datastore/atomic_entry_ref.h>
#include <vespa/vespalib/fuzzy/levenshtein_dfa.h>
#include <iostream>

namespace search::attribute {

/**
 * Class that uses a LevenshteinDfa to fuzzy match a target word against words in a dictionary.
 *
 * The dictionary iterator is advanced based on the successor string from the DFA
 * each time the candidate word is _not_ a match.
 */
class DfaFuzzyMatcher {
private:
    vespalib::fuzzy::LevenshteinDfa _dfa;
    std::vector<uint32_t>           _successor;
    std::vector<uint32_t>           _prefix;
    uint32_t                        _prefix_size;
    bool                            _cased;

    const char* skip_prefix(const char* word) const;
public:
    DfaFuzzyMatcher(std::string_view target, uint8_t max_edits, uint32_t prefix_size, bool cased, vespalib::fuzzy::LevenshteinDfa::DfaType dfa_type);
    ~DfaFuzzyMatcher();

    bool is_match(const char *word) const;

    /*
     * If prefix size is nonzero then this variant of is_match()
     * should only be called with words that starts with the extracted
     * prefix of the target word.
     *
     * Caller must position iterator at right location using lower bound
     * functionality in the dictionary.
     */
    template <typename DictionaryConstIteratorType>
    bool is_match(const char* word, DictionaryConstIteratorType& itr, const DfaStringComparator::DataStoreType& data_store) {
        if (_prefix_size > 0) {
            word = skip_prefix(word);
            if (_prefix.size() < _prefix_size) {
                if (*word == '\0') {
                    return true;
                }
                _successor.resize(_prefix.size());
                _successor.emplace_back(1);
            } else {
                _successor.resize(_prefix.size());
                auto match = _dfa.match(word, _successor);
                if (match.matches()) {
                    return true;
                }
            }
        } else {
            _successor.clear();
            auto match = _dfa.match(word, _successor);
            if (match.matches()) {
                return true;
            }
        }
        DfaStringComparator cmp(data_store, _successor);
        itr.seek(vespalib::datastore::AtomicEntryRef(), cmp);
        return false;
    }
};

}