#! /usr/bin/perl
#
# Read the id of a speaker and tranfer all relevant files
# from the recording text directory to the transcriptions directory.
#
#    Copyright (C) 2000, 2001  R.J.J.H. van Son
# 
#    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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

$RecordingTextsDir = "../RecordingTexts/";
$Transcriptions    = "../Transcriptions/";

# Make sure the source directories exist
sub check_Recordingdirectories  # ($ID) -> 1/0 recording texts directories exists or not
{
	my $ID = shift || return 0;
	#
	my $DirsExists = -d "$RecordingTextsDir${ID}1_Texts" && "$RecordingTextsDir${ID}2_Texts";
	return $DirsExists;
}

# Make sure the target directory exists
sub create_Transcriptiondirectory  # ($ID) -> 1/0 create directory if necessary
{
	my $ID = shift || return 0;
	#
	my $DirExists = -d "$Transcriptions$ID";
	(return mkdir("$Transcriptions$ID", 0744) || die "$Transcriptions$ID: $!\n") unless $DirExists;
	return 0;   # Nothing done
}

# Check  and prepare source and target directories
sub prepare_directories    # ($ID) -> 0/1 Check and create directories, 0: Error
{
	my $ID = shift || return 0;
	#
	return 0 unless check_Recordingdirectories($ID);
	create_Transcriptiondirectory($ID);
	return 1;
}

# Convert Fixed Pronunciation texts
sub convert_fixed_pronunciation	# ($ID, $RecordingNo) -> 0/1
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = shift || return 0;
	my $SourceType = $RecordingNo == 1 ?  "FixedTexts" : "VariableText";
	my $TargetType = $RecordingNo == 1 ? "FPA" : "FPB";
	#
	my $Source = "$RecordingTextsDir${ID}${RecordingNo}_Texts/${ID}$RecordingNo$SourceType.txt";
	my $Target1 = "$Transcriptions$ID/${ID}$RecordingNo${TargetType}1.txt";
	my $Target2 = "$Transcriptions$ID/${ID}$RecordingNo${TargetType}2.txt";

	my $Count = 0;
	#
	open(SOURCE, "<$Source") || die "$Source: /u/cvs/SLcorpus/scripts/TextToTranscriptions.pl,v $!\n";
	open(TARGET1, ">$Target1") || die "$Target1: $!\n";
	open(TARGET2, ">$Target2") || die "$Target2: $!\n";
	
	while(<SOURCE>)
	{
		next unless /\S/;
                last if /^\s*[\#]+/ || /de noordenwind en de zon/i;
		chomp;
		# File 1 & 2
		print TARGET1 $_;	# File 1
		print TARGET2 $_;	# File 2
		# Concatenate Alphabeth and numbers
		unless((/\s([A-Z]\s){4,}/ && !/Z/) || (/\s([0-9]\s){4,}/ && !/12/))
		{
			print TARGET1 "\n";	# File 1
			print TARGET2 "\n";
		}
		else
		{
			print TARGET1 " ";	# File 1
			print TARGET2 " ";	# File 2
		};
		++$Count;
	};
	close(SOURCE);
	close(TARGET1);
	close(TARGET2);

	return $Count;
};

