• 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 #include "utils/grammar/utils/locale-shard-map.h"
18 
19 #include <algorithm>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "utils/i18n/locale-list.h"
25 #include "utils/i18n/locale.h"
26 #include "utils/strings/append.h"
27 
28 namespace libtextclassifier3::grammar {
29 namespace {
30 
LocaleTagsToLocaleList(const std::string & locale_tags)31 std::vector<Locale> LocaleTagsToLocaleList(const std::string& locale_tags) {
32   std::vector<Locale> locale_list;
33   for (const Locale& locale : LocaleList::ParseFrom(locale_tags).GetLocales()) {
34     if (locale.IsValid()) {
35       locale_list.emplace_back(locale);
36     }
37   }
38   std::sort(locale_list.begin(), locale_list.end(),
39             [](const Locale& a, const Locale& b) { return a < b; });
40   return locale_list;
41 }
42 
43 }  // namespace
44 
CreateLocaleShardMap(const std::vector<std::string> & locale_tags)45 LocaleShardMap LocaleShardMap::CreateLocaleShardMap(
46     const std::vector<std::string>& locale_tags) {
47   LocaleShardMap locale_shard_map;
48   for (const std::string& locale_tag : locale_tags) {
49     locale_shard_map.AddLocalTags(locale_tag);
50   }
51   return locale_shard_map;
52 }
53 
GetLocales(const int shard) const54 std::vector<Locale> LocaleShardMap::GetLocales(const int shard) const {
55   auto locale_it = shard_to_locale_data_.find(shard);
56   if (locale_it != shard_to_locale_data_.end()) {
57     return locale_it->second;
58   }
59   return std::vector<Locale>();
60 }
61 
GetNumberOfShards() const62 int LocaleShardMap::GetNumberOfShards() const {
63   return shard_to_locale_data_.size();
64 }
65 
GetShard(const std::vector<Locale> locales) const66 int LocaleShardMap::GetShard(const std::vector<Locale> locales) const {
67   for (const auto& [shard, locale_list] : shard_to_locale_data_) {
68     if (std::equal(locales.begin(), locales.end(), locale_list.begin())) {
69       return shard;
70     }
71   }
72   return 0;
73 }
74 
GetShard(const std::string & locale_tags) const75 int LocaleShardMap::GetShard(const std::string& locale_tags) const {
76   std::vector<Locale> locale_list = LocaleTagsToLocaleList(locale_tags);
77   return GetShard(locale_list);
78 }
79 
AddLocalTags(const std::string & locale_tags)80 void LocaleShardMap::AddLocalTags(const std::string& locale_tags) {
81   std::vector<Locale> locale_list = LocaleTagsToLocaleList(locale_tags);
82   int shard_id = shard_to_locale_data_.size();
83   shard_to_locale_data_.insert({shard_id, locale_list});
84 }
85 
86 }  // namespace libtextclassifier3::grammar
87