• 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_LUA_ACTIONS_H_
18 #define LIBTEXTCLASSIFIER_ACTIONS_LUA_ACTIONS_H_
19 
20 #include "actions/actions_model_generated.h"
21 #include "actions/lua-utils.h"
22 #include "actions/types.h"
23 #include "utils/lua-utils.h"
24 #include "utils/tensor-view.h"
25 #include "utils/tflite-model-executor.h"
26 
27 namespace libtextclassifier3 {
28 
29 // Lua backed actions suggestions.
30 class LuaActionsSuggestions : public LuaEnvironment {
31  public:
32   static std::unique_ptr<LuaActionsSuggestions> CreateLuaActionsSuggestions(
33       const std::string& snippet, const Conversation& conversation,
34       const TfLiteModelExecutor* model_executor,
35       const TensorflowLiteModelSpec* model_spec,
36       const tflite::Interpreter* interpreter,
37       const reflection::Schema* actions_entity_data_schema,
38       const reflection::Schema* annotations_entity_data_schema);
39 
40   bool SuggestActions(std::vector<ActionSuggestion>* actions);
41 
42  private:
43   // Model tensor lua iterator.
44   class TensorViewIterator
45       : public LuaEnvironment::ItemIterator<TensorView<float>> {
46    public:
TensorViewIterator()47     explicit TensorViewIterator() {}
48     int Item(const TensorView<float>* tensor, const int64 index,
49              lua_State* state) const override;
50   };
51 
52   LuaActionsSuggestions(
53       const std::string& snippet, const Conversation& conversation,
54       const TfLiteModelExecutor* model_executor,
55       const TensorflowLiteModelSpec* model_spec,
56       const tflite::Interpreter* interpreter,
57       const reflection::Schema* actions_entity_data_schema,
58       const reflection::Schema* annotations_entity_data_schema);
59 
60   bool Initialize();
61 
62   const std::string& snippet_;
63   const Conversation& conversation_;
64   ConversationIterator conversation_iterator_;
65   TensorViewIterator tensor_iterator_;
66   TensorView<float> actions_scores_;
67   TensorView<float> smart_reply_scores_;
68   TensorView<float> sensitivity_score_;
69   TensorView<float> triggering_score_;
70   const reflection::Schema* actions_entity_data_schema_;
71   const reflection::Schema* annotations_entity_data_schema_;
72 };
73 
74 }  // namespace libtextclassifier3
75 
76 #endif  // LIBTEXTCLASSIFIER_ACTIONS_LUA_ACTIONS_H_
77