Skip to content

Commit

Permalink
Removed language_detection_result and moved the necessary containers …
Browse files Browse the repository at this point in the history
…to language_detector.h
  • Loading branch information
kinarr committed Oct 10, 2023
1 parent 882ec32 commit 91c5f84
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 56 deletions.
19 changes: 13 additions & 6 deletions mediapipe/tasks/c/components/containers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,24 @@ cc_test(
],
)

cc_library(
name = "language_detection_result",
hdrs = ["language_detection_result.h"],
)

cc_library(
name = "language_detection_result_converter",
srcs = ["language_detection_result_converter.cc"],
hdrs = ["language_detection_result_converter.h"],
deps = [
":language_detection_result",
"//mediapipe/tasks/c/text/language_detector:language_detector",
"//mediapipe/tasks/cc/text/language_detector:language_detector",
],
)

cc_test(
name = "language_detection_result_converter_test",
srcs = ["language_detection_result_converter_test.cc"],
deps = [
":language_detection_result_converter",
"//mediapipe/framework/port:gtest",
"//mediapipe/tasks/c/text/language_detector:language_detector",
"//mediapipe/tasks/cc/text/language_detector:language_detector",
"@com_google_googletest//:gtest_main",
],
)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.

#include <cstdlib>

#include "mediapipe/tasks/c/components/containers/language_detection_result.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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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_

#include "mediapipe/tasks/c/components/containers/language_detection_result.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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright 2023 The MediaPipe Authors.
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 "mediapipe/tasks/c/components/containers/language_detection_result_converter.h"

#include <optional>
#include <string>

#include "mediapipe/framework/port/gtest.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 {

TEST(LanguageDetectionResultConverterTest,
ConvertsLanguageDetectionResultCustomResult) {
mediapipe::tasks::text::language_detector::LanguageDetectorResult
cpp_detector_result = {{/* language_code= */ "fr",
/* probability= */ 0.5},
{/* language_code= */ "en",
/* probability= */ 0.5}};

LanguageDetectorResult c_detector_result;
CppConvertToLanguageDetectionResult(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);
}

TEST(LanguageDetectionResultConverterTest, FreesMemory) {
mediapipe::tasks::text::language_detector::LanguageDetectorResult
cpp_detector_result = {{"fr", 0.5}};

LanguageDetectorResult c_detector_result;
CppConvertToLanguageDetectionResult(cpp_detector_result, &c_detector_result);
EXPECT_NE(c_detector_result.predictions, nullptr);

CppCloseLanguageDetectionResult(&c_detector_result);
EXPECT_EQ(c_detector_result.predictions, nullptr);
}

} // namespace mediapipe::tasks::c::components::containers
10 changes: 9 additions & 1 deletion mediapipe/tasks/c/text/language_detector/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ cc_library(
hdrs = ["language_detector.h"],
visibility = ["//visibility:public"],
deps = [
"//mediapipe/tasks/c/components/containers:language_detection_result",
"//mediapipe/tasks/c/components/containers:language_detection_result_converter",
"//mediapipe/tasks/c/components/processors:classifier_options",
"//mediapipe/tasks/c/components/processors:classifier_options_converter",
Expand Down Expand Up @@ -69,6 +68,15 @@ cc_binary(
deps = [":language_detector_lib"],
)

cc_library(
name = "language_detector",
hdrs = ["language_detector.h"],
deps = [
"//mediapipe/tasks/c/components/processors:classifier_options",
"//mediapipe/tasks/c/core:base_options",
],
)

cc_test(
name = "language_detector_test",
srcs = ["language_detector_test.cc"],
Expand Down
18 changes: 17 additions & 1 deletion mediapipe/tasks/c/text/language_detector/language_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#ifndef MEDIAPIPE_TASKS_C_TEXT_LANGUAGE_DETECTOR_LANGUAGE_DETECTOR_H_
#define MEDIAPIPE_TASKS_C_TEXT_LANGUAGE_DETECTOR_LANGUAGE_DETECTOR_H_

#include "mediapipe/tasks/c/components/containers/language_detection_result.h"
#include "mediapipe/tasks/c/components/processors/classifier_options.h"
#include "mediapipe/tasks/c/core/base_options.h"

Expand All @@ -28,6 +27,23 @@ limitations under the License.
extern "C" {
#endif

// A language code and its probability.
struct LanguageDetectorPrediction {
// An i18n language / locale code, e.g. "en" for English, "uz" for Uzbek,
// "ja"-Latn for Japanese (romaji).
char* language_code;

float probability;
};

// Task output.
struct LanguageDetectorResult {
struct LanguageDetectorPrediction* predictions;

// The count of predictions.
uint32_t predictions_count;
};

// The options for configuring a MediaPipe language detector task.
struct LanguageDetectorOptions {
// Base options for configuring MediaPipe Tasks, such as specifying the model
Expand Down

0 comments on commit 91c5f84

Please sign in to comment.