Awali
Another Weighted Automata library
letterize_tape.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_TDC_LAW_TO_LAN_HH
18 #define AWALI_ALGOS_TDC_LAW_TO_LAN_HH
19 
21 #include <awali/sttc/algos/projection.hh> // tuple_to_tupleset
23 #include <awali/sttc/misc/add_epsilon_trans.hh> // is_epsilon
25 #include<unordered_map>
26 
27 /*
28  Convert a transducer with words on tape I
29  into a transducer with epsilon or letters on this tape.
30 */
31 
32 namespace awali {
33  namespace sttc {
34 
35  namespace internal {
36  template<typename T> struct map_nullable;
37  template<typename Letterset, typename... T>
38  struct map_nullable<tupleset<Letterset, T...>> {
41 
42  static tupleT gettuple(const std::tuple<Letterset, T...>& t) { return std::tuple_cat(std::make_tuple(get_nullableset(std::get<0>(t))), map_nullable<tupleset<T...>>::gettuple (tuple_tail(t))); }
43 
44  static type get(const tupleset<Letterset, T...>& ts) { return {gettuple(ts.sets())}; }
45 
46  };
47 
48  template<>
49  struct map_nullable<tupleset<>> {
50  using tupleT = std::tuple<>;
51  using type = tupleset<>;
52 
53  static tupleT gettuple(const std::tuple<>& t) { return t; }
54  static type get(const tupleset<>& ts) { return ts; }
55  };
56 
57  template <typename T, size_t I, typename U> struct replace_in_tupleset;
58 
59  template <typename... T, size_t I, typename U>
60  struct replace_in_tupleset<tupleset<T...>, I, U>{
61  using tp_t = tupleset<T...>;
63 
64  static
65  type get(const tp_t& t, const U& v) {
66  return {replace_in_tuple<I,U>::get(t.sets(), v)};
67  }
68  };
69 
70  template <typename Tdc, unsigned I, typename Labelset>
71  struct tdc_letterizer {
74  using Ilabelset_t = typename labelset_t::template valueset_t<I>;
77 
79  using nullable_label_t = typename nullable_labelset_t::value_t;
80 
84  using ret_label_t = typename ret_labelset_t::value_t;
85 
86  static ret_transducer_t tdc_letterize(const Tdc& tdc, bool keep_history) {
87  const labelset_t& in_labelset=*tdc->context().labelset();
88  nletterset_t nletset = get_nullableset(get_letterset(in_labelset.template set<I>()));
89  nullable_labelset_t null_labelset=map_nullable<labelset_t>::get(in_labelset);
91  auto ret= make_mutable_automaton(ret_context_t{ret_labelset,*tdc->context().weightset()});
92  copy_into(tdc, ret, keep_history);
93  return ret;
94  }
95  };
96 
97  template <typename Tdc, unsigned I, typename L>
98  struct tdc_letterizer<Tdc, I, wordset<L>> {
99  using transducer_t = Tdc;
102  using Ilabelset_t = typename labelset_t::template valueset_t<I>;
105 
107  using nullable_label_t = typename nullable_labelset_t::value_t;
108 
112  using ret_label_t = typename ret_labelset_t::value_t;
113 
114  static ret_transducer_t tdc_letterize(const transducer_t& tdc, bool keep_history) {
115  const labelset_t& in_labelset=*tdc->context().labelset();
116  nletterset_t nletset = get_nullableset(get_letterset(in_labelset.template set<I>()));
117  nullable_labelset_t null_labelset=map_nullable<labelset_t>::get(in_labelset);
119  auto ret= make_mutable_automaton(ret_context_t{ret_labelset,*tdc->context().weightset()});
120  std::unordered_map<state_t,state_t> state_map;
121  auto epsilon = ret_labelset.one();
122  for(auto s : tdc->states()) {
123  state_map[s]=ret->add_state();
124  }
125  state_map[ret->pre()]=tdc->pre();
126  state_map[ret->post()]=tdc->post();
127  for(auto tr: tdc->all_transitions()) {
128  nullable_label_t l=tdc->label_of(tr);
129  const auto& w=std::get<I>(l);
130  if(w.length()==0)
131  ret->new_transition(state_map[tdc->src_of(tr)], state_map[tdc->dst_of(tr)], replace_in_tuple<I,typename nletterset_t::value_t>::get(l,nletset.one()),tdc->weight_of(tr));
132  else if (w.length()==1)
133  ret->new_transition(state_map[tdc->src_of(tr)], state_map[tdc->dst_of(tr)], replace_in_tuple<I,typename nletterset_t::value_t>::get(l,w[0]), tdc->weight_of(tr));
134  else {
135  state_t dst=state_map[tdc->dst_of(tr)];
136  for(unsigned i=w.length()-1; i>0;i--) {
137  state_t src=ret->add_state();
138  std::get<I>(epsilon)=w[i];
139  ret->new_transition(src, dst, epsilon);
140  dst=src;
141  }
142  ret->new_transition(state_map[tdc->src_of(tr)], dst, replace_in_tuple<I,typename nletterset_t::value_t>::get(l,w[0]), tdc->weight_of(tr));
143  }
144  }
145  if(keep_history) {
146  auto sg_history = std::make_shared<single_history<transducer_t>>(tdc);
147  for(auto p : state_map) {
148  sg_history->add_state(p.second,p.first);
149  if(tdc->has_name(p.first)) {
150  ret->set_state_name(p.second, tdc->get_state_name(p.first));
151  }
152  }
153  ret->set_history(sg_history);
154  }
155  return ret;
156  }
157  };
158  }
159 
160  template<unsigned I, typename Tdc>
161  auto letterize_tape(const Tdc& tdc, bool keep_history=true) -> typename internal::tdc_letterizer<Tdc,I,typename labelset_t_of<Tdc>::template valueset_t<I>>::ret_transducer_t {
162  return internal::tdc_letterizer<Tdc,I,typename labelset_t_of<Tdc>::template valueset_t<I>>::tdc_letterize(tdc, keep_history);
163  }
164  }
165 }//end of ns awali::stc
166 
167 #endif
carries the algebraic settings of automata
Definition: context.hh:40
context(const context &that)
Definition: context.hh:67
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
const valuesets_t & sets() const
The componants valuesets, as a tuple.
Definition: tupleset.hh:152
Implementation of labels are words.
Definition: wordset.hh:35
weightset_description weightset(const std::string &k)
auto tuple_tail(const Tuple &t) -> typename tail_tuple< Tuple >::type
Definition: sub_tuple.hh:96
Definition: letterize_tape.hh:36
Definition: letterize_tape.hh:57
Definition: projection.hh:52
auto letterize_tape(const Tdc &tdc, bool keep_history=true) -> typename internal::tdc_letterizer< Tdc, I, typename labelset_t_of< Tdc >::template valueset_t< I >>::ret_transducer_t
Definition: letterize_tape.hh:161
void copy_into(const AutIn &in, AutOut &out, Pred keep_state, bool keep_history=true, bool transpose=false)
Copy an automaton.
Definition: copy.hh:144
auto get_nullableset(const L &labelset) -> typename labelset_trait< L >::nullable_t
Definition: traits.hh:246
auto get_letterset(const L &labelset) -> typename labelset_trait< L >::letterset_t
Definition: traits.hh:225
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
Definition: mutable_automaton.hh:915
std::shared_ptr< internal::mutable_automaton_impl< Context > > mutable_automaton
Definition: mutable_automaton.hh:45
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
static tupleT gettuple(const std::tuple< Letterset, T... > &t)
Definition: letterize_tape.hh:42
typename tuple_to_tupleset< tupleT >::type type
Definition: letterize_tape.hh:40
static type get(const tupleset< Letterset, T... > &ts)
Definition: letterize_tape.hh:44
typename std::cons_tuple< typename labelset_trait< Letterset >::nullable_t, typename map_nullable< tupleset< T... > >::tupleT >::type tupleT
Definition: letterize_tape.hh:39
static type get(const tupleset<> &ts)
Definition: letterize_tape.hh:54
std::tuple<> tupleT
Definition: letterize_tape.hh:50
static tupleT gettuple(const std::tuple<> &t)
Definition: letterize_tape.hh:53
Definition: sub_tuple.hh:144
static auto get(const Tuple &t, const T &x) -> type< Tuple >
Definition: sub_tuple.hh:152
static type get(const tp_t &t, const U &v)
Definition: letterize_tape.hh:65
typename tuple_to_tupleset< typename replace_in_tuple< I, U >::template type< std::tuple< T... > >>::type type
Definition: letterize_tape.hh:62
labelset_t_of< Tdc > labelset_t
Definition: letterize_tape.hh:100
typename labelset_trait< letterset_t >::nullable_t nletterset_t
Definition: letterize_tape.hh:104
static ret_transducer_t tdc_letterize(const transducer_t &tdc, bool keep_history)
Definition: letterize_tape.hh:114
typename replace_in_tupleset< nullable_labelset_t, I, nletterset_t >::type ret_labelset_t
Definition: letterize_tape.hh:109
typename map_nullable< labelset_t >::type nullable_labelset_t
Definition: letterize_tape.hh:106
typename labelset_trait< Ilabelset_t >::letterset_t letterset_t
Definition: letterize_tape.hh:103
mutable_automaton< ret_context_t > ret_transducer_t
Definition: letterize_tape.hh:111
weightset_t_of< Tdc > weightset_t
Definition: letterize_tape.hh:101
typename nullable_labelset_t::value_t nullable_label_t
Definition: letterize_tape.hh:107
typename labelset_t::template valueset_t< I > Ilabelset_t
Definition: letterize_tape.hh:102
typename ret_labelset_t::value_t ret_label_t
Definition: letterize_tape.hh:112
Definition: letterize_tape.hh:71
typename nullable_labelset_t::value_t nullable_label_t
Definition: letterize_tape.hh:79
typename labelset_trait< Ilabelset_t >::letterset_t letterset_t
Definition: letterize_tape.hh:75
static ret_transducer_t tdc_letterize(const Tdc &tdc, bool keep_history)
Definition: letterize_tape.hh:86
typename map_nullable< labelset_t >::type nullable_labelset_t
Definition: letterize_tape.hh:78
weightset_t_of< Tdc > weightset_t
Definition: letterize_tape.hh:73
mutable_automaton< ret_context_t > ret_transducer_t
Definition: letterize_tape.hh:83
typename labelset_trait< letterset_t >::nullable_t nletterset_t
Definition: letterize_tape.hh:76
labelset_t_of< Tdc > labelset_t
Definition: letterize_tape.hh:72
typename ret_labelset_t::value_t ret_label_t
Definition: letterize_tape.hh:84
typename labelset_t::template valueset_t< I > Ilabelset_t
Definition: letterize_tape.hh:74
typename replace_in_tupleset< nullable_labelset_t, I, nletterset_t >::type ret_labelset_t
Definition: letterize_tape.hh:81
L nullable_t
Definition: traits.hh:35
L letterset_t
Definition: traits.hh:37
Definition: tuple.hh:359