blocky/conv/fprob_fcprob.cc

67 lines
1.5 KiB
C++

#include <fstream>
#include <iostream>
#include <string>
#include <args.hxx>
#include "common.hh"
namespace param {
using namespace ::args;
ArgumentParser parser {
"converter: feature codes -> feature indices"
};
HelpFlag help {
parser, "help", "display this menu", {'h', "help"},
};
ValueFlag<std::string> smap {
parser, "path", "step map file path", {"smap"},
};
ValueFlag<std::string> fcmap {
parser, "path", "feature code map", {"fc-map"},
};
} // namespace param
static void Exec() {
const auto fprobs = ReadMatrix<double>(std::cin);
Enforce(fprobs.size() > 0 && fprobs[0].size() > 0, "empty matrix");
std::ifstream fcmap_st {args::get(param::fcmap)};
Enforce(!!fcmap_st, "fcmap path is invalid");
const auto fcmap = ReadMatrix<uint32_t>(fcmap_st);
Enforce(fcmap.size() > 0, "empty fcmap");
for (auto& idxs : fcmap) {
Enforce(idxs.size() > 0, "fcmap has empty item");
}
for (size_t t = 0; t < fprobs.size(); ++t) {
Enforce(fprobs[t].size() <= fcmap.size(), "unmatched fprobs and fcmap");
for (size_t c = 0; c < fcmap.size(); ++c) {
double sum = 0;
for (auto i : fcmap[c]) {
sum += fprobs[t][i];
}
std::cout << sum / fcmap[c].size() << ' ';
}
std::cout << '\n';
}
}
int main(int argc, char** argv)
try {
param::parser.ParseCLI(argc, argv);
Exec();
return EXIT_SUCCESS;
} catch (const args::Help&) {
std::cout << param::parser << std::endl;
return EXIT_SUCCESS;
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}