20 #ifndef FST_EXTENSIONS_FAR_EXTRACT_H_ 21 #define FST_EXTENSIONS_FAR_EXTRACT_H_ 31 #include <string_view> 37 std::string *okey,
int *nrep, int32_t generate_sources,
38 int i, std::string_view source_prefix,
39 std::string_view source_suffix) {
48 okey->assign(key.data(), key.size());
49 std::ostringstream source_path;
50 source_path << source_prefix;
51 if (generate_sources) {
52 source_path << std::setw(generate_sources) << std::setfill(
'0') << i;
55 if (*nrep > 0) source_path <<
'.' << *nrep;
57 source_path << source_suffix;
58 fst->
Write(source_path.str());
63 const std::string &keys,
const std::string &key_separator,
64 const std::string &range_delimiter,
65 const std::string &source_prefix,
66 const std::string &source_suffix) {
72 std::vector<std::string_view> key_vector =
75 for (
size_t k = 0; k < key_vector.size(); ++k, ++i) {
76 std::string_view key = key_vector[k];
77 std::vector<std::string_view> range_vector =
79 if (range_vector.size() == 1) {
80 if (!reader.
Find(key)) {
81 LOG(ERROR) <<
"Extract: Cannot find key " << key;
85 FarWriteFst(
fst, key, &okey, &nrep, generate_sources, i, source_prefix,
87 }
else if (range_vector.size() == 2) {
88 std::string_view begin_key = range_vector[0];
89 std::string_view end_key = range_vector[1];
90 if (begin_key.empty() || end_key.empty()) {
91 LOG(ERROR) <<
"Extract: Illegal range specification " << key;
94 if (!reader.
Find(begin_key)) {
95 LOG(ERROR) <<
"Extract: Cannot find key " << begin_key;
98 for (; !reader.
Done(); reader.
Next(), ++i) {
99 const auto &ikey = reader.
GetKey();
100 if (end_key < ikey)
break;
103 source_prefix, source_suffix);
106 LOG(ERROR) <<
"Extract: Illegal range specification " << key;
113 for (
size_t i = 1; !reader.
Done(); reader.
Next(), ++i) {
114 const auto &key = reader.
GetKey();
116 FarWriteFst(
fst, key, &okey, &nrep, generate_sources, i, source_prefix,
123 #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, const std::string &key_separator, const std::string &range_delimiter, const std::string &source_prefix, const std::string &source_suffix)
virtual bool Find(std::string_view key)=0