• 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(
52       const jstring device_locales, const ClassificationResult& classification,
53       const int64 reference_time_ms_utc, const std::string& text,
54       const CodepointSpan selection_indices, const jobject context,
55       const reflection::Schema* annotations_entity_data_schema,
56       const bool enable_add_contact_intent, const bool enable_search_intent,
57       std::vector<RemoteActionTemplate>* remote_actions) const;
58 
59   // Generates intents for an action suggestion.
60   // Returns true, if the intent generator snippets could be successfully run,
61   // returns false otherwise.
62   bool GenerateIntents(const jstring device_locales,
63                        const ActionSuggestion& action,
64                        const Conversation& conversation, const jobject context,
65                        const reflection::Schema* annotations_entity_data_schema,
66                        const reflection::Schema* actions_entity_data_schema,
67                        std::vector<RemoteActionTemplate>* remote_actions) const;
68 
69  private:
IntentGenerator(const IntentFactoryModel * options,const ResourcePool * resources,const std::shared_ptr<JniCache> & jni_cache)70   IntentGenerator(const IntentFactoryModel* options,
71                   const ResourcePool* resources,
72                   const std::shared_ptr<JniCache>& jni_cache)
73       : options_(options),
74         resources_(Resources(resources)),
75         jni_cache_(jni_cache) {}
76 
77   std::vector<Locale> ParseDeviceLocales(const jstring device_locales) const;
78 
79   const IntentFactoryModel* options_;
80   const Resources resources_;
81   std::shared_ptr<JniCache> jni_cache_;
82   std::map<std::string, std::string> generators_;
83 };
84 
85 }  // namespace libtextclassifier3
86 
87 #endif  // LIBTEXTCLASSIFIER_UTILS_INTENTS_INTENT_GENERATOR_H_
88