xref: /aosp_15_r20/external/icing/icing/expand/expander.h (revision 8b6cd535a057e39b3b86660c4aa06c99747c2136)
1 // Copyright (C) 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ICING_EXPAND_EXPANDER_H_
16 #define ICING_EXPAND_EXPANDER_H_
17 
18 #include <string>
19 #include <string_view>
20 #include <utility>
21 #include <vector>
22 
23 namespace icing {
24 namespace lib {
25 
26 // Struct to hold the text of a term, and whether it has undergone certain
27 // expansion operations.
28 struct ExpandedTerm {
29   std::string text;
30   bool is_stemmed_term;
31 
ExpandedTermExpandedTerm32   explicit ExpandedTerm(std::string text_in, bool is_stemmed_term_in)
33       : text(std::move(text_in)), is_stemmed_term(is_stemmed_term_in) {}
34 
35   bool operator==(const ExpandedTerm& other) const {
36     return text == other.text && is_stemmed_term == other.is_stemmed_term;
37   }
38 };
39 
40 class Expander {
41  public:
42   virtual ~Expander() = default;
43 
44   // Expands a given term into a vector of expanded terms. The first term in the
45   // output vector is always the original term.
46   //
47   // See implementation classes for specific expansion behaviors.
48   virtual std::vector<ExpandedTerm> Expand(std::string_view term) const = 0;
49 };
50 
51 }  // namespace lib
52 }  // namespace icing
53 
54 #endif  // ICING_EXPAND_EXPANDER_H_
55