• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_ACTIONS_REGEX_ACTIONS_H_
18 #define LIBTEXTCLASSIFIER_ACTIONS_REGEX_ACTIONS_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "actions/actions_model_generated.h"
25 #include "actions/types.h"
26 #include "utils/flatbuffers.h"
27 #include "utils/utf8/unilib.h"
28 #include "utils/zlib/zlib.h"
29 
30 namespace libtextclassifier3 {
31 
32 // Regular expression backed actions suggestions.
33 class RegexActions {
34  public:
RegexActions(const UniLib * unilib,const std::string & smart_reply_action_type)35   explicit RegexActions(const UniLib* unilib,
36                         const std::string& smart_reply_action_type)
37       : unilib_(*unilib), smart_reply_action_type_(smart_reply_action_type) {}
38 
39   // Decompresses and initializes all rules in a model.
40   bool InitializeRules(
41       const RulesModel* rules, const RulesModel* low_confidence_rules,
42       const TriggeringPreconditions* triggering_preconditions_overlay,
43       ZlibDecompressor* decompressor);
44 
45   // Checks whether the input triggers the low confidence rules.
46   bool IsLowConfidenceInput(
47       const Conversation& conversation, const int num_messages,
48       std::vector<const UniLib::RegexPattern*>* post_check_rules) const;
49 
50   // Checks and filters suggestions triggering the low confidence post checks.
51   bool FilterConfidenceOutput(
52       const std::vector<const UniLib::RegexPattern*>& post_check_rules,
53       std::vector<ActionSuggestion>* actions) const;
54 
55   // Suggests actions for a conversation from a message stream using the regex
56   // rules.
57   bool SuggestActions(const Conversation& conversation,
58                       const ReflectiveFlatbufferBuilder* entity_data_builder,
59                       std::vector<ActionSuggestion>* actions) const;
60 
61  private:
62   struct CompiledRule {
63     const RulesModel_::RegexRule* rule;
64     std::unique_ptr<UniLib::RegexPattern> pattern;
65     std::unique_ptr<UniLib::RegexPattern> output_pattern;
CompiledRuleCompiledRule66     CompiledRule(const RulesModel_::RegexRule* rule,
67                  std::unique_ptr<UniLib::RegexPattern> pattern,
68                  std::unique_ptr<UniLib::RegexPattern> output_pattern)
69         : rule(rule),
70           pattern(std::move(pattern)),
71           output_pattern(std::move(output_pattern)) {}
72   };
73 
74   // Decompresses and initializes a set of regular expression rules.
75   bool InitializeRulesModel(const RulesModel* rules,
76                             ZlibDecompressor* decompressor,
77                             std::vector<CompiledRule>* compiled_rules) const;
78 
79   const UniLib& unilib_;
80   const std::string smart_reply_action_type_;
81   std::vector<CompiledRule> rules_, low_confidence_rules_;
82 };
83 
84 }  // namespace libtextclassifier3
85 
86 #endif  // LIBTEXTCLASSIFIER_ACTIONS_REGEX_ACTIONS_H_
87