• 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 
18 #ifndef LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_
19 #define LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_
20 
21 #include <jni.h>
22 
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include "actions/types.h"
29 #include "annotator/types.h"
30 #include "utils/i18n/locale.h"
31 #include "utils/intents/intent-config_generated.h"
32 #include "utils/intents/remote-action-template.h"
33 #include "utils/java/jni-cache.h"
34 #include "utils/resources.h"
35 #include "utils/resources_generated.h"
36 #include "utils/strings/stringpiece.h"
37 #include "flatbuffers/reflection_generated.h"
38 
39 namespace libtextclassifier3 {
40 
41 // Helper class to generate Android intents for text classifier results.
42 class IntentGenerator {
43  public:
44   static std::unique_ptr<IntentGenerator> Create(
45       const IntentFactoryModel* options, const ResourcePool* resources,
46       const std::shared_ptr<JniCache>& jni_cache);
47 
48   // Generates intents for a classification result.
49   // Returns true, if the intent generator snippets could be successfully run,
50   // returns false otherwise.
51   bool GenerateIntents(const jstring device_locales,
52                        const ClassificationResult& classification,
53                        const int64 reference_time_ms_utc,
54                        const std::string& text,
55                        const CodepointSpan selection_indices,
56                        const jobject context,
57                        const reflection::Schema* annotations_entity_data_schema,
58                        std::vector<RemoteActionTemplate>* remote_actions) const;
59 
60   // Generates intents for an action suggestion.
61   // Returns true, if the intent generator snippets could be successfully run,
62   // returns false otherwise.
63   bool GenerateIntents(const jstring device_locales,
64                        const ActionSuggestion& action,
65                        const Conversation& conversation, const jobject context,
66                        const reflection::Schema* annotations_entity_data_schema,
67                        const reflection::Schema* actions_entity_data_schema,
68                        std::vector<RemoteActionTemplate>* remote_actions) const;
69 
70  private:
IntentGenerator(const IntentFactoryModel * options,const ResourcePool * resources,const std::shared_ptr<JniCache> & jni_cache)71   IntentGenerator(const IntentFactoryModel* options,
72                   const ResourcePool* resources,
73                   const std::shared_ptr<JniCache>& jni_cache)
74       : options_(options),
75         resources_(Resources(resources)),
76         jni_cache_(jni_cache) {}
77 
78   std::vector<Locale> ParseDeviceLocales(const jstring device_locales) const;
79 
80   const IntentFactoryModel* options_;
81   const Resources resources_;
82   std::shared_ptr<JniCache> jni_cache_;
83   std::map<std::string, std::string> generators_;
84 };
85 
86 }  // namespace libtextclassifier3
87 
88 #endif  // LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_
89