48 std::string usage =
"Converts an RTN represented by FSTs";
49 usage +=
" and non-terminal labels into PDT.\n\n Usage: ";
51 usage +=
" root.fst rootlabel [rule1.fst label1 ...] [out.fst]\n";
53 SET_FLAGS(usage.c_str(), &argc, &argv,
true);
59 const std::string out_name = argc % 2 == 0 ? argv[argc - 1] :
"";
64 LOG(ERROR) << argv[0] <<
": Unknown PDT parser type: " 65 << FST_FLAGS_pdt_parser_type;
69 std::vector<std::pair<int64_t, std::unique_ptr<const FstClass>>> pairs;
70 for (
auto i = 1; i < argc - 1; i += 2) {
71 std::unique_ptr<const FstClass> ifst(FstClass::Read(argv[i]));
75 const auto label = atoll(argv[i + 1]);
76 pairs.emplace_back(label, std::move(ifst));
80 LOG(ERROR) << argv[0] <<
"At least one replace pair must be provided.";
83 const auto root = pairs.front().first;
84 VectorFstClass ofst(pairs.back().second->ArcType());
85 std::vector<std::pair<int64_t, int64_t>> parens;
87 FST_FLAGS_start_paren_labels,
88 FST_FLAGS_left_paren_prefix,
89 FST_FLAGS_right_paren_prefix);
91 if (!FST_FLAGS_pdt_parentheses.empty() &&
96 return !ofst.Write(out_name);
void ShowUsage(bool long_usage=true)
bool GetPdtParserType(std::string_view str, PdtParserType *pt)
void Replace(const std::vector< std::pair< int64_t, const FstClass * >> &pairs, MutableFstClass *ofst, std::vector< std::pair< int64_t, int64_t >> *parens, int64_t root, PdtParserType parser_type, int64_t start_paren_labels, const std::string &left_paren_prefix, const std::string &right_paren_prefix)
bool WriteLabelPairs(std::string_view source, const std::vector< std::pair< Label, Label >> &pairs)
int pdtreplace_main(int argc, char **argv)
#define SET_FLAGS(usage, argc, argv, rmflags)
DECLARE_string(pdt_parentheses)
std::vector< std::pair< int64_t, const FstClass * > > BorrowPairs(const std::vector< std::pair< int64_t, std::unique_ptr< const FstClass >>> &pairs)
DECLARE_int64(start_paren_labels)