#!/usr/bin/perl # Copyright (C) 2006 Sebastian Nagel # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. use strict; use warnings; my (%options); use Getopt::Long qw(:config no_ignore_case auto_help); GetOptions ( 'help|h|?' => \$options{help}, 'n' => \$options{number_nodes}, ); if ($options{help}) { print <<"HELP"; $0 -- convert a two-level-transducer produced by fst-print into dot format Options: ?|h|help show this help n number the states See: Stuttgart Finite State Transducer Tools (SFST) : http://www.ims.uni-stuttgart.de/projekte/gramotron/SOFTWARE/SFST.html Dot / Graphviz : http://www.graphviz.org/ HELP exit; } my $name = "sfst"; if (@ARGV) { my $n = $ARGV[0]; $n =~ s/(<=.)\.a$//; $n =~ s@.+?/@@; if ($n ne '') { $name = $n; } } my $node_shape = "circle"; print <<"HEADER"; # generated by $0\n digraph $name { rankdir="LR"; ratio="fill"; node [ shape="$node_shape", fontsize="11", label="", width=".2" ]; edge [ arrowhead="normal", arrowsize=".5", fontsize=14, labeldistance=".1" ]; HEADER # labelfloat="true" % nein! my (%states); # initial state # print "0 [ shape=\"polygon\", sides=\"3\", orientation=\"270\", regular=\"1\" ];\n"; print " -1 [ shape=\"point\", width=\".01\" ];\n"; print " -1 -> 0;\n"; while (<>) { chomp; if (/^(\d+)\t(.+?)\t(\d+)$/) { print " $1 -> $3 [ label=\"$2\" ];\n"; for ($1, $3) { unless (exists $states{$_}) { $states{$_} = $_; } } } elsif (/^final: (\d+)$/) { print " $1 [ shape=\"doublecircle\" ];\n"; } else { print STDERR "error on line $.:\n(\"$_\")\n"; exit 1; } } if ($options{number_nodes}) { foreach my $k (keys %states) { print " $k [ label=\"". $states{$k} . "\" ];\n"; } } print "}\n";