• 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_UTILS_RESOURCES_H_
18 #define LIBTEXTCLASSIFIER_UTILS_RESOURCES_H_
19 
20 #include <vector>
21 
22 #include "utils/i18n/language-tag_generated.h"
23 #include "utils/i18n/locale.h"
24 #include "utils/resources_generated.h"
25 #include "utils/strings/stringpiece.h"
26 
27 namespace libtextclassifier3 {
28 
29 // Class for accessing localized model resources.
30 class Resources {
31  public:
Resources(const ResourcePool * resources)32   explicit Resources(const ResourcePool* resources) : resources_(resources) {}
33 
34   // Returns the string value associated with the particular resource.
35   // `locales` are locales in preference order.
36   bool GetResourceContent(const std::vector<Locale>& locales,
37                           const StringPiece resource_name,
38                           std::string* result) const;
39 
40  private:
41   // Match priorities: language > script > region with wildcard matches being
42   // weaker than an exact match.
43   // For a resource lookup, at least language needs to (weakly) match.
44   // c.f. developer.android.com/guide/topics/resources/multilingual-support
45   enum LocaleMatch {
46     LOCALE_NO_MATCH = 0,
47     LOCALE_REGION_WILDCARD_MATCH = 1 << 0,
48     LOCALE_REGION_MATCH = 1 << 1,
49     LOCALE_SCRIPT_WILDCARD_MATCH = 1 << 2,
50     LOCALE_SCRIPT_MATCH = 1 << 3,
51     LOCALE_LANGUAGE_WILDCARD_MATCH = 1 << 4,
52     LOCALE_LANGUAGE_MATCH = 1 << 5
53   };
54   int LocaleMatch(const Locale& locale, const LanguageTag* entry_locale) const;
55 
56   // Finds a resource entry by name.
57   const ResourceEntry* FindResource(const StringPiece resource_name) const;
58 
59   // Finds the best locale matching resource from a resource entry.
60   int BestResourceForLocales(const ResourceEntry* resource,
61                              const std::vector<Locale>& locales) const;
62 
63   const ResourcePool* resources_;
64 };
65 
66 // Compresses resources in place.
67 bool CompressResources(ResourcePoolT* resources,
68                        const bool build_compression_dictionary = false,
69                        const int dictionary_sample_every = 1);
70 std::string CompressSerializedResources(
71     const std::string& resources,
72     const bool build_compression_dictionary = false,
73     const int dictionary_sample_every = 1);
74 
75 bool DecompressResources(ResourcePoolT* resources,
76                          const bool build_compression_dictionary = false);
77 
78 }  // namespace libtextclassifier3
79 
80 #endif  // LIBTEXTCLASSIFIER_UTILS_RESOURCES_H_
81