From 62a7a68025422b60a97df6cfaf24609a5de6d3e8 Mon Sep 17 00:00:00 2001 From: takenori-y Date: Tue, 28 May 2024 10:26:28 +0900 Subject: [PATCH] strip -e option --- src/main/onehot.cc | 69 +++++-------------------------------------- test/test_onehot.bats | 4 +-- 2 files changed, 10 insertions(+), 63 deletions(-) diff --git a/src/main/onehot.cc b/src/main/onehot.cc index d413b6e..4b4f4b8 100644 --- a/src/main/onehot.cc +++ b/src/main/onehot.cc @@ -25,10 +25,7 @@ namespace { -enum WarningType { kIgnore = 0, kWarn, kExit, kNumWarningTypes }; - const int kDefaultVectorLength(10); -const WarningType kDefaultWarningType(kIgnore); void PrintUsage(std::ostream* stream) { // clang-format off @@ -40,11 +37,6 @@ void PrintUsage(std::ostream* stream) { *stream << " options:" << std::endl; *stream << " -l l : length of vector (double)[" << std::setw(5) << std::right << kDefaultVectorLength << "][ 1 <= l <= ]" << std::endl; // NOLINT *stream << " -m m : order of vector (double)[" << std::setw(5) << std::right << "l-1" << "][ 0 <= m <= ]" << std::endl; // NOLINT - *stream << " -e e : out-of-range error ( int)[" << std::setw(5) << std::right << kDefaultWarningType << "][ 0 <= e <= 2 ]" << std::endl; // NOLINT - *stream << " 0 (no warning)" << std::endl; - *stream << " 1 (output the index to stderr)" << std::endl; - *stream << " 2 (output the index to stderr and" << std::endl; - *stream << " exit immediately)" << std::endl; *stream << " -h : print this message" << std::endl; *stream << " infile:" << std::endl; *stream << " 0-based index ( int)[stdin]" << std::endl; @@ -65,11 +57,6 @@ void PrintUsage(std::ostream* stream) { * - length of vector @f$(1 \le L)@f$ * - @b -m @e int * - order of vector @f$(0 \le L - 1)@f$ - * - @b -e @e int - * - warning type - * \arg @c 0 no warning - * \arg @c 1 output index - * \arg @c 2 output index and exit immediately * - @b infile @e str * - int-type 0-based index * - @b stdout @@ -86,10 +73,9 @@ void PrintUsage(std::ostream* stream) { */ int main(int argc, char* argv[]) { int vector_length(kDefaultVectorLength); - WarningType warning_type(kDefaultWarningType); for (;;) { - const int option_char(getopt_long(argc, argv, "l:m:e:h", NULL, NULL)); + const int option_char(getopt_long(argc, argv, "l:m:h", NULL, NULL)); if (-1 == option_char) break; switch (option_char) { @@ -116,21 +102,6 @@ int main(int argc, char* argv[]) { ++vector_length; break; } - case 'e': { - const int min(0); - const int max(static_cast(kNumWarningTypes) - 1); - int tmp; - if (!sptk::ConvertStringToInteger(optarg, &tmp) || - !sptk::IsInRange(tmp, min, max)) { - std::ostringstream error_message; - error_message << "The argument for the -e option must be an integer " - << "in the range of " << min << " to " << max; - sptk::PrintErrorMessage("onehot", error_message); - return 1; - } - warning_type = static_cast(tmp); - break; - } case 'h': { PrintUsage(&std::cout); return 0; @@ -175,44 +146,20 @@ int main(int argc, char* argv[]) { for (int sample_index(0); sptk::ReadStream(&index, &input_stream); ++sample_index) { - const bool is_valid(0 <= index && index < vector_length); - if (is_valid) { - onehot_vector[index] = 1.0; - } else { - switch (warning_type) { - case kIgnore: { - // nothing to do - break; - } - case kWarn: { - std::ostringstream error_message; - error_message << sample_index << "th sample is out of range"; - sptk::PrintErrorMessage("onehot", error_message); - break; - } - case kExit: { - std::ostringstream error_message; - error_message << sample_index << "th sample is out of range"; - sptk::PrintErrorMessage("onehot", error_message); - return 1; - } - default: { - std::ostringstream error_message; - error_message << "Unknown warning type"; - sptk::PrintErrorMessage("onehot", error_message); - return 1; - } - } + if (index < 0 || vector_length <= index) { + std::ostringstream error_message; + error_message << sample_index << "th sample is out of range"; + sptk::PrintErrorMessage("onehot", error_message); + return 1; } + onehot_vector[index] = 1.0; if (!sptk::WriteStream(0, vector_length, onehot_vector, &std::cout, NULL)) { std::ostringstream error_message; error_message << "Failed to write one-hot vector"; sptk::PrintErrorMessage("onehot", error_message); return 1; } - if (is_valid) { - onehot_vector[index] = 0.0; - } + onehot_vector[index] = 0.0; } return 0; diff --git a/test/test_onehot.bats b/test/test_onehot.bats index c6a95a3..6eafbe8 100755 --- a/test/test_onehot.bats +++ b/test/test_onehot.bats @@ -28,8 +28,8 @@ teardown() { } @test "onehot: compatibility" { - echo 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 | $sptk3/x2x +ad > $tmp/1 - $sptk3/ramp -s -1 -e 3 | $sptk3/x2x +di | $sptk4/onehot -l 3 > $tmp/2 + echo 1 0 0 0 1 0 0 0 1 | $sptk3/x2x +ad > $tmp/1 + $sptk3/ramp -s 0 -e 2 | $sptk3/x2x +di | $sptk4/onehot -l 3 > $tmp/2 run $sptk4/aeq $tmp/1 $tmp/2 [ "$status" -eq 0 ] }