#! /usr/bin/perl
# 
# Convert a word list from the following query into a
# readable lexicon for the IFAcorpus:
#
# This program is very straightforeward. It reads a large transcription dictionary
# and just replaces the transcription with a tab-delimited list of:
# 1) A CGN compatible phoneme transcription
# 2) as 1) but without the syllable boundaries (-) and stress markers (")
# 3) A transcription in terms of Manner of Articulation
# 4) A transcription in terms of Primary Articulator
# 5) A transcription in terms of Voicing
#
# 
# Date: Tue Jan 29 12:43:41 2002
# QUERY: 
# SELECT DISTINCT
# 	WORDSPHONEME.value as WORDSPHONEME_value, 
# 	LEXCGNPHONEME.value as LEXCGNPHONEME_value, 
# 	POSPOS.value as POSPOS_value, 
# 	WORDSPHONEME.texttype as WORDSPHONEME_texttype, 
# 	COUNT(WORDSPHONEME.ID) as COUNTID
# FROM 
# 	WORDSPHONEME,
# 	LEXCGNPHONEME,
# 	POSPOS
# WHERE 
# 	(LEXCGNPHONEME.id = WORDSPHONEME.id AND
# 	WORDSPHONEME.id = LEXCGNPHONEME.id AND
# 	POSPOS.id = WORDSPHONEME.id AND
# 	WORDSPHONEME.id = POSPOS.id) AND
# 	WORDSPHONEME.style in ('R', 'T') AND
# 	( 	WORDSPHONEME.id !~ 'FT' AND 
# 	WORDSPHONEME.marker !~ '[AX]'  
# 	)
# GROUP BY 
# 	WORDSPHONEME_value,
# 	LEXCGNPHONEME_value,
# 	POSPOS_value,
# 	WORDSPHONEME_texttype
# 
# Nrows: 2462
# Table: WORDSPHONEME
# Function: COUNT
# 
# COUNT
# 
# Group (rows X columns):
# WORDSPHONEME_value;LEXCGNPHONEME_value;POSPOS_value
#  X WORDSPHONEME_texttype
# Counts
# 
# Add the following Column Definitions to the top of the
# output file:
# 
#> Number [INT4] Ordinal number of word in list (a kind of ID)
#> Word [Text] Orthographic transcription of word
#> CGN [TEXT] Phonemically transcribed word (CGN format)
#> Phonemes [TEXT] Phonemically transcribed word WITHOUT syllable boundaries or stress markers (CGN format)
#> Manner [TEXT] Manner of Articulation
#> Articulator [TEXT] Primary Articulator
#> Voicing [TEXT] Voiced (V) / Unvoiced (U)
#> STRESSPAT [TEXT] Syllable Stress (1/0) pattern
#> FREQUENCY [INT4] Number of occurrences
#> CLASS [CHAR(4)] Part of Speech category
#
# 
#
###############################################################################
#
# Copyright R.J.J.H. van Son © 2000, 2001
#
# Author David Weenink, 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.
#
#
#######################################################
#
require 'CGN_phonemes.pl';
#
# The Column Headers list will add the corresponding column headers.
my @LineexiconColumnHeaders = (
'Number [INT4] Ordinal number of word in list (a kind of ID)',
'Word [Text] Orthographic transcription of word', 
'CGN [TEXT] Phonemically transcribed word (CGN format)', 
'Phonemes [TEXT] Phonemically transcribed word WITHOUT syllable boundaries or stress markers (CGN format)', 
'Manner [TEXT] Manner of Articulation', 
'Articulator [TEXT] Primary Articulator', 
'Voicing [TEXT] Voiced (V) / Unvoiced (U)',
'STRESSPAT [TEXT] Syllable Stress (1/0) pattern', 
'FREQUENCY [INT4] Number of occurrences', 
'CLASS [CHAR(4)] Part of Speech category'
);


#
# Get Input File and open it
#
my $InputFileName = shift(@ARGV) || '-';
if($InputFileName =~ /\.gz$/)
{
	open(INPUT, "gunzip -c '$InputFileName' | ") || die "gunzip -c '$InputFileName': $!\n";
}
else
{
	open(INPUT, "<$InputFileName") || die "$InputFileName: $!\n";
};


# Print the column headers
print STDOUT "\#> ", join("\n\#> ", @LineexiconColumnHeaders), "\n";

# Print the copyright license
if(open(LICENSE, "<$HomeDir/DatabaseFiles/scripts/ConstructTables/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 STDOUT $_;
    };
    close(LICENSE)
};

# Keep track of the summ frequency
my $SummFrequency = 0;
# Start at 1
my $WordCounter = 1;
while(<INPUT>)
{
	if(/^\#/ || !/\S/)
	{
		print $_;
		next
	};
	next if(/^\s*\*/);
	split;
	@Line = map {@a = split(/\;/); @a} @_; 
	splice(@Line, 3, 2);
	$SummFrequency += $Line[3];
	@StressPattern = split(/\-/, $Line[1]);
	@StressPattern = map {/\"/ ? 1 : 0;} @StressPattern;
	$Stress = join("", @StressPattern);
	push(@Line, $Stress);
	
	# Construct the phonemes and feature attributes
	$NoSyllable = $Line[1];
	$NoSyllable =~ s/[\-\"]//g;
	
	# Construct Manner of Articulation, Articulator, and voicing
	@Extra = ($NoSyllable, CGNtoManner($NoSyllable), CGNtoArticulator($NoSyllable), CGNtoVoicing($NoSyllable));

	# Print them
	print join("\t", $WordCounter, $Line[0], $Line[1], @Extra, $Line[4], $Line[3], $Line[2]), "\n";
	
	++$WordCounter;
};
# Print the summ frequency
print STDOUT "\# Summ Frequency = $SummFrequency\n";
