• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 
17 #include "i18n_entity_impl.h"
18 #include "unicode/utypes.h"
19 #include "unicode/locid.h"
20 #include "utils.h"
21 #include "entity_recognizer.h"
22 #include "i18n_ffi.h"
23 #include "i18n_hilog.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 
CEntity(std::string locale)29 CEntity::CEntity(std::string locale)
30 {
31     UErrorCode localeStatus = U_ZERO_ERROR;
32     icu::Locale localeTag = icu::Locale::forLanguageTag(locale, localeStatus);
33     if (!IsValidLocaleTag(localeTag)) {
34         entityRecognizer_ = nullptr;
35         return;
36     }
37     entityRecognizer_ = std::make_unique<EntityRecognizer>(localeTag);
38     if (entityRecognizer_ == nullptr) {
39         HILOG_ERROR_I18N("Create EntityRecognizer fail");
40     }
41 }
42 
EntityInfoItems2C(const std::vector<EntityInfoItem> & items,CEntityInfoItemArr & arr)43 static int32_t EntityInfoItems2C(const std::vector<EntityInfoItem>& items, CEntityInfoItemArr &arr)
44 {
45     int32_t size = static_cast<int32_t>(items.size());
46     if (size > 0) {
47         arr.head = static_cast<CEntityInfoItem*>(malloc(sizeof(CEntityInfoItem) * size));
48         if (arr.head == nullptr) {
49             return -1;
50         }
51         arr.size = size;
52 
53         for (int32_t i = 0; i < size; ++i) {
54             const EntityInfoItem& item = items[i];
55             CEntityInfoItem cItem;
56             cItem.type = MallocCString(item.type);
57             cItem.begin = item.begin;
58             cItem.end = item.end;
59 
60             if (cItem.type == nullptr) {
61                 return -1;
62             }
63 
64             arr.head[i] = cItem;
65         }
66     } else {
67         arr.head = nullptr;
68         arr.size = 0;
69     }
70     return 0;
71 }
72 
FindEntityInfo(std::string text)73 CEntityInfoItemArr CEntity::FindEntityInfo(std::string text)
74 {
75     std::vector<std::vector<int>> entityInfo = entityRecognizer_->FindEntityInfo(text);
76     std::vector<std::string> types = {"phone_number", "date"};
77     std::vector<EntityInfoItem> items;
78     CEntityInfoItemArr arr = { 0, nullptr };
79     int index = 0;
80     for (std::string::size_type t = 0; t < types.size(); t++) {
81         for (int i = 0; i < entityInfo[t][0]; i++) {
82             int begin = entityInfo[t][2 * i + 1];
83             int end = entityInfo[t][2 * i + 2];
84             std::string type = types[t];
85             EntityInfoItem item = { type, begin, end };
86             items.push_back(item);
87             index++;
88         }
89     }
90     EntityInfoItems2C(items, arr);
91     return arr;
92 }
93 
InitSuccess()94 bool CEntity::InitSuccess()
95 {
96     return entityRecognizer_ != nullptr;
97 }
98 
99 }  // namespace I18n
100 }  // namespace Global
101 }  // namespace OHOS
102