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 // Utils for creating action suggestions. 18 19 #ifndef LIBTEXTCLASSIFIER_ACTIONS_UTILS_H_ 20 #define LIBTEXTCLASSIFIER_ACTIONS_UTILS_H_ 21 22 #include <string> 23 #include <vector> 24 25 #include "actions/actions_model_generated.h" 26 #include "actions/types.h" 27 #include "annotator/types.h" 28 #include "utils/flatbuffers/flatbuffers.h" 29 #include "utils/flatbuffers/mutable.h" 30 #include "utils/utf8/unicodetext.h" 31 #include "utils/utf8/unilib.h" 32 33 namespace libtextclassifier3 { 34 35 // Fills an action suggestion from a template. 36 void FillSuggestionFromSpec(const ActionSuggestionSpec* action, 37 MutableFlatbuffer* entity_data, 38 ActionSuggestion* suggestion); 39 40 // Creates text replies from capturing matches. 41 void SuggestTextRepliesFromCapturingMatch( 42 const MutableFlatbufferBuilder* entity_data_builder, 43 const RulesModel_::RuleActionSpec_::RuleCapturingGroup* group, 44 const UnicodeText& match_text, const std::string& smart_reply_action_type, 45 std::vector<ActionSuggestion>* actions); 46 47 // Applies normalization to a capturing match. 48 UnicodeText NormalizeMatchText( 49 const UniLib& unilib, 50 const RulesModel_::RuleActionSpec_::RuleCapturingGroup* group, 51 StringPiece match_text); 52 53 UnicodeText NormalizeMatchText( 54 const UniLib& unilib, 55 const RulesModel_::RuleActionSpec_::RuleCapturingGroup* group, 56 const UnicodeText match_text); 57 58 // Fills the fields in an annotation from a capturing match. 59 bool FillAnnotationFromCapturingMatch( 60 const CodepointSpan& span, 61 const RulesModel_::RuleActionSpec_::RuleCapturingGroup* group, 62 const int message_index, StringPiece match_text, 63 ActionSuggestionAnnotation* annotation); 64 65 // Merges entity data from a capturing match. 66 // Parses and sets values from the text and merges fixed data. 67 bool MergeEntityDataFromCapturingMatch( 68 const RulesModel_::RuleActionSpec_::RuleCapturingGroup* group, 69 StringPiece match_text, MutableFlatbuffer* buffer); 70 71 // Changes datetime classifications to a time type if no date component is 72 // present. Modifies classifications in-place. 73 void ConvertDatetimeToTime(std::vector<AnnotatedSpan>* annotations); 74 75 } // namespace libtextclassifier3 76 77 #endif // LIBTEXTCLASSIFIER_ACTIONS_UTILS_H_ 78