From 2882076463327b80284e9cf26bde2141246395a7 Mon Sep 17 00:00:00 2001 From: zhaolinglan Date: Wed, 16 Jul 2025 15:50:17 +0800 Subject: [PATCH] test text prediction Signed-off-by: zhaolinglan --- .../include/text_predictor.h | 36 +++++++ .../src/input_method_ability.cpp | 2 + .../src/text_predictor.cpp | 97 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 frameworks/native/inputmethod_ability/include/text_predictor.h create mode 100644 frameworks/native/inputmethod_ability/src/text_predictor.cpp diff --git a/frameworks/native/inputmethod_ability/include/text_predictor.h b/frameworks/native/inputmethod_ability/include/text_predictor.h new file mode 100644 index 000000000..a48155bda --- /dev/null +++ b/frameworks/native/inputmethod_ability/include/text_predictor.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IMF_TEXT_PREDICTOR_H +#define IMF_TEXT_PREDICTOR_H + +#include +#include +#include + +namespace OHOS { +namespace MiscServices { +class TextPredictor { +public: + static void UpdateContent(const std::string &text); + +private: + static std::vector FindKeywords(const std::string &text); + static std::string KeywordsToString(const std::vector &keywords); +}; +} // namespace MiscServices +} // namespace OHOS + +#endif // IMF_TEXT_PREDICTOR_H diff --git a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp index b05eff733..6c0cde5b0 100644 --- a/frameworks/native/inputmethod_ability/src/input_method_ability.cpp +++ b/frameworks/native/inputmethod_ability/src/input_method_ability.cpp @@ -37,6 +37,7 @@ #include "task_manager.h" #include "tasks/task.h" #include "tasks/task_imsa.h" +#include "text_predictor.h" #include "variant_util.h" namespace OHOS { @@ -432,6 +433,7 @@ void InputMethodAbility::OnSelectionChange( IMSA_HILOGE("kdListener_ is nullptr!"); return; } + TextPredictor::UpdateContent(Str16ToStr8(text)); kdListener_->OnTextChange(Str16ToStr8(text)); kdListener_->OnSelectionChange(oldBegin, oldEnd, newBegin, newEnd); } diff --git a/frameworks/native/inputmethod_ability/src/text_predictor.cpp b/frameworks/native/inputmethod_ability/src/text_predictor.cpp new file mode 100644 index 000000000..6d07c120c --- /dev/null +++ b/frameworks/native/inputmethod_ability/src/text_predictor.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "text_predictor.h" + +#include + +#include "global.h" + +namespace OHOS { +namespace MiscServices { +void TextPredictor::UpdateContent(const std::string &text) +{ + if (text.empty()) { + IMSA_HILOGE(""); + return; + } + auto keywords = FindKeywords(text); + if (keywords.empty()) { + IMSA_HILOGI(""); + return; + } + // call Recommendation Engine + IMSA_HILOGI("end"); +} + +std::vector TextPredictor::FindKeywords(const std::string &text) +{ + IMSA_HILOGI("input: %{public}s", text.c_str()); + // date pattern + std::regex datePattern(R"((今天|明天|后天|周一|周二|周三|周四|周五|周六|周日|\d{1,4}年\d{1,2}月\d{1,2}日))"); + // time pattern + std::regex timePattern1(R"((上午|下午|晚上|傍晚))"); + std::regex timePattern2(R"((\d{1,4}点\d{1,2}分|\d{1,2}点半|\d{1,2}点一刻))"); + // event pattern + std::regex eventPattern(R"((会议|电话|聚餐|面试|讲座|约会|报告|航班|高铁))"); + + std::vector results; + + // get date + std::sregex_iterator dateIt(text.begin(), text.end(), datePattern); + std::sregex_iterator dateEnd; + while (dateIt != dateEnd) { + results.push_back(dateIt->str()); + ++dateIt; + } + + // get time + std::sregex_iterator timeIt(text.begin(), text.end(), timePattern1); + std::sregex_iterator timeEnd; + while (timeIt != timeEnd) { + results.push_back(timeIt->str()); + ++timeIt; + } + std::sregex_iterator timeIt2(text.begin(), text.end(), timePattern2); + std::sregex_iterator timeEnd2; + while (timeIt2 != timeEnd2) { + results.push_back(timeIt2->str()); + ++timeIt2; + } + + // get event + std::sregex_iterator eventIt(text.begin(), text.end(), eventPattern); + std::sregex_iterator eventEnd; + while (eventIt != eventEnd) { + results.push_back(eventIt->str()); + ++eventIt; + } + + IMSA_HILOGI("keywords: %{public}s", KeywordsToString(results).c_str()); + return results; +} + +std::string TextPredictor::KeywordsToString(const std::vector &keywords) +{ + std::string str = "["; + for (const auto &keyword : keywords) { + str.append(keyword); + str.append(" "); + } + str.append("]"); + return str; +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file -- Gitee