#! /usr/bin/perl
#
# Special purpose program for Unix/Linux:
# Read all arguments as aligned praat Celex label files with 
# translit, words, POS, wordfrequency, syllables, and syllable frequency tiers.
#
# Construct a Database table for each tier.
#
# Use:
# ./ConstructCelexTables.pl <SpeakerGlob>
#
# This will read in all files from <SpeakerGlob> (e.g., F*)
# and constructs Table files with the names <Speaker><tier>.txt
# in the phoneme/<tier> subdirectories of the DatabaseFiles/ directory
#
#
###############################################################################
#
# 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.
#
#
#######################################################
#
$HomeDir = '../../..';
require "$HomeDir/Links.pl";
require "$Scripts/CGN_phonemes.pl";  # For the phoneme classes
require "$Scripts/SentenceLabel.pl";

#
# Where are the files
my $TypeExtension = 'celex';
my $TableDirectory = "$DatabaseTables/$TypeExtension";

#
# Tiers to handle
@TierList = ('LEXSYLLFREQ', 'WORDFREQ', 'SYLLFREQ', 'POS');

# Postediting routines for all tiers
# Given a Sentence Label and a Record list, they will change/add the
# values in the Record.
# 
# ColumnHeaderPostEditing list will change column header names (should work on $_)
my @ColumnHeaderPostEditing = (
's/Value\s+\[TEXT\]/Value [FLOAT4]/g if $Tier =~ /FREQ/i',
's/TRANSLIT/SENTENCE/g',
's/WORDFREQ/WORDS/g',
's/LEXSYLLFREQ/SYLLABLES/g',
's/SYLLFREQ/SYLLABLES/g',
's/POS/WORDS/g',
's/(LEX)?CGN/WORDS/g',
's/LEXSYLL/SYLLABLES/g',
);

# The Column Headers list will add the corresponding column headers.
my @PHONEMEScolumnHeaders = (
'Manner [CHAR(1)] Manner of Articulation', 
'Articulator [CHAR(1)] Primary Articulator', 
'Voicing [CHAR(1)] Voiced (+) / Unvoiced (-)',
'prevManner [CHAR(1)] Manner for previous item', 
'prevArticulator [CHAR(1)] Articulator for previous item', 
'prevVoicing [CHAR(1)] Voicing of previous item',
'nextManner [CHAR(1)] Manner for next item', 
'nextArticulator [CHAR(1)] Articulator for next item', 
'nextVoicing [CHAR(1)] Voicing of next item'
);
sub PHONEMESedit        # ($Label, \@Records)
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    # Phoneme information of current, previous and next Phoneme
    my $i;
    foreach $i (5, 13, 15)
    {
        # Get Manner, Articulator and Voicing
        my $Manner = $CGN_phonemes{$Record->[$i]}[0];
        my $Articulator = $CGN_phonemes{$Record->[$i]}[1];
        my $Voicing = $CGN_phonemes{$Record->[$i]}[2];
        
        push(@$Record, $Manner, $Articulator, $Voicing);
    };
    
    return @$Record;
}

my @SYLLPARTScolumnHeaders = ('NumPhon [INT2] Number of phonemes in syllable part');
sub SYLLPARTSedit       # ($Label, \@Records)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    my $Value = $Record->[5];
    $Value =~ s/[\+\-\~\:]//g;
    push(@$Record, length($Value));
    
    return 1;
}

