Vcsn  2.1
Be Rational
levenshtein.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include <vcsn/alphabets/char.hh>
8 #include <vcsn/dyn/automaton.hh>
9 #include <vcsn/dyn/context.hh>
10 #include <vcsn/weightset/fwd.hh>
11 
12 namespace vcsn
13 {
14  template <typename Context>
15  mutable_automaton<Context>
16  levenshtein(const Context& ctx)
17  {
18  static_assert(Context::is_lat,
19  "levenshtein: labelset must be a tupleset");
20  static_assert(Context::labelset_t::size() == 2,
21  "levenshtein: labelset must have 2 tapes");
22  static_assert(std::tuple_element<0, typename Context::labelset_t::valuesets_t>::type::has_one(),
23  "levenshtein: first tape must have empty word");
24  static_assert(std::tuple_element<1, typename Context::labelset_t::valuesets_t>::type::has_one(),
25  "levenshtein: second tape must have empty word");
26  static_assert(std::is_same<typename Context::weightset_t, nmin>::value,
27  "levenshtein: weightset must be nmin");
28  using label_t = typename Context::labelset_t::value_t;
29  const auto& ls = *ctx.labelset();
30  const auto& ls1 = ls.template set<0>();
31  const auto& ls2 = ls.template set<1>();
32  const auto& ws = *ctx.weightset();
33  auto letters = detail::make_vector(ls1.genset());
34  auto letters2 = detail::make_vector(ls2.genset());
35 
36  using automaton_t = mutable_automaton<Context>;
37  automaton_t res = make_mutable_automaton(ctx);
38 
39  auto s = res->new_state();
40  res->set_initial(s);
41  res->set_final(s);
42  for (auto l : letters)
43  res->new_transition(s, s, label_t{l, ls2.one()}, 1);
44  for (auto l : letters2)
45  res->new_transition(s, s, label_t{ls1.one(), l}, 1);
46  for (auto l : letters)
47  for (auto l2 : letters2)
48  res->new_transition(s, s, label_t{l, l2}, !ls1.equal(l, l2));
49  return res;
50  }
51 
52  /*-------------------.
53  | dyn::levenshtein. |
54  `-------------------*/
55 
56  namespace dyn
57  {
58  namespace detail
59  {
61  template <typename Context>
62  automaton
64  {
65  const auto& c = ctx->as<Context>();
66  return make_automaton(::vcsn::levenshtein(c));
67  }
68  }
69  }
70 }
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:20
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:41
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:80
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
automaton levenshtein(const dyn::context &ctx)
Bridge.
Definition: levenshtein.hh:63
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)
mutable_automaton< Context > levenshtein(const Context &ctx)
Definition: levenshtein.hh:16
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
Definition: fwd.hh:24