20 #ifndef FST_EXTENSIONS_MPDT_COMPOSE_H_ 21 #define FST_EXTENSIONS_MPDT_COMPOSE_H_ 32 template <
class Filter>
35 using FST1 =
typename Filter::FST1;
36 using FST2 =
typename Filter::FST2;
37 using Arc =
typename Filter::Arc;
38 using Label =
typename Arc::Label;
53 const std::vector<std::pair<Label, Label>> *parens =
nullptr,
54 const std::vector<Label> *assignments =
nullptr,
55 bool expand =
false,
bool keep_parens =
true)
56 : filter_(fst1, fst2, matcher1, matcher2),
57 parens_(parens ? *parens : std::vector<std::pair<
Label,
Label>>()),
58 assignments_(assignments ? *assignments : std::vector<
Label>()),
60 keep_parens_(keep_parens),
62 stack_(parens_, assignments_),
65 for (
const auto &pair : *parens) {
66 parens_.push_back(pair);
78 : filter_(filter.filter_, safe),
79 parens_(filter.parens_),
80 expand_(filter.expand_),
81 keep_parens_(filter.keep_parens_),
83 stack_(filter.parens_, filter.assignments_),
92 filter_.SetState(s1, s2, fs_.
GetState1());
95 if (paren_id != paren_id_) {
96 if (paren_id_ != -1) {
97 GetMatcher1()->RemoveCloseParen(parens_[paren_id_].second);
98 GetMatcher2()->RemoveCloseParen(parens_[paren_id_].second);
100 paren_id_ = paren_id;
101 if (paren_id_ != -1) {
102 GetMatcher1()->AddCloseParen(parens_[paren_id_].second);
103 GetMatcher2()->AddCloseParen(parens_[paren_id_].second);
109 const auto fs1 = filter_.FilterArc(arc1, arc2);
112 if (arc1->olabel ==
kNoLabel && arc2->ilabel) {
114 arc1->ilabel = arc2->ilabel;
115 }
else if (arc2->ilabel) {
116 arc2->olabel = arc1->ilabel;
118 return FilterParen(arc2->ilabel, fs1, fs2);
119 }
else if (arc2->ilabel ==
kNoLabel && arc1->olabel) {
121 arc2->olabel = arc1->olabel;
123 arc1->ilabel = arc2->olabel;
125 return FilterParen(arc1->olabel, fs1, fs2);
133 filter_.FilterFinal(w1, w2);
143 const auto oprops = filter_.Properties(iprops);
151 const auto stack_id = stack_.
Find(fs2.
GetState(), label);
160 std::vector<std::pair<Label, Label>> parens_;
161 std::vector<Label> assignments_;
171 template <
class Arc,
bool left_pdt = true>
174 Arc, ParenMatcher<Fst<Arc>>,
175 MPdtParenFilter<AltSequenceComposeFilter<ParenMatcher<Fst<Arc>>>>> {
186 const std::vector<std::pair<Label, Label>> &parens,
187 const std::vector<typename Arc::Label> &assignments,
188 const Fst<Arc> &ifst2,
bool expand =
false,
189 bool keep_parens =
true) {
192 filter =
new MPdtFilter(ifst1, ifst2, matcher1, matcher2, &parens,
193 &assignments, expand, keep_parens);
202 Arc, ParenMatcher<Fst<Arc>>,
203 MPdtParenFilter<SequenceComposeFilter<ParenMatcher<Fst<Arc>>>>> {
214 const std::vector<std::pair<Label, Label>> &parens,
215 const std::vector<typename Arc::Label> &assignments,
216 bool expand =
false,
bool keep_parens =
true) {
219 filter =
new MPdtFilter(ifst1, ifst2, matcher1, matcher2, &parens,
220 &assignments, expand, keep_parens);
231 : connect(connect), filter_type(filter_type) {}
244 const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
246 const std::vector<typename Arc::Label> &assignments,
const Fst<Arc> &ifst2,
252 expand, keep_parens);
255 if (opts.connect)
Connect(ofst);
268 const std::vector<std::pair<typename Arc::Label, typename Arc::Label>>
270 const std::vector<typename Arc::Label> &assignments,
MutableFst<Arc> *ofst,
275 expand, keep_parens);
278 if (opts.connect)
Connect(ofst);
283 #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)