20 #ifndef FST_EXTENSIONS_MPDT_COMPOSE_H_ 21 #define FST_EXTENSIONS_MPDT_COMPOSE_H_ 23 #include <sys/types.h> 42 template <
class Filter>
45 using FST1 =
typename Filter::FST1;
46 using FST2 =
typename Filter::FST2;
47 using Arc =
typename Filter::Arc;
48 using Label =
typename Arc::Label;
63 const std::vector<std::pair<Label, Label>> *parens =
nullptr,
64 const std::vector<Label> *assignments =
nullptr,
65 bool expand =
false,
bool keep_parens =
true)
66 : filter_(fst1, fst2, matcher1, matcher2),
67 parens_(parens ? *parens : std::vector<std::pair<
Label,
Label>>()),
68 assignments_(assignments ? *assignments : std::vector<
Label>()),
70 keep_parens_(keep_parens),
72 stack_(parens_, assignments_),
75 for (
const auto &pair : *parens) {
76 parens_.push_back(pair);
88 : filter_(filter.filter_, safe),
89 parens_(filter.parens_),
90 expand_(filter.expand_),
91 keep_parens_(filter.keep_parens_),
93 stack_(filter.parens_, filter.assignments_),
102 filter_.SetState(s1, s2, fs_.
GetState1());
103 if (!expand_)
return;
105 if (paren_id != paren_id_) {
106 if (paren_id_ != -1) {
107 GetMatcher1()->RemoveCloseParen(parens_[paren_id_].second);
108 GetMatcher2()->RemoveCloseParen(parens_[paren_id_].second);
110 paren_id_ = paren_id;
111 if (paren_id_ != -1) {
112 GetMatcher1()->AddCloseParen(parens_[paren_id_].second);
113 GetMatcher2()->AddCloseParen(parens_[paren_id_].second);
119 const auto fs1 = filter_.FilterArc(arc1, arc2);
122 if (arc1->olabel ==
kNoLabel && arc2->ilabel) {
124 arc1->ilabel = arc2->ilabel;
125 }
else if (arc2->ilabel) {
126 arc2->olabel = arc1->ilabel;
128 return FilterParen(arc2->ilabel, fs1, fs2);
129 }
else if (arc2->ilabel ==
kNoLabel && arc1->olabel) {
131 arc2->olabel = arc1->olabel;
133 arc1->ilabel = arc2->olabel;
135 return FilterParen(arc1->olabel, fs1, fs2);
143 filter_.FilterFinal(w1, w2);
153 const auto oprops = filter_.Properties(iprops);
161 const auto stack_id = stack_.
Find(fs2.
GetState(), label);
170 std::vector<std::pair<Label, Label>> parens_;
171 std::vector<Label> assignments_;
181 template <
class Arc,
bool left_pdt = true>
184 Arc, ParenMatcher<Fst<Arc>>,
185 MPdtParenFilter<AltSequenceComposeFilter<ParenMatcher<Fst<Arc>>>>> {
196 const std::vector<std::pair<Label, Label>> &parens,
197 const std::vector<typename Arc::Label> &assignments,
198 const Fst<Arc> &ifst2,
bool expand =
false,
199 bool keep_parens =
true) {
202 filter =
new MPdtFilter(ifst1, ifst2, matcher1, matcher2, &parens,
203 &assignments, expand, keep_parens);
212 Arc, ParenMatcher<Fst<Arc>>,
213 MPdtParenFilter<SequenceComposeFilter<ParenMatcher<Fst<Arc>>>>> {
224 const std::vector<std::pair<Label, Label>> &parens,
225 const std::vector<typename Arc::Label> &assignments,
226 bool expand =
false,
bool keep_parens =
true) {
229 filter =
new MPdtFilter(ifst1, ifst2, matcher1, matcher2, &parens,
230 &assignments, expand, keep_parens);
241 : connect(connect), filter_type(filter_type) {}
254 const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
256 const std::vector<typename Arc::Label> &assignments,
const Fst<Arc> &ifst2,
262 expand, keep_parens);
265 if (opts.connect)
Connect(ofst);
278 const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
280 const std::vector<typename Arc::Label> &assignments,
MutableFst<Arc> *ofst,
285 expand, keep_parens);
288 if (opts.connect)
Connect(ofst);
293 #endif // FST_EXTENSIONS_MPDT_COMPOSE_H_ MPdtComposeFstOptions(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, const std::vector< std::pair< Label, Label >> &parens, const std::vector< typename Arc::Label > &assignments, bool expand=false, bool keep_parens=true)
constexpr uint32_t kParenLoop
typename SequenceComposeFilter< ParenMatcher< Fst< Arc > > >::Arc Arc
typename SequenceComposeFilter< ParenMatcher< Fst< Arc > > >::Matcher1 Matcher1
ssize_t Top(StackId stack_id) const
MPdtParenFilter(const MPdtParenFilter &filter, bool safe=false)
MPdtComposeFstOptions(const Fst< Arc > &ifst1, const std::vector< std::pair< Label, Label >> &parens, const std::vector< typename Arc::Label > &assignments, const Fst< Arc > &ifst2, bool expand=false, bool keep_parens=true)
IntegerFilterState< StackId > FilterState2
void FilterFinal(Weight *w1, Weight *w2) const
void Connect(MutableFst< Arc > *fst)
typename SequenceComposeFilter< ParenMatcher< Fst< Arc > > >::Matcher2 Matcher2
typename Arc::Label Label
constexpr uint32_t kParenList
typename Arc::Label Label
typename SequenceComposeFilter< ParenMatcher< Fst< Arc > > >::FilterState FilterState1
static const PairFilterState NoState()
FilterState FilterArc(Arc *arc1, Arc *arc2) const
typename SequenceComposeFilter< ParenMatcher< Fst< Arc > > >::FST2 FST2
void Compose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const ComposeOptions &opts=ComposeOptions())
constexpr uint64_t kOLabelInvariantProperties
MPdtParenFilter(const FST1 &fst1, const FST2 &fst2, Matcher1 *matcher1=nullptr, Matcher2 *matcher2=nullptr, const std::vector< std::pair< Label, Label >> *parens=nullptr, const std::vector< Label > *assignments=nullptr, bool expand=false, bool keep_parens=true)
void SetState(StateId s1, StateId s2, const FilterState &fs)
constexpr uint64_t kILabelInvariantProperties
FilterState Start() const
typename Arc::StateId StateId
MPdtComposeOptions(bool connect=true, PdtComposeFilter filter_type=PdtComposeFilter::PAREN)
typename Arc::Label Label
const FilterState1 & GetState1() const
PairFilterState< FilterState1, FilterState2 > FilterState
PdtComposeFilter filter_type
uint64_t Properties(uint64_t iprops) const
const FilterState2 & GetState2() const
typename Arc::Weight Weight
typename SequenceComposeFilter< ParenMatcher< Fst< Arc > > >::FST1 FST1
StackId Find(StackId stack_id, Label label)