# Convert Text Read aloud
sub convert_read_text	# ($ID, $RecordingNo) -> 0/1
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = shift || return 0;
	my $SourceType = $RecordingNo == 1 ?  "FixedTexts" : "VariableText";
	my $TargetType = $RecordingNo == 1 ? "FT" : "VT";
	#
	my $Source = "$RecordingTextsDir${ID}${RecordingNo}_Texts/${ID}$RecordingNo$SourceType.txt";
	my $Target = "$Transcriptions$ID/${ID}$RecordingNo${TargetType}";
	my $Chunk = 1;

	my $Count = 0;
	#
	open(SOURCE, "<$Source") || die "$Source: /u/cvs/SLcorpus/scripts/TextToTranscriptions.pl,v $!\n";
	open(TARGET, ">$Target$Chunk.txt") || die "$Target$Chunk.txt: $!\n";
	
	# Locate start of text
	if($RecordingNo == 1)
	{
		while(($_ = <SOURCE>) && $_ !~ /de noordenwind en de zon/i){};
		print TARGET $_;
		++$Count;
	}
	else
	{
		# Locate ##################
		while(($_ = <SOURCE>) && !/[\#]+/i){};
	};

	# Skip empty lines
	while(($_ = <SOURCE>) && !/\S/){};
	print TARGET $_ unless /[\#]+/i;
	++$Count;

	while(<SOURCE>)
	{
		next if /[\#]+/i;	# Skip comment lines
		unless(/\S/ && ! /[\#]+/i)
		{
			close(TARGET);
			++$Chunk;
			while(($_ = <SOURCE>) && ($_ !~ /\S/ || $_ =~ /[\#]+/i)){};
			last unless $_ =~ /\S/;
			open(TARGET, ">$Target$Chunk.txt") || die "$Target$Chunk.txt: $!\n";
		};

		print TARGET $_;

		++$Count;
	};
	close(SOURCE);
	close(TARGET);
	return $Count;
};

# Copy Informal text
sub convert_variable_informal  # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 1;
	my $SourceType = "VariableText";
	my $TargetType = "VI";

	unless(-e "$Transcriptions$ID/${ID}${RecordingNo}VT1.txt")
	{
	     die "$ID: No Variable Text reading\n" unless convert_read_text($ID, 2);
	};

	my $ChunkNr = 1;
	while(-e "$Transcriptions$ID/${ID}2VT$ChunkNr.txt")
	{
		`cp $Transcriptions$ID/${ID}2VT$ChunkNr.txt $Transcriptions$ID/${ID}${RecordingNo}VI$ChunkNr.txt`;
		die "$ID: No Variable Informal text\n" unless -e "$Transcriptions$ID/${ID}${RecordingNo}VI$ChunkNr.txt";
		++$ChunkNr;
	}; 
	
	return 1;
};


# Convert Fixed Sentences
sub convert_fixed_sentences     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 1;
	my $SourceType = "FixedSentences";
	my $TargetType = "FS";
	#
	my $Source = "$RecordingTextsDir${ID}${RecordingNo}_Texts/${ID}$RecordingNo$SourceType.txt";
	my $Target = "$Transcriptions$ID/${ID}$RecordingNo$TargetType.txt";

	my $Count = 0;
	#
	open(SOURCE, "<$Source") || die "$Source: /u/cvs/SLcorpus/scripts/TextToTranscriptions.pl,v $!\n";
	open(TARGET, ">$Target") || die "$Target: $!\n";
	#
	while(<SOURCE>)
	{
		next unless /\S/;
                next unless /^[^\#]/;
		print TARGET $_;
		++$Count;
	};

	close(SOURCE);
	close(TARGET);

	return $Count;
};

# Convert Variable Sentences
sub convert_variable_sentences     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 2;
	my $SourceType = "VariableSentences";
	my $TargetType = "VS";
	#
	my $Source = "$RecordingTextsDir${ID}${RecordingNo}_Texts/${ID}$RecordingNo$SourceType.txt";
	my $Target = "$Transcriptions$ID/${ID}$RecordingNo$TargetType.txt";

	my $Count = 0;
	#
	open(SOURCE, "<$Source") || die "$Source: /u/cvs/SLcorpus/scripts/TextToTranscriptions.pl,v $!\n";
	open(TARGET, ">$Target") || die "$Target: $!\n";
	#
	while(<SOURCE>)
	{
		next unless /\S/;
                next unless /^[^\#]/;
		print TARGET $_;
		++$Count;
	};
	close(SOURCE);
	close(TARGET);

	return $Count;
};

# Convert Pseudo Sentences
sub convert_pseudo_sentences     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 2;
	my $SourceType = "SUSentences";
	my $TargetType = "PS";
	#
	my $Source = "$RecordingTextsDir${ID}${RecordingNo}_Texts/${ID}$RecordingNo$SourceType.txt";
	my $Target = "$Transcriptions$ID/${ID}$RecordingNo$TargetType.txt";

	my $Count = 0;
	#
	open(SOURCE, "<$Source") || die "$Source: /u/cvs/SLcorpus/scripts/TextToTranscriptions.pl,v $!\n";
	open(TARGET, ">$Target") || die "$Target: $!\n";
	#
	while(<SOURCE>)
	{
		next unless /\S/;
                next unless /^[^\#]/;
		print TARGET $_;
		++$Count;
	};
	close(SOURCE);
	close(TARGET);


	return $Count;
};

# Convert Fixed Words
sub convert_lists     # ($ID, $RecordingNo, $SourceType, $TargetType) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = shift;
	my $SourceType = shift;
	my $TargetType = shift;
	#
	my $Source = "$RecordingTextsDir${ID}${RecordingNo}_Texts/${ID}$RecordingNo$SourceType.txt";
	my $Target = "$Transcriptions$ID/${ID}$RecordingNo$TargetType.txt";

	my $Count = 0;
	#
	open(SOURCE, "<$Source") || die "$Source: /u/cvs/SLcorpus/scripts/TextToTranscriptions.pl,v $!\n";
	open(TARGET, ">$Target") || die "$Target: $!\n";
	#
	while(<SOURCE>)
	{
		next unless /\S/;
                next unless /^[^\#]/;
		chomp;
		print TARGET "$_ ";
		++$Count;
		# Print a newline every 10 words
		print TARGET "\n" unless $Count % 10;
	};
	print TARGET "\n";

	close(SOURCE);
	close(TARGET);

	return $Count;
};

sub convert_fixed_words     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 1;
	my $SourceType = "FixedWords";
	my $TargetType = "FW";

	return convert_lists($ID, $RecordingNo, $SourceType, $TargetType);
};

# Convert Variable Words
sub convert_variable_words     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 2;
	my $SourceType = "VariableWords";
	my $TargetType = "VW";

	return convert_lists($ID, $RecordingNo, $SourceType, $TargetType);
};

# Convert Fixed Syllables
sub convert_fixed_syllables     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 1;
	my $SourceType = "FixedSyllables";
	my $TargetType = "FY";

	return convert_lists($ID, $RecordingNo, $SourceType, $TargetType);
};


# Convert Variable Syllables
sub convert_variable_syllables     # ($ID) -> 0/1   Sentences
{
	my $ID = shift || return 0;
	#
	my $RecordingNo = 2;
	my $SourceType = "VariableSyllables";
	my $TargetType = "VY";

	return convert_lists($ID, $RecordingNo, $SourceType, $TargetType);
};


# Run the complete conversion
sub run_conversion    # ($ID) -> 0/1 Convert Recording Texts to Transcription texts 
{
	my $ID = shift || return 0;

	# Make sure all directories are present
	die "$ID directories not ready\n" unless prepare_directories($ID);
        
        die "$ID: No fixed sentences\n" unless convert_fixed_sentences($ID);
        die "$ID: No variable sentences\n" unless convert_variable_sentences($ID);
        print STDERR "$ID: No pseudo sentences\n" unless convert_pseudo_sentences($ID);
        die "$ID: No Fixed Words\n" unless convert_fixed_words($ID);
        die "$ID: No Variable Words\n" unless convert_variable_words($ID);
        die "$ID: No Fixed Syllables\n" unless convert_fixed_syllables($ID);
        die "$ID: No Variable Syllables\n" unless convert_variable_syllables($ID);
	die "$ID: No Fixed Pronunciation A\n" unless convert_fixed_pronunciation($ID, 1);
	die "$ID: No Fixed Pronunciation B\n" unless convert_fixed_pronunciation($ID, 2);
	die "$ID: No Fixed Text reading\n" unless convert_read_text($ID, 1);
	die "$ID: No Variable Text reading\n" unless convert_read_text($ID, 2);
    die "$ID: No Variable Informal text\n" unless convert_variable_informal($ID);
};

# Run the conversion
my $ID = shift(@ARGV) || exit 0;

run_conversion($ID);
