Vcsn is a platform for weighted automata and rational expressions. It is composed of C++ libraries with various interfaces, Python bindings, some specific features for IPython and a set of command line tools.
This 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:
vcsn thompson -Ee '[ab]*c' | vcsn proper | vcsn determinize
).Examples
Research
Additional material is available on the web:
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):
%%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>
Vcsn offers several interfaces:
static
dyn
on top of static
dyn
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.
import vcsn
ctx = vcsn.context("lal(abc), b")
ctx
This object, the context, defines the types of the various entities. To build a rational expression on this alphabet, use ctx.expression
as follows:
e1 = ctx.expression("ab*")
e1
The syntax for rational expressions is as follows (with increasing precedence):
\z
denotes the empty language\e
denotes the language of the empty worda
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 b
s.
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:
e1.shortest(10)
You may compose rational expressions using Python operators such as +
for sum, *
for multiplication (concatenation):
e1 + e1 * e1
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":
e1.standard()
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.
a1 = e1.derived_term()
a1
Python operators that are accepted by rational expressions are also accepted by automata, with matching semantics.
a2 = (e1 + e1*e1).derived_term()
a2
a3 = a1 + a1 * a1
a3
Well, those two automata are not equal (or more rigorously "isomorphic"), but they are equivalent:
a2.is_equivalent(a3)
All the classical algorithms about automata are implemented:
a3
a3.determinize()
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.
a3.determinize().strip().complete()
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:
a3.evaluate("a")
a3("b")
To see the 10 first accepted words (if there are that many), use automaton.shortest:
a3.shortest(10)
To extract a rational expression from the automaton, use expression()
:
a3.expression()
This concludes this quick overview of Vcsn's IPython interface. You should now proceed to discover other features in other notebooks.