From 3433ba083a2a24c2b4c661099111b6fa6497fc17 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 30 Nov 2023 15:56:39 -0800 Subject: [PATCH] Move LanguageDetectorResult converter to LanguageDetector task PiperOrigin-RevId: 586812754 --- mediapipe/tasks/c/components/containers/BUILD | 23 ----------------- .../tasks/c/text/language_detector/BUILD | 25 ++++++++++++++++++- .../language_detector/language_detector.cc | 12 ++++----- .../language_detector_result_converter.cc} | 6 ++--- .../language_detector_result_converter.h} | 10 ++++---- ...anguage_detector_result_converter_test.cc} | 16 ++++++------ 6 files changed, 46 insertions(+), 46 deletions(-) rename mediapipe/tasks/c/{components/containers/language_detection_result_converter.cc => text/language_detector/language_detector_result_converter.cc} (90%) rename mediapipe/tasks/c/{components/containers/language_detection_result_converter.h => text/language_detector/language_detector_result_converter.h} (78%) rename mediapipe/tasks/c/{components/containers/language_detection_result_converter_test.cc => text/language_detector/language_detector_result_converter_test.cc} (77%) diff --git a/mediapipe/tasks/c/components/containers/BUILD b/mediapipe/tasks/c/components/containers/BUILD index a75e8b22f..3c4b557b3 100644 --- a/mediapipe/tasks/c/components/containers/BUILD +++ b/mediapipe/tasks/c/components/containers/BUILD @@ -212,26 +212,3 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) - -cc_library( - name = "language_detection_result_converter", - srcs = ["language_detection_result_converter.cc"], - hdrs = ["language_detection_result_converter.h"], - deps = [ - "//mediapipe/tasks/c/text/language_detector", - "//mediapipe/tasks/cc/text/language_detector", - ], -) - -cc_test( - name = "language_detection_result_converter_test", - srcs = ["language_detection_result_converter_test.cc"], - linkstatic = 1, - deps = [ - ":language_detection_result_converter", - "//mediapipe/framework/port:gtest", - "//mediapipe/tasks/c/text/language_detector", - "//mediapipe/tasks/cc/text/language_detector", - "@com_google_googletest//:gtest_main", - ], -) diff --git a/mediapipe/tasks/c/text/language_detector/BUILD b/mediapipe/tasks/c/text/language_detector/BUILD index 9a3ce21e7..f4a674a35 100644 --- a/mediapipe/tasks/c/text/language_detector/BUILD +++ b/mediapipe/tasks/c/text/language_detector/BUILD @@ -22,7 +22,7 @@ cc_library( hdrs = ["language_detector.h"], visibility = ["//visibility:public"], deps = [ - "//mediapipe/tasks/c/components/containers:language_detection_result_converter", + ":language_detector_result_converter", "//mediapipe/tasks/c/components/processors:classifier_options", "//mediapipe/tasks/c/components/processors:classifier_options_converter", "//mediapipe/tasks/c/core:base_options", @@ -77,6 +77,29 @@ cc_library( ], ) +cc_library( + name = "language_detector_result_converter", + srcs = ["language_detector_result_converter.cc"], + hdrs = ["language_detector_result_converter.h"], + deps = [ + ":language_detector", + "//mediapipe/tasks/cc/text/language_detector", + ], +) + +cc_test( + name = "language_detector_result_converter_test", + srcs = ["language_detector_result_converter_test.cc"], + linkstatic = 1, + deps = [ + ":language_detector", + ":language_detector_result_converter", + "//mediapipe/framework/port:gtest", + "//mediapipe/tasks/cc/text/language_detector", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "language_detector_test", srcs = ["language_detector_test.cc"], diff --git a/mediapipe/tasks/c/text/language_detector/language_detector.cc b/mediapipe/tasks/c/text/language_detector/language_detector.cc index c71433fdc..ec2aba521 100644 --- a/mediapipe/tasks/c/text/language_detector/language_detector.cc +++ b/mediapipe/tasks/c/text/language_detector/language_detector.cc @@ -20,9 +20,9 @@ limitations under the License. #include "absl/log/absl_log.h" #include "absl/status/status.h" -#include "mediapipe/tasks/c/components/containers/language_detection_result_converter.h" #include "mediapipe/tasks/c/components/processors/classifier_options_converter.h" #include "mediapipe/tasks/c/core/base_options_converter.h" +#include "mediapipe/tasks/c/text/language_detector/language_detector_result_converter.h" #include "mediapipe/tasks/cc/text/language_detector/language_detector.h" namespace mediapipe::tasks::c::text::language_detector { @@ -30,9 +30,9 @@ namespace mediapipe::tasks::c::text::language_detector { namespace { using ::mediapipe::tasks::c::components::containers:: - CppCloseLanguageDetectionResult; + CppCloseLanguageDetectorResult; using ::mediapipe::tasks::c::components::containers:: - CppConvertToLanguageDetectionResult; + CppConvertToLanguageDetectorResult; using ::mediapipe::tasks::c::components::processors:: CppConvertToClassifierOptions; using ::mediapipe::tasks::c::core::CppConvertToBaseOptions; @@ -72,16 +72,16 @@ int CppLanguageDetectorDetect(void* detector, const char* utf8_str, auto cpp_detector = static_cast(detector); auto cpp_result = cpp_detector->Detect(utf8_str); if (!cpp_result.ok()) { - ABSL_LOG(ERROR) << "Language Detection failed: " << cpp_result.status(); + ABSL_LOG(ERROR) << "Language Detector failed: " << cpp_result.status(); return CppProcessError(cpp_result.status(), error_msg); } - CppConvertToLanguageDetectionResult(*cpp_result, result); + CppConvertToLanguageDetectorResult(*cpp_result, result); return 0; } void CppLanguageDetectorCloseResult(LanguageDetectorResult* result) { - CppCloseLanguageDetectionResult(result); + CppCloseLanguageDetectorResult(result); } int CppLanguageDetectorClose(void* detector, char** error_msg) { diff --git a/mediapipe/tasks/c/components/containers/language_detection_result_converter.cc b/mediapipe/tasks/c/text/language_detector/language_detector_result_converter.cc similarity index 90% rename from mediapipe/tasks/c/components/containers/language_detection_result_converter.cc rename to mediapipe/tasks/c/text/language_detector/language_detector_result_converter.cc index 89b112e45..435c3d1dc 100644 --- a/mediapipe/tasks/c/components/containers/language_detection_result_converter.cc +++ b/mediapipe/tasks/c/text/language_detector/language_detector_result_converter.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#include "mediapipe/tasks/c/components/containers/language_detection_result_converter.h" +#include "mediapipe/tasks/c/text/language_detector/language_detector_result_converter.h" #include #include @@ -23,7 +23,7 @@ limitations under the License. namespace mediapipe::tasks::c::components::containers { -void CppConvertToLanguageDetectionResult( +void CppConvertToLanguageDetectorResult( const mediapipe::tasks::text::language_detector::LanguageDetectorResult& in, LanguageDetectorResult* out) { out->predictions_count = in.size(); @@ -42,7 +42,7 @@ void CppConvertToLanguageDetectionResult( } } -void CppCloseLanguageDetectionResult(LanguageDetectorResult* in) { +void CppCloseLanguageDetectorResult(LanguageDetectorResult* in) { for (uint32_t i = 0; i < in->predictions_count; ++i) { auto prediction_in = in->predictions[i]; diff --git a/mediapipe/tasks/c/components/containers/language_detection_result_converter.h b/mediapipe/tasks/c/text/language_detector/language_detector_result_converter.h similarity index 78% rename from mediapipe/tasks/c/components/containers/language_detection_result_converter.h rename to mediapipe/tasks/c/text/language_detector/language_detector_result_converter.h index c9cfd55bd..c8b0c04ed 100644 --- a/mediapipe/tasks/c/components/containers/language_detection_result_converter.h +++ b/mediapipe/tasks/c/text/language_detector/language_detector_result_converter.h @@ -13,20 +13,20 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#ifndef MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_CONVERTER_H_ -#define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_CONVERTER_H_ +#ifndef MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTOR_RESULT_CONVERTER_H_ +#define MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTOR_RESULT_CONVERTER_H_ #include "mediapipe/tasks/c/text/language_detector/language_detector.h" #include "mediapipe/tasks/cc/text/language_detector/language_detector.h" namespace mediapipe::tasks::c::components::containers { -void CppConvertToLanguageDetectionResult( +void CppConvertToLanguageDetectorResult( const mediapipe::tasks::text::language_detector::LanguageDetectorResult& in, LanguageDetectorResult* out); -void CppCloseLanguageDetectionResult(LanguageDetectorResult* in); +void CppCloseLanguageDetectorResult(LanguageDetectorResult* in); } // namespace mediapipe::tasks::c::components::containers -#endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTION_RESULT_CONVERTER_H_ +#endif // MEDIAPIPE_TASKS_C_COMPONENTS_CONTAINERS_LANGUAGE_DETECTOR_RESULT_CONVERTER_H_ diff --git a/mediapipe/tasks/c/components/containers/language_detection_result_converter_test.cc b/mediapipe/tasks/c/text/language_detector/language_detector_result_converter_test.cc similarity index 77% rename from mediapipe/tasks/c/components/containers/language_detection_result_converter_test.cc rename to mediapipe/tasks/c/text/language_detector/language_detector_result_converter_test.cc index 633b77eae..4c93aa232 100644 --- a/mediapipe/tasks/c/components/containers/language_detection_result_converter_test.cc +++ b/mediapipe/tasks/c/text/language_detector/language_detector_result_converter_test.cc @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ -#include "mediapipe/tasks/c/components/containers/language_detection_result_converter.h" +#include "mediapipe/tasks/c/text/language_detector/language_detector_result_converter.h" #include "mediapipe/framework/port/gtest.h" #include "mediapipe/tasks/c/text/language_detector/language_detector.h" @@ -21,8 +21,8 @@ limitations under the License. namespace mediapipe::tasks::c::components::containers { -TEST(LanguageDetectionResultConverterTest, - ConvertsLanguageDetectionResultCustomResult) { +TEST(LanguageDetectorResultConverterTest, + ConvertsLanguageDetectorResultCustomResult) { mediapipe::tasks::text::language_detector::LanguageDetectorResult cpp_detector_result = {{/* language_code= */ "fr", /* probability= */ 0.5}, @@ -30,24 +30,24 @@ TEST(LanguageDetectionResultConverterTest, /* probability= */ 0.5}}; LanguageDetectorResult c_detector_result; - CppConvertToLanguageDetectionResult(cpp_detector_result, &c_detector_result); + CppConvertToLanguageDetectorResult(cpp_detector_result, &c_detector_result); EXPECT_NE(c_detector_result.predictions, nullptr); EXPECT_EQ(c_detector_result.predictions_count, 2); EXPECT_NE(c_detector_result.predictions[0].language_code, "fr"); EXPECT_EQ(c_detector_result.predictions[0].probability, 0.5); - CppCloseLanguageDetectionResult(&c_detector_result); + CppCloseLanguageDetectorResult(&c_detector_result); } -TEST(LanguageDetectionResultConverterTest, FreesMemory) { +TEST(LanguageDetectorResultConverterTest, FreesMemory) { mediapipe::tasks::text::language_detector::LanguageDetectorResult cpp_detector_result = {{"fr", 0.5}}; LanguageDetectorResult c_detector_result; - CppConvertToLanguageDetectionResult(cpp_detector_result, &c_detector_result); + CppConvertToLanguageDetectorResult(cpp_detector_result, &c_detector_result); EXPECT_NE(c_detector_result.predictions, nullptr); - CppCloseLanguageDetectionResult(&c_detector_result); + CppCloseLanguageDetectorResult(&c_detector_result); EXPECT_EQ(c_detector_result.predictions, nullptr); }