#!/usr/bin/perl # The Missing Textutils, Ondrej Bojar, obo@cuni.cz # http://www.cuni.cz/~obo/textutils # # 'indent_brackets.pl' processes stdin to stdout, indenting accoring to () # Useful e.g. for Collins/PennTreeBank-style trees or Moses 'python lattice # format'. # # $Id: indent_brackets.pl,v 1.1 2010-10-25 12:31:59 bojar Exp $ use strict; my $depth = 0; my $terminal = 1; sub action { my $char = shift; my $out; if ($char eq "(") { $out = "\n" if $depth > 0; $out .= " " x $depth; $out .= "("; $terminal = 1; $depth++; } else { # closing bracket $depth--; if (!$terminal) { $out = "\n"; $out .= " " x $depth; } $out .= ")"; $terminal = 0; } return $out; } while (<>) { chomp; # walk the chars $depth = 0; s/([\(\)])/action($1)/ge; print $_; print "\n"; }