1 /*
2  * Copyright (c) 2023 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 #include "entity_recognizer.h"
16 #include "i18n_hilog.h"
17 
18 namespace OHOS {
19 namespace Global {
20 namespace I18n {
EntityRecognizer(icu::Locale & locale)21 EntityRecognizer::EntityRecognizer(icu::Locale& locale)
22 {
23     std::string region = locale.getCountry();
24     std::string language = locale.getLanguage();
25     phoneNumberMatched = new PhoneNumberMatched(region);
26     if (phoneNumberMatched == nullptr) {
27         HILOG_ERROR_I18N("PhoneNumberMatched construct failed.");
28     }
29     dateTimeMatched = new DateTimeMatched(language);
30     if (dateTimeMatched == nullptr) {
31         HILOG_ERROR_I18N("DateTimeMatched construct failed.");
32     }
33 }
34 
~EntityRecognizer()35 EntityRecognizer::~EntityRecognizer()
36 {
37     if (phoneNumberMatched != nullptr) {
38         delete phoneNumberMatched;
39     }
40     if (dateTimeMatched != nullptr) {
41         delete dateTimeMatched;
42     }
43 }
44 
FindEntityInfo(std::string & message)45 std::vector<std::vector<int>> EntityRecognizer::FindEntityInfo(std::string& message)
46 {
47     icu::UnicodeString messageStr = message.c_str();
48     messageStr = ConvertQanChar(messageStr);
49     std::vector<std::vector<int>> EntityInfo;
50     if (phoneNumberMatched == nullptr) {
51         HILOG_ERROR_I18N("FindEntityInfo failed because phoneNumberMatched is nullptr.");
52         return EntityInfo;
53     }
54     std::vector<int> phoneNumberInfo = phoneNumberMatched->GetMatchedPhoneNumber(messageStr);
55     EntityInfo.push_back(phoneNumberInfo);
56     if (dateTimeMatched == nullptr) {
57         HILOG_ERROR_I18N("FindEntityInfo failed because dateTimeMatched is nullptr.");
58         return EntityInfo;
59     }
60     std::vector<int> dateTimeInfo = dateTimeMatched->GetMatchedDateTime(messageStr);
61     EntityInfo.push_back(dateTimeInfo);
62     return EntityInfo;
63 }
64 
65 // replace full-width symbols with half-width symbols
ConvertQanChar(icu::UnicodeString & instr)66 icu::UnicodeString EntityRecognizer::ConvertQanChar(icu::UnicodeString& instr)
67 {
68     icu::UnicodeString result = "";
69     icu::UnicodeString fwchstrFirst = ":/.\∕,!()?﹡;:﹣—-【】-+={%}1234567890abcdefghi";
70     icu::UnicodeString fwchstrSecond = "jklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
71     icu::UnicodeString fwchstr = fwchstrFirst + fwchstrSecond;
72     icu::UnicodeString hwchstrFirst = ":/.\\/,!()?*;:---[]-+={%}1234567890abcdefghi";
73     icu::UnicodeString hwchstrSecond = "jklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
74     icu::UnicodeString hwchstr = hwchstrFirst + hwchstrSecond;
75     int len = instr.length();
76     for (int i = 0; i < len; i++) {
77         int index = fwchstr.indexOf(instr[i]);
78         if (index == -1) {
79             result += instr[i];
80         } else {
81             result += hwchstr[index];
82         }
83     }
84     return result;
85 }
86 } // namespace I18n
87 } // namespace Global
88 } // namespace OHOS