#! /usr/bin/perl
#
# Construct new TRANSLIT tier from WORD tier for POS tagging
#
# Use:
# WritePOSsourceSentences.pl <SpeakerGlob> <FileNamePattern> ...
#
# (NO path information)
#
###############################################################################
#
# Copyright R.J.J.H. van Son © 2000, 2001
#
# Author Rob van Son
# Institute of Phonetic Sciences & ACLC
# University of Amsterdam
# Herengracht 338
# NL-1016CG Amsterdam, The Netherlands
# Email: Rob.van.Son@hum.uva.nl
#        rob.van.son@workmail.com
# WWW  : http://www.fon.hum.uva.nl/rob/
# mail:  Institute of Phonetic Sciences
#        University of Amsterdam
#        Herengracht 338
#        NL-1016CG Amsterdam
#        The Netherlands
#        tel +31 205252183
#        fax +31 205252197
#
# License for use and disclaimers
#
# 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.
#
#
#######################################################
#
my $HomeDir = "..";
require "$HomeDir/Links.pl";
require "$Scripts/SentenceLabel.pl";

# Get arguments
my $SpeakerGlob = shift(@ARGV);
$SpeakerGlob = '*' unless $SpeakerGlob;
my @FilePatterns = @ARGV;
@FilePatterns = ('*') unless @FilePatterns;

my $TargetPath = "$HomeDir/home/POS/source";

# Get the real speakers matchin the Speaker Glob
my @SpeakerList = map {m@/?([^\/]+)$@;$1;} glob("$SentenceLabels/$SpeakerGlob");

# Iterate over all speakers
my $CurrentSpeaker;
foreach $CurrentSpeaker (@SpeakerList)
{
	# Skip CVS directories
	next if $CurrentSpeaker =~ /CVS/;
	
	# Make sure the target directory exists
	unless(-d "$TargetPath/$CurrentSpeaker")
	{
	    # Prevent weird directory problems
	    die "$TargetPath/$CurrentSpeaker exists and is NOT a directory\n"
	    	if -e "$TargetPath/$CurrentSpeaker"; 
	    # Create the target directory
            mkdir("$TargetPath/$CurrentSpeaker", 0744) 
	    	|| die "$TargetPath/$CurrentSpeaker: $!\n";
	};
	
	# Iterate over all file patterns
	my $CurrentFilePattern;
	foreach $CurrentFilePattern (@FilePatterns)
	{
		# Get all files matchin the pattern
		my @FileList = glob("$SentenceLabels/$CurrentSpeaker/phoneme/$CurrentFilePattern");
		# Process all files
		my $CurrentFile;
		foreach $CurrentFile (@FileList)
		{
			# Get the Label file
			my $FileID = $CurrentFile =~ m@/?([^/_\.]+)[_\.][^/]+$@ ? $1 : "";
			my $Label = new SentenceLabel;
			$Label->ReadLabelFile($CurrentFile);
			# Construct a new tier
			$Label->copyTier($FileID, 'TRANSLIT');
			# combine all the words in this new tier
			$Label->assembleTierValues($FileID, 'WORDS', ' ', '');
			# Remove Silence
			$Label->presetItem($FileID);
			while($Label->nextItem($FileID) > -1)
			{
				my $text = $Label->currentValue($FileID);
				$text = "" if $text eq '#' || $text eq '*';
				$text =~ s/(^|\s)[\#\*](\s|$)/ /g;
				$text =~ s/[\=]//g;
				$text =~ s/\'\'/\'/g;
				$text .= '.' if $text =~ /\S/ && $text !~ /[\.\?\!\:\;]\s*$/;
				$Label->currentValue($FileID, $text);
			};
			# Write to output
			$Label->print("$TargetPath/$CurrentSpeaker/$FileID.ort", $FileID);
		};
	};
};
