{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome to Vcsn 2.8\n",
"\n",
"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.\n",
"\n",
"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.\n",
"\n",
"Once you read this page, you should explore these:\n",
"* [Read me first](!Read-me-first.ipynb) - This page.\n",
"* Python\n",
" * [Contexts](Contexts.ipynb) - The typing system for automata, expressions, etc.\n",
" * [Automata](Automata.ipynb) - How to define or edit automata.\n",
" * [Expressions](Expressions.ipynb) - The definition of the rational expressions.\n",
" * [Algorithms](Algorithms.ipynb) - Index of the available operations on automata, expressions, etc.\n",
"* [The C++ Library](C++-Library.ipynb) - Tutorial on how to use the Vcsn C++ library.\n",
"* [Tools](Tools.ipynb) - Executables to run from the shell, easy to chain via pipes (e.g., `vcsn thompson -Ee '[ab]*c' | vcsn proper | vcsn determinize`).\n",
"* [Troubleshooting](Troubleshooting.ipynb) - Some known problems (during build or use), and a few known solutions.\n",
"* Examples\n",
" * [Sms2fr](Sms2fr.ipynb) - Translate SMS (i.e., text messages) to proper French.\n",
" * [Spell-checker](Spell-checker.ipynb) - From a dictionary, build a spell checker that fixes errors.\n",
" * [Stackoverflow](Stackoverflow.ipynb) - Some questions asked on Stackoverflow where Vcsn can compute the answer.\n",
"\n",
"* Research\n",
" * [References](References.ipynb) - Publications on the algorithms and constructs used in Vcsn.\n",
" * [ICTAC-2016](ICTAC-2016.ipynb) - Examples taken from a paper presented to ICTAC 2016.\n",
" * [ICTAC-2017](ICTAC-2017.ipynb) - Examples taken from a paper presented to ICTAC 2017.\n",
" * [CIAA-2016](CIAA-2016.ipynb) - Examples taken from a paper presented to CIAA 2016.\n",
"* [Hacking](Hacking.ipynb) - Random notes, badly written, obsolete, meant for Vcsn developers.\n",
"\n",
"Additional material is available on the web:\n",
"* The [Vcsn Website](http://vcsn.lrde.epita.fr) contains more information, tarballs, etc.\n",
"* The [Vcsn Sandbox](http://vcsn-sandbox.lrde.epita.fr) offers a playground to experiment with Vcsn from a web browser.\n",
"* The [Vcsn Gitlab](http://gitlab.lrde.epita.fr/vcsn/vcsn/issues) is the right place to submit bug reports."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Videos\n",
"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):"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%HTML\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quick Start\n",
"\n",
"Vcsn offers several interfaces:\n",
"- fast efficient C++ templated library dubbed `static`\n",
"- a dynamic and flexible C++ interface dubbed `dyn` on top of `static`\n",
"- a Python interface on top of `dyn`\n",
"- an IPython interface built on top of the Python API (which is used to generate this very document)\n",
"- and also a shell interface ([Tools](Tools.ipynb)).\n",
"\n",
"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:\n",
"\n",
" $ vcsn notebook &\n",
"\n",
"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.\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\{a, b, c\\}\\to\\mathbb{B}$"
],
"text/plain": [
"{abc} -> B"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import vcsn\n",
"ctx = vcsn.context(\"lal(abc), b\")\n",
"ctx"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This object, the context, defines the types of the various entities. To build a rational expression on this alphabet, use `ctx.expression` as follows:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$a \\, {b}^{*}$"
],
"text/plain": [
"ab*"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1 = ctx.expression(\"ab*\")\n",
"e1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The syntax for rational expressions is as follows (with increasing precedence):\n",
"- `\\z` denotes the empty language\n",
"- `\\e` denotes the language of the empty word\n",
"- `a` denotes the language of the word `a`\n",
"- `e+f` denotes the union of the languages of `e` and `f` (note the use of `+`, `|` is not accepted)\n",
"- `ef` denotes the concatenation of the languages of `e` and `f`\n",
"- `e*` denotes the Kleene closure of the language of `e`\n",
"\n",
"For more details, please see [the documentation of expressions](Expressions.ipynb).\n",
"\n",
"So for instance `e1` denotes the words starting with a single `a` followed by any number of `b`s.\n",
"\n",
"Rational expressions are objects that feature methods. One such method is [expression.shortest(_number_)](expression.shortest.ipynb) that lists the _`number`_ first (in shortlex order) words of the language defined by the rational expresion:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\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}$"
],
"text/plain": [
"a + ab + abb + abbb + abbbb + abbbbb + abbbbbb + abbbbbbb + abbbbbbbb + abbbbbbbbb"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1.shortest(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may compose rational expressions using Python operators such as `+` for sum, `*` for multiplication (concatenation):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$a \\, {b}^{*} + a \\, {b}^{*} \\, a \\, {b}^{*}$"
],
"text/plain": [
"ab*+ab*ab*"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1 + e1 * e1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Vcsn features different means to build an automaton from a rational expression. The [expression.standard](expression.standard.ipynb) method builds the \"standard autamaton\", also known as the \"position automaton\", or the \"Glushkov automaton\":"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"mutable_automaton, b>>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"e1.standard()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n",
"\n",
"The [expression.derived_term](expression.derived_term.ipynb) method builds the \"derived-term automaton\", aka, the Antimirov automaton."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"expression_automaton, b>>>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1 = e1.derived_term()\n",
"a1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python operators that are accepted by rational expressions are also accepted by automata, with matching semantics."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"expression_automaton, b>>>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2 = (e1 + e1*e1).derived_term()\n",
"a2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"mutable_automaton, b>>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3 = a1 + a1 * a1\n",
"a3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well, those two automata are not equal (or more rigorously \"isomorphic\"), but they are equivalent:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2.is_equivalent(a3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"All the classical algorithms about automata are implemented:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"mutable_automaton, b>>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"determinized_automaton, b>>, vcsn::wet_kind_t::bitset, false>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3.determinize()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The states of this automaton are decorated with metadata: the corresponding set of states of the input automaton. Use [automaton.strip](automaton.strip.ipynb) to remove this decoration."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"mutable_automaton, b>>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3.determinize().strip().complete()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that useless states and transitions are grayed."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To evaluate a word on an automaton, use `evaluate()`, or simpler yet: use the automaton as if it were a function:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\top$"
],
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3.evaluate(\"a\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\bot$"
],
"text/plain": [
"0"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3(\"b\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To see the 10 first accepted words (if there are that many), use [automaton.shortest](automaton.shortest.ipynb):"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\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}$"
],
"text/plain": [
"a + aa + ab + aab + aba + abb + aabb + abab + abba + abbb"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3.shortest(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To extract a rational expression from the automaton, use `expression()`:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$a \\, {b}^{*} + a \\, {b}^{*} \\, a \\, {b}^{*}$"
],
"text/plain": [
"ab*+ab*ab*"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a3.expression()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This concludes this quick overview of Vcsn's IPython interface. You should now proceed to discover other features in other notebooks."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}