#!/usr/bin/perl -w
# detokenize: undo effects of tokenization (except for added newlines)
# usage: detokenize < file
# 20050210 erikt@science.uva.nl

while (<STDIN>) {
   $line = $_;
   chomp($line);
   @words = split(/\s+/,$line);
   for ($i=0;$i<=$#words;$i++) {
      if ($words[$i] =~ /^[({\[]$/) {
         $j = $i+1;
         while ($j <= $#words and $words[$j] eq "") { $j++; }
         if ($j <= $#words) { 
            $words[$j] = "$words[$i]$words[$j]";
            $words[$i] = "";
         }
      } elsif ($words[$i] =~ /^[\.,!?:;\$\)\]}]$/) {
         $j = $i-1;
         while ($j >= 0 and $words[$j] eq "") { $j--; }
         if ($j >= 0) { 
            $words[$j] .= $words[$i];
            $words[$i] = "";
         }
      }
   }
   for ($i=0;$i<=$#words;$i++) {
      if ($words[$i] ne "") { print "$words[$i] "; }
   }
   print "\n";
}
exit(0);


