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