• 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_TYPES_H_
18 #define LIBTEXTCLASSIFIER_ACTIONS_TYPES_H_
19 
20 #include <map>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "actions/actions-entity-data_generated.h"
26 #include "annotator/types.h"
27 #include "utils/flatbuffers/flatbuffers.h"
28 
29 namespace libtextclassifier3 {
30 
31 // A text span in the conversation.
32 struct MessageTextSpan {
33   explicit MessageTextSpan() = default;
MessageTextSpanMessageTextSpan34   MessageTextSpan(const int message_index, const CodepointSpan span,
35                   const std::string& text)
36       : message_index(message_index), span(span), text(text) {}
37 
38   // The referenced message.
39   // -1 if not referencing a particular message in the provided input.
40   int message_index = kInvalidIndex;
41 
42   // The span within the reference message.
43   // (-1, -1) if not referencing a particular location.
44   CodepointSpan span = CodepointSpan{kInvalidIndex, kInvalidIndex};
45 
46   // The span text.
47   std::string text;
48 };
49 
50 // An entity associated with an action.
51 struct ActionSuggestionAnnotation {
52   MessageTextSpan span;
53   ClassificationResult entity;
54 
55   // Optional annotation name.
56   std::string name;
57 };
58 
59 // A slot associated with an action {
60 struct Slot {
61   std::string type;
62   MessageTextSpan span;
63   float confidence_score;
64 };
65 
66 // Action suggestion that contains a response text and the type of the response.
67 struct ActionSuggestion {
68   // Text of the action suggestion.
69   std::string response_text;
70 
71   // Type of the action suggestion.
72   std::string type;
73 
74   // Score.
75   float score = 0.f;
76 
77   // Priority score for internal conflict resolution.
78   float priority_score = 0.f;
79 
80   // The associated annotations.
81   std::vector<ActionSuggestionAnnotation> annotations;
82 
83   // Extras information.
84   std::string serialized_entity_data;
85 
86   // Slots corresponding to the action suggestion.
87   std::vector<Slot> slots;
88 
entity_dataActionSuggestion89   const ActionsEntityData* entity_data() const {
90     return LoadAndVerifyFlatbuffer<ActionsEntityData>(
91         serialized_entity_data.data(), serialized_entity_data.size());
92   }
93 };
94 
95 // Options for suggesting actions.
96 struct ActionSuggestionOptions {
DefaultActionSuggestionOptions97   static ActionSuggestionOptions Default() { return ActionSuggestionOptions(); }
98   std::unordered_map<std::string, Variant> model_parameters;
99 };
100 
101 // Actions suggestions result containing meta - information and the suggested
102 // actions.
103 struct ActionsSuggestionsResponse {
104   // The sensitivity assessment.
105   float sensitivity_score = -1.f;
106   float triggering_score = -1.f;
107 
108   // Whether the input conversation is considered as sensitive.
109   bool is_sensitive = false;
110 
111   // Whether the output was suppressed by the triggering score threshold.
112   bool output_filtered_min_triggering_score = false;
113 
114   // Whether the output was suppressed by the low confidence patterns.
115   bool output_filtered_low_confidence = false;
116 
117   // Whether the output was suppressed due to locale mismatch.
118   bool output_filtered_locale_mismatch = false;
119 
120   // The suggested actions.
121   std::vector<ActionSuggestion> actions;
122 };
123 
124 // Represents a single message in the conversation.
125 struct ConversationMessage {
126   // User ID distinguishing the user from other users in the conversation.
127   int user_id = 0;
128 
129   // Text of the message.
130   std::string text;
131 
132   // Reference time of this message.
133   int64 reference_time_ms_utc = 0;
134 
135   // Timezone in which the input text was written (format as accepted by ICU).
136   std::string reference_timezone;
137 
138   // Annotations on the text.
139   std::vector<AnnotatedSpan> annotations;
140 
141   // Comma-separated list of BCP 47 language tags of the message.
142   std::string detected_text_language_tags;
143 };
144 
145 // Conversation between multiple users.
146 struct Conversation {
147   // Sequence of messages that were exchanged in the conversation.
148   std::vector<ConversationMessage> messages;
149 };
150 
151 }  // namespace libtextclassifier3
152 
153 #endif  // LIBTEXTCLASSIFIER_ACTIONS_TYPES_H_
154