Welcome to Vcsn 2.5

Vcsn is a platform for weighted automata and rational expressions. it is composed of C++ libraries with various interfaces, a Python binding, some specific features for IPython and a set of command line tools.

This short tutorial guides you through the Python binding of Vcsn, and more specifically the IPython interface. If you are not a Python programmer, rest assured that there is not much to know, and if you are a Python programmer, rest assured that its conventions have been respected, and you will be able to take the full benefit from both Vcsn and Python.

Once you read this page, you should explore these:

  • Read me first - This page.
  • Python
    • Contexts - The typing system for automata, expressions, etc.
    • Automata - How to define or edit automata.
    • Expressions - The definition of the rational expressions.
    • Algorithms - Index of the available operations on automata, expressions, etc.
  • The C++ Library - Tutorial on how to use the Vcsn C++ library.
  • Tools - Executables to run from the shell, easy to chain via pipes.
  • Trouble-Shooting - Some known problems (during build or use), and a few known solutions.
  • Examples

    • Sms2fr - Translate SMS (i.e., text messages) to proper French.
    • Spell-checker - From a dictionary, build a spell checker that fixes errors.
    • Stackoverflow - Some questions asked on Stackoverflow where Vcsn can compute the answer.
  • Research

    • References - Publications on the algorithms and constructs used in Vcsn.
    • ICTAC-2016 - Examples taken from a paper presented to ICTAC 2016.
    • CIAA-2016 - Examples taken from a paper presented to CIAA 2016.
  • Hacking - Random notes, badly written, obsolete, meant for Vcsn developers.

Additional material is available on the web:

  • The Vcsn Website contains more information, tarballs, etc.
  • The Vcsn Sandbox offers a playground to experiment with Vcsn from a web browser.
  • The Vcsn Gitlab is the right place to submit bug reports.

Videos

If you prefer to look at video rather than having to make the physical effort of sliding in a webpage, you might want to look at these short introductory videos (English and French):

In [1]:
%%HTML
<iframe width="300" height="169" src="https://www.youtube.com/embed/LzbXsEmqyC0" frameborder="0" allowfullscreen></iframe>
<iframe width="300" height="169" src="https://www.youtube.com/embed/LFYVBNbStZU" frameborder="0" allowfullscreen></iframe>

Quick Start

Vcsn offers several interfaces:

  • fast efficient C++ templated library dubbed static
  • a dynamic and flexible C++ interface dubbed dyn on top of static
  • a Python interface on top of dyn
  • an IPython interface built on top of the Python API (which is used to generate this very document)
  • and also a shell interface (Executables).

This documentation shows how to use the IPython interactive environment. Provided that Vcsn was properly deployed on your platform, to launch it run the following command from your shell:

$ vcsn notebook &

A web browser should open on a list of files. Click on the "New" menu, and select "Python" (or "Python 3"), which should open a new sheet. The remainder of this documentation is about such sheets.

First, import Vcsn into Python, and define the "context" in which you want to work. Do not worry about the (ugly!) syntax, just see where the alphabet (the set of letters, $\{a, b, c\}$) is defined. The last line (ctx) is here so that IPython displays what this variable contains.

In [2]:
import vcsn
ctx = vcsn.context("lal(abc), b")
ctx
Out[2]:
$\{a, b, c\}\to\mathbb{B}$

This object, the context, defines the types of the various entities. To build a rational expression on this alphabet, use ctx.expression as follows:

In [3]:
e1 = ctx.expression("ab*")
e1
Out[3]:
$a \, {b}^{*}$

The syntax for rational expressions is as follows (with increasing precedence):

  • \z denotes the empty language
  • \e denotes the language of the empty word
  • a denotes the language of the word a
  • e+f denotes the union of the languages of e and f (note the use of +, | is not accepted)
  • ef denotes the concatenation of the languages of e and f
  • e* denotes the Kleene closure of the language of e

For more details, please see the documentation of expressions.

So for instance e1 denotes the words starting with a single a followed by any number of bs.

Rational expressions are objects that feature methods. One such method is expression.shortest(number) that lists the number first (in shortlex order) words of the language defined by the rational expresion:

