Vcsn  2.2
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 {
15  template <typename Context>
16  mutable_automaton<Context>
17  levenshtein(const Context& ctx)
18  {
19  static_assert(Context::is_lat,
20  "levenshtein: labelset must be a tupleset");
21  static_assert(Context::labelset_t::size() == 2,
22  "levenshtein: labelset must have 2 tapes");
23  static_assert(Context::labelset_t::template valueset_t<0>::has_one(),
24  "levenshtein: first tape must have empty word");
25  static_assert(Context::labelset_t::template valueset_t<1>::has_one(),
26  "levenshtein: second tape must have empty word");
27  static_assert(std::is_same<typename Context::weightset_t, nmin>::value,
28  "levenshtein: weightset must be nmin");
29  using label_t = typename Context::labelset_t::value_t;
30  const auto& ls = *ctx.labelset();
31  const auto& ls1 = ls.template set<0>();
32  const auto& ls2 = ls.template set<1>();
33  const auto& ws = *ctx.weightset();
34  auto letters1 = detail::make_vector(ls1.generators());
35  auto letters2 = detail::make_vector(ls2.generators());
36 
37  auto res = make_mutable_automaton(ctx);
38 
39  auto s = res->new_state();
40  res->set_initial(s);
41  res->set_final(s);
42  // Suppressions.
43  for (auto l : letters1)
44  res->new_transition(s, s, label_t{l, ls2.one()}, 1);
45  // Insertions.
46  for (auto l : letters2)
47  res->new_transition(s, s, label_t{ls1.one(), l}, 1);
48  // Substitutions.
49  for (auto l : letters1)
50  for (auto l2 : letters2)
51  res->new_transition(s, s, label_t{l, l2}, !ls1.equal(l, l2));
52  return res;
53  }
54 
55  /*-------------------.
56  | dyn::levenshtein. |
57  `-------------------*/
58 
59  namespace dyn
60  {
61  namespace detail
62  {
64  template <typename Context>
65  automaton
67  {
68  const auto& c = ctx->as<Context>();
69  return make_automaton(::vcsn::levenshtein(c));
70  }
71  }
72  }
73 }
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
Definition: automaton.hh:75
Definition: a-star.hh:8
mutable_automaton< Context > levenshtein(const Context &ctx)
The Levenshtein automaton for a given context.
Definition: levenshtein.hh:17
std::shared_ptr< const detail::context_base > context
A dyn::context.
Definition: fwd.hh:43
std::vector< typename Cont::value_type > make_vector(const Cont &cont)
The content of cont as a vector.
Definition: vector.hh:20
automaton levenshtein(const dyn::context &ctx)
Bridge.
Definition: levenshtein.hh:66
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:69
static dyn::context ctx(const driver &d)
Get the context of the driver.
Definition: parse.cc:82
mutable_automaton< Context > make_mutable_automaton(const Context &ctx)
size_t size(const ExpSet &rs, const typename ExpSet::value_t &r)