Skip to content

Instantly share code, notes, and snippets.

@vchahun
Created September 26, 2012 03:38
Show Gist options
  • Save vchahun/3785868 to your computer and use it in GitHub Desktop.
Save vchahun/3785868 to your computer and use it in GitHub Desktop.
Draw with pyfst
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Draw"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.core.display import SVG, display\n",
"import subprocess as sp\n",
"\n",
"def draw_dot(dot):\n",
" process = sp.Popen(['dot', '-Tsvg'], stdin=sp.PIPE, stdout=sp.PIPE)\n",
" out, _ = process.communicate(dot)\n",
" display(SVG(out))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import fst"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic example"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f = fst.Fst()\n",
"f.start = f.add_state()\n",
"\n",
"f.add_arc(0, 1, 1, 1, 0.5)\n",
"f.add_arc(0, 1, 2, 2, 1.5)\n",
"\n",
"f.add_state()\n",
"f.add_arc(1, 2, 3, 3, 2.5)\n",
"\n",
"f.add_state()\n",
"f.set_final(2, 3.5)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"draw_dot(f.draw())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"svg": [
"<svg height=\"76pt\" viewBox=\"0.00 0.00 290.00 76.00\" width=\"290pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g class=\"graph\" id=\"graph1\" transform=\"scale(1 1) rotate(0) translate(4 72)\">\n",
"<title>FST</title>\n",
"<polygon fill=\"white\" points=\"-4,5 -4,-72 287,-72 287,5 -4,5\" stroke=\"white\"/>\n",
"<!-- 0 -->\n",
"<g class=\"node\" id=\"node1\"><title>0</title>\n",
"<ellipse cx=\"18\" cy=\"-34\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\" stroke-width=\"2\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"18\" y=\"-28.4\">0</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g class=\"node\" id=\"node3\"><title>1</title>\n",
"<ellipse cx=\"124\" cy=\"-34\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"124\" y=\"-28.4\">1</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g class=\"edge\" id=\"edge2\"><title>0-&gt;1</title>\n",
"<path d=\"M36.1735,-34C52.3569,-34 76.8693,-34 95.7267,-34\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"95.801,-37.5001 105.801,-34 95.801,-30.5001 95.801,-37.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"71\" y=\"-36.8\">1:1/0.5</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g class=\"edge\" id=\"edge4\"><title>0-&gt;1</title>\n",
"<path d=\"M32.4597,-22.7807C37.8731,-18.9586 44.3931,-15.1585 51,-13.2 68.0446,-8.14729 73.9554,-8.14729 91,-13.2 94.4067,-14.2099 97.7903,-15.7093 101.011,-17.449\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"99.2055,-20.4479 109.54,-22.7807 102.916,-14.5122 99.2055,-20.4479\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"71\" y=\"-15.8\">2:2/1.5</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g class=\"node\" id=\"node6\"><title>2</title>\n",
"<ellipse cx=\"247\" cy=\"-34\" fill=\"none\" rx=\"30.7249\" ry=\"30.7249\" stroke=\"black\"/>\n",
"<ellipse cx=\"247\" cy=\"-34\" fill=\"none\" rx=\"34.7388\" ry=\"34.7388\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"247\" y=\"-28.4\">2/3.5</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g class=\"edge\" id=\"edge6\"><title>1-&gt;2</title>\n",
"<path d=\"M142.08,-34C157.572,-34 181.094,-34 201.843,-34\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"202.07,-37.5001 212.07,-34 202.07,-30.5001 202.07,-37.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"177\" y=\"-36.8\">3:3/2.5</text>\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text": [
"<IPython.core.display.SVG at 0x102b344d0>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def make_input(word, syms):\n",
" \"\"\"\n",
" Make a charcter input transducer:\n",
" [0] =w:w=> 1 =o:o=> 2 =r:r=> 3 =d:d=> (4) \n",
" \"\"\"\n",
" inp = fst.Fst()\n",
" inp.start = inp.add_state()\n",
" source = inp.start\n",
" for c in word:\n",
" dest = inp.add_state()\n",
" inp.add_arc(source, dest, syms[c], syms[c])\n",
" source = dest\n",
" inp.set_final(source)\n",
" return inp"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"syms = fst.SymbolTable()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"hello = make_input('hello', syms)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"draw_dot(hello.draw(isyms=syms, osyms=syms))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"svg": [
"<svg height=\"52pt\" viewBox=\"0.00 0.00 458.00 52.00\" width=\"458pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
"<g class=\"graph\" id=\"graph1\" transform=\"scale(1 1) rotate(0) translate(4 48)\">\n",
"<title>FST</title>\n",
"<polygon fill=\"white\" points=\"-4,5 -4,-48 455,-48 455,5 -4,5\" stroke=\"white\"/>\n",
"<!-- 0 -->\n",
"<g class=\"node\" id=\"node1\"><title>0</title>\n",
"<ellipse cx=\"18\" cy=\"-22\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\" stroke-width=\"2\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"18\" y=\"-16.4\">0</text>\n",
"</g>\n",
"<!-- 1 -->\n",
"<g class=\"node\" id=\"node3\"><title>1</title>\n",
"<ellipse cx=\"102\" cy=\"-22\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"102\" y=\"-16.4\">1</text>\n",
"</g>\n",
"<!-- 0&#45;&gt;1 -->\n",
"<g class=\"edge\" id=\"edge2\"><title>0-&gt;1</title>\n",
"<path d=\"M36.3901,-22C47.2257,-22 61.3988,-22 73.7087,-22\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"73.9162,-25.5001 83.9162,-22 73.9161,-18.5001 73.9162,-25.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"60\" y=\"-24.8\">h:h</text>\n",
"</g>\n",
"<!-- 2 -->\n",
"<g class=\"node\" id=\"node5\"><title>2</title>\n",
"<ellipse cx=\"184\" cy=\"-22\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"184\" y=\"-16.4\">2</text>\n",
"</g>\n",
"<!-- 1&#45;&gt;2 -->\n",
"<g class=\"edge\" id=\"edge4\"><title>1-&gt;2</title>\n",
"<path d=\"M120.359,-22C130.703,-22 144.059,-22 155.779,-22\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"155.95,-25.5001 165.95,-22 155.95,-18.5001 155.95,-25.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"143\" y=\"-24.8\">e:e</text>\n",
"</g>\n",
"<!-- 3 -->\n",
"<g class=\"node\" id=\"node7\"><title>3</title>\n",
"<ellipse cx=\"262\" cy=\"-22\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"262\" y=\"-16.4\">3</text>\n",
"</g>\n",
"<!-- 2&#45;&gt;3 -->\n",
"<g class=\"edge\" id=\"edge6\"><title>2-&gt;3</title>\n",
"<path d=\"M202.246,-22C211.477,-22 223.065,-22 233.505,-22\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"233.823,-25.5001 243.823,-22 233.823,-18.5001 233.823,-25.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"223\" y=\"-24.8\">l:l</text>\n",
"</g>\n",
"<!-- 4 -->\n",
"<g class=\"node\" id=\"node9\"><title>4</title>\n",
"<ellipse cx=\"340\" cy=\"-22\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"340\" y=\"-16.4\">4</text>\n",
"</g>\n",
"<!-- 3&#45;&gt;4 -->\n",
"<g class=\"edge\" id=\"edge8\"><title>3-&gt;4</title>\n",
"<path d=\"M280.246,-22C289.477,-22 301.065,-22 311.505,-22\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"311.823,-25.5001 321.823,-22 311.823,-18.5001 311.823,-25.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"301\" y=\"-24.8\">l:l</text>\n",
"</g>\n",
"<!-- 5 -->\n",
"<g class=\"node\" id=\"node11\"><title>5</title>\n",
"<ellipse cx=\"428\" cy=\"-22\" fill=\"none\" rx=\"18\" ry=\"18\" stroke=\"black\"/>\n",
"<ellipse cx=\"428\" cy=\"-22\" fill=\"none\" rx=\"22\" ry=\"22\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"428\" y=\"-16.4\">5</text>\n",
"</g>\n",
"<!-- 4&#45;&gt;5 -->\n",
"<g class=\"edge\" id=\"edge10\"><title>4-&gt;5</title>\n",
"<path d=\"M358.403,-22C369.14,-22 383.224,-22 395.835,-22\" fill=\"none\" stroke=\"black\"/>\n",
"<polygon fill=\"black\" points=\"395.976,-25.5001 405.976,-22 395.976,-18.5001 395.976,-25.5001\" stroke=\"black\"/>\n",
"<text font-family=\"Times,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"382\" y=\"-24.8\">o:o</text>\n",
"</g>\n",
"</g>\n",
"</svg>"
],
"text": [
"<IPython.core.display.SVG at 0x103444a90>"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment