Skip to content

Commit

Permalink
strip -e option
Browse files Browse the repository at this point in the history
  • Loading branch information
takenori-y committed May 28, 2024
1 parent 71707c3 commit 62a7a68
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 63 deletions.
69 changes: 8 additions & 61 deletions src/main/onehot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -116,21 +102,6 @@ int main(int argc, char* argv[]) {
++vector_length;
break;
}
case 'e': {
const int min(0);
const int max(static_cast<int>(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<WarningType>(tmp);
break;
}
case 'h': {
PrintUsage(&std::cout);
return 0;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/test_onehot.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
}
Expand Down

0 comments on commit 62a7a68

Please sign in to comment.