#! /bin/perl

# I assume Perl Ver. 5

## Written by Greg Sanders, December 2001
## This program will convert the canonicalized textual version of a logfile
## into an XML equivalent.

if ($#ARGV != 1)    # $#ARGV == 1 if there are two command line arguments
{  print "\n  Usage: convert_to_xml input_file.txt output_file.xml\n";
   print "         The input_file must already exist.  The output file is created.\n\n";
   print "         The output file will eventually include multiple XML documents,\n";
   print "         ... (each representing one call) separated by lines of hyphens.\n\n";
   exit;
}

@siteNames = ("", "AT&amp;T", "BBN", "CMU", "Colorado", "IBM", "MIT", "MITRE", "Lucent", "SRI");

open INFILE, "$ARGV[0]" or die "Error: can't open input file $ARGV[0]\n\n";
open XMLFILE, "> $ARGV[1]";
$inputLineNum = 0;

while (<INFILE>)
{
   $inputLineNum += 1;

   if (/^--------------------------------------/)
   {   print XMLFILE "$_\n";
       print XMLFILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n";
   }
   elsif (/^(\d\d\d\d\d)_[X0-9][X0-9]_[X0-9][X0-9]_(\d\d)_(\d\d\d\d)(\d\d)(\d\d)_(\d\d\d\d\d\d)/)
   {   printf XMLFILE "<call pin=\"$1\" system=\"%s\" year=\"$3\" month=\"$4\" day=\"$5\" time=\"$6\" timezone=\"EDT\">\n",
                                       $siteNames[$2];
   }
   elsif (/^Task Start Time ([0-9.]+)/)
   {  $startTime = $1;
      printf XMLFILE "<task>\n";
      printf XMLFILE "  <start time=\"%.3f\" element_type=\"task\" />\n", $1 - $startTime;

      ## So, we're indicating the task starts at time 0.000
      ## All other times will be measured from this zero point.
   }
   elsif (/^Task End Time ([0-9.]+)/)
   {
      printf XMLFILE "  <end time=\"%.3f\" element_type=\"task\" />\n", $1 - $startTime;
      printf XMLFILE "</task>\n";
   }
   elsif (/^Start user turn (\d+) at ([0-9.]+)/)
   {
      printf XMLFILE "    <start time=\"%.3f\" speaker=\"user\" element_type=\"turn\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^Start sys turn (\d+) at ([0-9.]+)/)
   {
      printf XMLFILE "    <start time=\"%.3f\" speaker=\"system\" element_type=\"turn\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^Start user utt (\d+) at ([0-9.]+)/)
   {
      printf XMLFILE "      <start time=\"%.3f\" speaker=\"user\" element_type=\"utterance\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^Start sys utt (\d+) at ([0-9.]+)/)
   {
      printf XMLFILE "      <start time=\"%.3f\" speaker=\"system\" element_type=\"utterance\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^End user utt (\d+) at ([0-9.]+)/)
   {  printf XMLFILE "      <end time=\"%.3f\" speaker=\"user\" element_type=\"utterance\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^End sys utt (\d+) at ([0-9.]+)/)
   {  printf XMLFILE "      <end time=\"%.3f\" speaker=\"system\" element_type=\"utterance\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^End user turn (\d+) at ([0-9.]+)/)
   {  printf XMLFILE "    <end time=\"%.3f\" speaker=\"user\" element_type=\"turn\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif (/^End sys turn (\d+) at ([0-9.]+)/)
   {  printf XMLFILE "    <end time=\"%.3f\" speaker=\"system\" element_type=\"turn\" number=\"$1\" />\n",
               $2 - $startTime;
   }
   elsif ( /  User: (\S+)\s+(\S+)\s+Asr: ([^\/]+)\/  Transcr: (.*)$/)
   {  printf XMLFILE "        <text type=\"transcription\" start_time=\"%.3f\" end_time=\"%.3f\">\n",
                           $1 - $startTime,  $2 - $startTime;
      printf XMLFILE "          <![CDATA[$4]]>\n";
      printf XMLFILE "        </text>\n";
      printf XMLFILE "        <text type=\"asr\" start_time=\"%.3f\" end_time=\"%.3f\">\n",
                           $1 - $startTime,  $2 - $startTime;
      printf XMLFILE "          <![CDATA[$3]]>\n";
      printf XMLFILE "        </text>\n";
   }
   elsif ( /User: (\S+)\s+(\S+)\s+Asr: ([^\/]+)\/  Transcr: (.*)$/)
   {  printf XMLFILE "  <text type=\"transcription\" start_time=\"%.3f\" end_time=\"%.3f\">\n",
                           $1 - $startTime,  $2 - $startTime;
      printf XMLFILE "    <![CDATA[$4]]>\n";
      printf XMLFILE "  </text>\n";
      printf XMLFILE "  <text type=\"asr\" start_time=\"%.3f\" end_time=\"%.3f\">\n",
                           $1 - $startTime,  $2 - $startTime;
      printf XMLFILE "    <![CDATA[$3]]>\n";
      printf XMLFILE "  </text>\n";
   }
   elsif (/  Sys:\s+(\S+)\s+(\S+)\s+(.*)$/)
   {  if ($1 eq "?" and $2 eq "?")
      {  printf XMLFILE "    <text type=\"system\" believed_spoken=\"false\">\n";
         printf XMLFILE "      <![CDATA[$3]]>\n";
         printf XMLFILE "    </text>\n";
      }    
      else
      {  printf XMLFILE "        <text type=\"system\" believed_spoken=\"true\" start_time=\"%.3f\" end_time=\"%.3f\">\n",
                           $1 - $startTime,  $2 - $startTime;
         printf XMLFILE "          <![CDATA[$3]]>\n";
         printf XMLFILE "        </text>\n";
      }
   }
   elsif (/System:\s+(\S+)\s+(\S+)\s+(.*)$/)
   {  if ($1 eq "?" and $2 eq "?")
      {  printf XMLFILE "  <text type=\"system\" believed_spoken=\"false\">\n";
      }
      else    
      {  printf XMLFILE "  <text type=\"system\" believed_spoken=\"true\" start_time=\"%.3f\" end_time=\"%.3f\">\n",
                           $1 - $startTime,  $2 - $startTime;
      }
      printf XMLFILE "    <![CDATA[$3]]>\n";
      printf XMLFILE "  </text>\n";
   }
   elsif (/Overlap from ([0-9.]+) to ([0-9.]+)/)
   {  printf XMLFILE "<overlap from=\"%.3f\" to=\"%.3f\" />\n",
                           $1 - $startTime,  $2 - $startTime;
   }
   elsif (/^\s*$/)
   {  printf XMLFILE "\n";
   }
   else
   {  chomp;
      printf XMLFILE "<!--$_-->\n";
   }
}  