In [4]:
e1.shortest(10)
Out[4]:
$\mathit{a} \oplus \mathit{ab} \oplus \mathit{abb} \oplus \mathit{abbb} \oplus \mathit{abbbb} \oplus \mathit{abbbbb} \oplus \mathit{abbbbbb} \oplus \mathit{abbbbbbb} \oplus \mathit{abbbbbbbb} \oplus \mathit{abbbbbbbbb}$

You may compose rational expressions using Python operators such as + for sum, * for multiplication (concatenation):

In [5]:
e1 + e1 * e1
Out[5]:
$a \, {b}^{*} + a \, {b}^{*} \, a \, {b}^{*}$

Vcsn features different means to build an automaton from a rational expression. The expression.standard method builds the "standard autamaton", also known as the "position automaton", or the "Glushkov automaton":

In [6]:
e1.standard()
Out[6]:
%3 I0 0 0 I0->0 F1 F3 1 1 0->1 a 1->F1 3 3 1->3 b 3->F3 3->3 b

When it comes to displaying automata as graphs, there are several "traditions". In Vcsn, initial states are denoted by an entering arrow, and final (or "accepting") states by an exiting arrow. This automaton has one initial state, and two final states.

The expression.derived_term method builds the "derived-term automaton", aka, the Antimirov automaton.

In [7]:
a1 = e1.derived_term()
a1
Out[7]:
%3 I0 0 ab* I0->0 F1 1 b* 0->1 a 1->F1 1->1 b

Python operators that are accepted by rational expressions are also accepted by automata, with matching semantics.

In [8]:
a2 = (e1 + e1*e1).derived_term()
a2
Out[8]:
%3 I0 0 ab*+ab*ab* I0->0 F1 1 b* 0->1 a 2 b*ab* 0->2 a 1->F1 1->1 b 2->1 a 2->2 b
In [9]:
a3 = a1 + a1 * a1
a3
Out[9]:
%3 I0 0 0 I0->0 F1 F3 1 1 0->1 a 2 2 0->2 a 1->F1 1->1 b 2->2 b 3 3 2->3 a 3->F3 3->3 b

Well, those two automata are not equal (or more rigorously "isomorphic"), but they are equivalent:

In [10]:
a2.is_equivalent(a3)
Out[10]:
True

All the classical algorithms about automata are implemented:

In [11]:
a3
Out[11]:
%3 I0 0 0 I0->0 F1 F3 1 1 0->1 a 2 2 0->2 a 1->F1 1->1 b 2->2 b 3 3 2->3 a 3->F3 3->3 b
In [12]:
a3.determinize()
Out[12]:
%3 I0 0 0 I0->0 F1 F2 1 1, 2 0->1 a 1->F1 1->1 b 2 3 1->2 a 2->F2 2->2 b

The states of this automaton are decorated with metadata: the corresponding set of states of the input automaton. Use automaton.strip to remove this decoration.

In [13]:
a3.determinize().strip().complete()
Out[13]:
%3 I0 0 0 I0->0 F1 F2 1 1 0->1 a 3 3 0->3 b, c 1->F1 1->1 b 2 2 1->2 a 1->3 c 2->F2 2->2 b 2->3 a, c 3->3 [^]

Note that useless states and transitions are grayed.

To evaluate a word on an automaton, use evaluate(), or simpler yet: use the automaton as if it were a function:

In [14]:
a3.evaluate("a")
Out[14]:
$\top$
In [15]:
a3("b")
Out[15]:
$\bot$

To see the 10 first accepted words (if there are that many), use automaton.shortest:

In [16]:
a3.shortest(10)
Out[16]:
$\mathit{a} \oplus \mathit{aa} \oplus \mathit{ab} \oplus \mathit{aab} \oplus \mathit{aba} \oplus \mathit{abb} \oplus \mathit{aabb} \oplus \mathit{abab} \oplus \mathit{abba} \oplus \mathit{abbb}$

To extract a rational expression from the automaton, use expression():

In [17]:
a3.expression()
Out[17]:
$a \, {b}^{*} + a \, {b}^{*} \, a \, {b}^{*}$

This concludes this quick overview of Vcsn's IPython interface. You should now proceed to discover other features in other notebooks.