my @SYLLABLEScolumnHeaders = (
'Stress [CHAR(1)] Syllable stress (+/-)', 
'prevStress [CHAR(1)] Stress of previous item', 
'nextStress [CHAR(1)] Stress of next item', 
'NumPhon [INT2] Number of phonemes in syllable'
);
sub SYLLABLESedit       # ($Label, \@Record)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    # The syllable stress marker
    my $StressedSyllable = ($Record->[5] =~ s/\"//g) ? '+' : '-';
    my $prevStressedSyllable = ($Record->[13] =~ s/\"//g) ? '+' : '-';
    my $nextStressedSyllable = ($Record->[15] =~ s/\"//g) ? '+' : '-';
    
    push(@$Record, $StressedSyllable, $prevStressedSyllable, $nextStressedSyllable);
    
    my $Value = $Record->[5];
    $Value =~ s/[\+\-\~\:]//g;
    push(@$Record, length($Value));
    
    return 1;
}

my @SYLLFREQcolumnHeaders = (
'Stress [CHAR(1)] Syllable stress (+/-)', 
'prevStress [CHAR(1)] Stress of previous item', 
'nextStress [CHAR(1)] Stress of next item', 
'NumPhon [INT2] Number of phonemes in syllable'
);
sub SYLLFREQedit        # ($Label, \@Record)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    # The syllable stress marker
    my $StressedSyllable = ($Record->[5] =~ s/\"//g) ? '+' : '-';
    my $prevStressedSyllable = ($Record->[13] =~ s/\"//g) ? '+' : '-';
    my $nextStressedSyllable = ($Record->[15] =~ s/\"//g) ? '+' : '-';
    
    push(@$Record, $StressedSyllable, $prevStressedSyllable, $nextStressedSyllable);
    
    my $Value = $Record->[5];
    $Value =~ s/[\+\-\~\:]//g;
    push(@$Record, length($Value));
    
    return 1;
}

my @LEXSYLLFREQcolumnHeaders = (
'Stress [CHAR(1)] Syllable stress (+/-)', 
'prevStress [CHAR(1)] Stress of previous item', 
'nextStress [CHAR(1)] Stress of next item', 
'NumPhon [INT2] Number of phonemes in syllable'
);
sub LEXSYLLFREQedit     # ($Label, \@Record)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    # The syllable stress marker
    my $StressedSyllable = ($Record->[5] =~ s/\"//g) ? '+' : '-';
    my $prevStressedSyllable = ($Record->[13] =~ s/\"//g) ? '+' : '-';
    my $nextStressedSyllable = ($Record->[15] =~ s/\"//g) ? '+' : '-';
    
    push(@$Record, $StressedSyllable, $prevStressedSyllable, $nextStressedSyllable);
    
    my $Value = $Record->[5];
    $Value =~ s/[\+\-\~\:]//g;
    push(@$Record, length($Value));
    
    return 1;
}

my @CGNcolumnHeaders = (
'NumSyll [INT2] Number of syllables in word', 
'StressPatt [TEXT] Stress patter of word', 
'NumPhon [INT2] Number of phonemes in word'
);
sub CGNedit     # ($Label, \@Records)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    my $Value = $Record->[5];
    # Get the number of Syllables and the stress pattern
    my @Syllables = map {/\"/ ? 1 : 0;} split(/[\-\_]/, $Value);
    push(@$Record, scalar(@Syllables), join("", @Syllables));
    # Get the number of phonemes
    $Value =~ s/[\+\-\~\:]//g;
    push(@$Record, length($Value));
    
    return 1;
}

my @WORDScolumnHeaders = ('Marker [CHAR(1)] Star marker of transliteration (a/u/v/x)');
sub WORDSedit   # ($Label, \@Records)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    # Split of transcription markers like *a, *u, *v, and *x
    my $Value = $Record->[5];
    my ($Word, $Marker) = ($Value, 'NULL');
    if($Word =~ /\*(\S)/)
    {
        $Marker = uc($1);
        $Word = $`;
    };
    $Record->[5] = $Word;
    
    push(@$Record, $Marker);
    
    # Remove markers from other words
    $Record->[13] =~ s/\*[\w]+//g;
    $Record->[15] =~ s/\*[\w]+//g;
    
    return 1;
}

my @WORDFREQcolumnHeaders = ();
sub WORDFREQedit        # ($Label, \@Records)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    return 1;
}

my @POScolumnHeaders = ();
sub POSedit     # ($Label, \@Records)  
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    return 1;
}

my @TRANSLITcolumnHeaders = (
'NrWords [INT2] Number of words in transliteration', 
'NrSyllables [INT2] Number of syllables in transliteration', 
'NrPhonemes [INT2] Number of phonemes in transliteration',
'WordRate [FLOAT4] Rate in words per second', 
'SyllableRate [FLOAT4] Rate in syllables per second', 
'PhonemeRate [FLOAT4] Rate in phonemes per second'
);
sub TRANSLITedit        # ($Label, \@Records) 
{
    my $Label = shift || return 0;
    my $Record = shift || return 0;
    
    # The word tier
    my $WordTier = 'CGN';
    
    # Remove unwanted items
    splice(@$Record, 12, 4);
    
    # Initialize
    my ($WordNumber, $SyllableNumber, $PhonemeNumber) = (0, 0, 0);
    my $Duration = 0;
    
    # Get the mean words, syllables and phonemes per second from CGN
    # First, count the non-pause words etc. and sum their durations
    $Label->presetItem($WordTier);
    while($Label->nextStartininterval($WordTier, 'TRANSLIT') > -1)
    {
        my $Word = $Label->currentValue($WordTier);
        
        # Skip (filled) pauses (Silence, uh, uhm, enne)
        next if $Word =~ /^(\*|m|\@[mh]?|E[\-n]+\@)$/;
        
        # Get the duration
        $Duration += $Label->currentEnd($WordTier) - $Label->currentStart($WordTier);
        
        # Get Syllables
        my @SyllableList = split(/[\-\_]/, $Word);
        my $Phonemes = $Word;
        # Remove all superfluous chars
        $Phonemes =~ s/[\"\'\-\:\~\+]//g;
        
        # Count words, syllables and phonemes
        ++$WordNumber;
        $SyllableNumber += scalar(@SyllableList);
        $PhonemeNumber += length($Phonemes);
    };
    
    # Calculate the rates (in items per second)
    my ($WordRate, $SyllableRate, $PhonemeRate) = (0, 0, 0);
    if($Duration > 0)
    {
        $WordRate = $WordNumber / $Duration;
        $SyllableRate = $SyllableNumber / $Duration;
        $PhonemeRate = $PhonemeNumber / $Duration;
    };
    
    # Add numbers and rates
    push(@$Record, $WordNumber, $SyllableNumber, $PhonemeNumber, $WordRate, $SyllableRate, $PhonemeRate);
    
    return 1;
}


my %PostEditors = (
'PHONEMES' => \&PHONEMESedit, 
'SYLLPARTS' => \&SYLLPARTSedit, 
'SYLLFREQ' => \&SYLLFREQedit, 
'LEXSYLLFREQ' => \&LEXSYLLFREQedit, 
'SYLLABLES' => \&SYLLABLESedit, 
'CGN' => \&CGNedit, 
'POS' => \&POSedit, 
'WORDFREQ' => \&WORDFREQedit, 
'WORDS' => \&WORDSedit, 
'TRANSLIT' => \&TRANSLITedit
);

my %PostHeader = (
'PHONEMES' => \@PHONEMEScolumnHeaders, 
'SYLLPARTS' => \@SYLLPARTScolumnHeaders, 
'SYLLFREQ' => \@SYLLFREQcolumnHeaders, 
'LEXSYLLFREQ' => \@LEXSYLLFREQcolumnHeaders, 
'SYLLABLES' => \@SYLLABLEScolumnHeaders, 
'CGN' => \@CGNcolumnHeaders, 
'POS' => \@POScolumnHeaders, 
'WORDS' => \@WORDScolumnHeaders, 
'WORDFREQ' => \@WORDFREQcolumnHeaders, 
'TRANSLIT' => \@TRANSLITcolumnHeaders
);


#
# Major loop, process all speakers
my $SpeakerGlob;

foreach $SpeakerGlob (@ARGV)
{
    # Get the label filenames
    @PhonemeFileList = 
    glob("$SentenceLabels/$SpeakerGlob/$TypeExtension/*.$TypeExtension");
    # Process all files
    my $File;
    foreach $File (@PhonemeFileList)
    {
        # Get the ID of the current speaker
        my $CurrentSpeaker = "";
        if($File =~ m@/([^/]+)/$TypeExtension/[^/]+$@)
        {
            $CurrentSpeaker = $1;
        }
        else
        {
            die "No speaker found: $File\n";
        };
        
        # Get the ID of the current author
        my $CurrentAuthor = 'NULL';
        if($File =~ /\_([^\.]+)\.[^\.]+$/)
        {
            $CurrentAuthor = $1;
        };
        
        my $Label = new SentenceLabel;
        next unless $Label->ReadLabelFile($File);
        
        $Label->presetItem('TRANSLIT');
        while($Label->nextItem('TRANSLIT') > -1)
        {
            # Skip empty sentences
            next unless $Label->currentValue('TRANSLIT') =~ /[^\#\*\s]/;
            
            # Correct some odd errors (SentenceID =! TranslitID)
            $Label->currentItem('SENTENCE', $Label->currentItem('TRANSLIT'));
            $Label->currentIDcode('SENTENCE', $Label->currentIDcode('TRANSLIT'));
            
            my $Tier;
            foreach $Tier (@TierList)
            {
                my @Table = $Label->TierToTable($Tier, 'TRANSLIT', $PostEditors{$Tier}, $CurrentAuthor);
                print STDERR "Error with '$Tier' from $File\n" unless @Table;
                
                # Print result to file
                my $PrintHeader = (-e "$TableDirectory/$CurrentSpeaker$Tier$TypeExtension.txt") ? 0 : 1;
                open(OUTPUT, ">>$TableDirectory/$CurrentSpeaker$Tier$TypeExtension.txt") 
                || die ">>$TableDirectory/$CurrentSpeaker$Tier$TypeExtension.txt: $!\n";
                
                # Print the column headers
                if($PrintHeader)
                {
                    # Make sure the tier is somewhere
                    $Label->currentItem($Tier, 1) unless $Tier eq 'TRANSLIT';
                    
                    # Construct the list with column headers
                    my @CurrentHeaders = $Label->TierToTableColumnheaders($Tier, @{$PostHeader{$Tier}}, 
                    'Author [TEXT] ID of annotator');
                    # Remove some items if using the TRANSLIT tier
                    splice(@CurrentHeaders, 12, 4) if $Tier eq 'TRANSLIT';
                    
                    # Post editing of the column headers
                    @CurrentHeaders = map {eval join(";", @ColumnHeaderPostEditing); $_;} @CurrentHeaders;
                    
                    # Print the column headers
                    print OUTPUT "\#> ", join("\n\#> ", @CurrentHeaders), "\n";
                    
                    # Print some extra information
                    print OUTPUT "\# ";
                    print OUTPUT "Tier: $Tier\n\# Speaker: $CurrentSpeaker\n\# Source: $TypeExtension\n";
                    print OUTPUT "\#\n";

		    # Print the copyright license
		    if(open(LICENSE, "<./licenseterms.txt"))
		    {
			    my $Date = localtime();
			    $Date =~ /\s([\d]+)\s*$/;
			    my $Year = $1;
			    while(<LICENSE>)
			    {
				    if(/\©/ && ! /$Year/)
				    {
					    s/(\d)\s{6}(\s+\#)/\1, $Year\2/ig;
				    };
				    print OUTPUT $_;
			    };
			    close(LICENSE)
		    };

                    
                    # Header is printed ONLY once
                    $PrintHeader = 0;
                };
                
                my $Record;
                foreach $Record (@Table)
                {
                    print OUTPUT join("\t", @$Record), "\n";
                };
                close(OUTPUT);
            };
        };
    };
    
};



