20 #ifndef FST_EXTENSIONS_FAR_EXTRACT_H_ 21 #define FST_EXTENSIONS_FAR_EXTRACT_H_ 35 #include <string_view> 41 std::string *okey,
int *nrep, int32_t generate_sources,
42 int i, std::string_view source_prefix,
43 std::string_view source_suffix) {
52 okey->assign(key.data(), key.size());
53 std::ostringstream source_path;
54 source_path << source_prefix;
55 if (generate_sources) {
56 source_path << std::setw(generate_sources) << std::setfill(
'0') << i;
59 if (*nrep > 0) source_path <<
'.' << *nrep;
61 source_path << source_suffix;
62 fst->
Write(source_path.str());
67 const std::string &keys, std::string_view key_separator,
68 std::string_view range_delimiter, std::string_view source_prefix,
69 std::string_view source_suffix) {
75 std::vector<std::string_view> key_vector =
78 for (
size_t k = 0; k < key_vector.size(); ++k, ++i) {
79 std::string_view key = key_vector[k];
80 std::vector<std::string_view> range_vector =
82 if (range_vector.size() == 1) {
83 if (!reader.
Find(key)) {
84 LOG(ERROR) <<
"Extract: Cannot find key " << key;
88 FarWriteFst(
fst, key, &okey, &nrep, generate_sources, i, source_prefix,
90 }
else if (range_vector.size() == 2) {
91 std::string_view begin_key = range_vector[0];
92 std::string_view end_key = range_vector[1];
93 if (begin_key.empty() || end_key.empty()) {
94 LOG(ERROR) <<
"Extract: Illegal range specification " << key;
97 if (!reader.
Find(begin_key)) {
98 LOG(ERROR) <<
"Extract: Cannot find key " << begin_key;
101 for (; !reader.
Done(); reader.
Next(), ++i) {
102 const auto &ikey = reader.
GetKey();
103 if (end_key < ikey)
break;
106 source_prefix, source_suffix);
109 LOG(ERROR) <<
"Extract: Illegal range specification " << key;
116 for (
size_t i = 1; !reader.
Done(); reader.
Next(), ++i) {
117 const auto &key = reader.
GetKey();
119 FarWriteFst(
fst, key, &okey, &nrep, generate_sources, i, source_prefix,
126 #endif // FST_EXTENSIONS_FAR_EXTRACT_H_ virtual const std::string & GetKey() const =0
void FarWriteFst(const Fst< Arc > *fst, std::string_view key, std::string *okey, int *nrep, int32_t generate_sources, int i, std::string_view source_prefix, std::string_view source_suffix)
virtual bool Done() const =0
virtual bool Write(std::ostream &strm, const FstWriteOptions &opts) const
virtual const Fst< Arc > * GetFst() const =0
internal::StringSplitter StrSplit(std::string_view full, ByAnyChar delim)
void Extract(FarReader< Arc > &reader, int32_t generate_sources, const std::string &keys, std::string_view key_separator, std::string_view range_delimiter, std::string_view source_prefix, std::string_view source_suffix)
virtual bool Find(std::string_view key)=0