#!/usr/local/bin/perl
# script to script to convert the hand segmentation to stm forma
# Ziad Al Bawab, ziada@cs.cmu.edu, April 25 2009

use Getopt::Long;
use POSIX;

if(!GetOptions(
               '-inputtrans=s'  => \$intransfile,
               '-outputtrans=s'  => \$outtrans,
 		))
{
        printf("         $0 <options>: script to convert the hand segmentation to stm format\n");      
        printf("         -inputtrans  		<string>: input transcript file in trs format\n");
        printf("         -outputtrans	  	<string>: output transcript file in stm format\n"); 

}


# open the files
open(transcriptf,$intransfile);
open(outtrans,">$outtrans");

#################################################################
# Start reading the "version 2" CMU Meetings transcription format
#################################################################

# First get the header information
while(defined($headerline = <transcriptf>))
{
	if ($headerline =~ /audio_filename="(.*?)"/)
	{
		$filename = $1;
	}  
	elsif($headerline =~ /Turn startTime="(.*)" endTime="(.*)"/)
	{
		$starttimefile = $1;
		$endtimefile = $2;
#		print("starttimefile: $starttimefile, endtimefile: $endtimefile\n");
		last;	
	}	
}


while(defined($line = <transcriptf>))
{
	chomp($line);	

	if($line =~ /Sync time="(.*?)"/ )
	{
		##################################################
		### Get the utterance timing information and   ###	 
		### create utterance ID and control file entry ###
		##################################################

		# set the starting time of this utterance
		$starttime = $1;

		# read the corresponding transcript
		$line = <transcriptf>;	 
		chomp($line);

	}
	elsif ($line =~ /[a-z]/)
        {
                $starttime = $endtime;
        }

	if ($line =~ /sil/)
	{
		$transline = "<sil>";
	}
	elsif ($line =~ /Introduction/)
	{
		$transline = "<introduction>";
	}
	else
	{
		$transline = $line;
	}
		
	# read the ending time of this utterance
	$line = <transcriptf>;
	chomp($line);	

	if( $line =~ /Sync time="(.*?)"/ )
	{
		$endtime = $1; 	
		print outtrans "$filename 1 $filename\-1 $starttime $endtime \<O\,fx\,M\>  $transline\n";	
	}
	elsif( $line =~ /<\/Turn>/ )
	{
	 	$endtime = $endtimefile;	
		print outtrans "$filename 1 $filename\-1 $starttime $endtime \<O\,fx\,M\>  $transline\n";			    last;	
	}	 
		
}

	
close(transcriptf);
close(outtrans);





