#!/usr/bin/make
# Copyright (C) 2012  Sergio Oller <sergioller@gmail.com>
#
#  This script is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation,
#  version 2.1 of the License.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


# Meta Information:
PACKAGE_NAME = @PACKAGE_NAME@
PACKAGE_TARNAME = @PACKAGE_TARNAME@
PACKAGE_VERSION = @PACKAGE_VERSION@
PACKAGE_STRING = @PACKAGE_STRING@
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
PACKAGE_URL = @PACKAGE_URL@

# Programs:
FESTIVAL = @FESTIVALBIN@
WAGON = @WAGON@
PERL = @PERL@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL_DATA@
MKDIR_P = @MKDIR_P@
SED = @SED@
GREP = @GREP@
AWK = @AWK@
NGRAM_BUILD = @NGRAMBUILD@
ONLYINSTALL = @ONLYINSTALL@

# Installation Variables:
FESTDATADIR = @FESTDATADIR@
LANGUAGESSCMFILE = @LANGUAGESSCMFILE@
LANGUAGESSCMVERSION = @LANGUAGESSCMVERSION@

# Empty variables to be filled:
CLEAN = 
DISTCLEAN = 

# File lists:
LTS_RULES_CENTRAL = festival/lib/dicts/upc/upc_catalan_lts_rules-central.scm
REDUCED_LEXICON_CENTRAL = festival/lib/dicts/upc/upcdict_catalan-1.0-central.out

LTS_RULES_VALENCIA = festival/lib/dicts/upc/upc_catalan_lts_rules-valencia.scm
REDUCED_LEXICON_VALENCIA = festival/lib/dicts/upc/upcdict_catalan-1.0-valencia.out

POSLEX = festival/lib/dicts/upc/upc_freeling_catalan.poslex

LIB_UPCCATALAN_FILES = festival/lib/upc_catalan/cat_intro.text \
              festival/lib/upc_catalan/upc_ca_generic_duration.scm \
              festival/lib/upc_catalan/upc_ca_generic_durdata-central.scm \
              festival/lib/upc_catalan/upc_ca_generic_durdata-valencia.scm \
              festival/lib/upc_catalan/upc_ca_generic_f0model.scm \
              festival/lib/upc_catalan/upc_ca_generic_intonation.scm \
              festival/lib/upc_catalan/upc_ca_generic_lexicon.scm \
              festival/lib/upc_catalan/upc_ca_generic_phoneset.scm \
              festival/lib/upc_catalan/upc_ca_generic_phrasing.scm \
              festival/lib/upc_catalan/upc_ca_generic_tagger.scm \
              festival/lib/upc_catalan/upc_ca_generic_tokenizer.scm \
              festival/lib/upc_catalan/upc_catalan_date.scm \
              festival/lib/upc_catalan/upc_catalan_hour.scm \
              festival/lib/upc_catalan/upc_catalan_numbers.scm \
              festival/lib/upc_catalan/upc_catalan_phones-central.scm \
              festival/lib/upc_catalan/upc_catalan_phones-valencia.scm \
              festival/lib/upc_catalan/upc_catalan_phones.scm \
              festival/lib/upc_catalan/upc_catalan_pos_tagger.scm \
              festival/lib/upc_catalan/upc_catalan.scm \
              festival/lib/upc_catalan/upc_catalan_tokenizer.scm \
              festival/lib/upc_catalan/upc_catalan_tokenpos.scm

ALLOWABLES = 
LIB_DICTS_UPCLEX = festival/lib/dicts/upc/upclex_catalan.scm \
              festival/lib/dicts/upc/upclex_central.scm \
              festival/lib/dicts/upc/upclex_valencia.scm \
              festival/lib/dicts/upc/upclex_postlex.scm \
              festival/lib/dicts/upc/upclex_syl.scm

LIB_DICTS_FILES = $(LTS_RULES_CENTRAL) \
              $(REDUCED_LEXICON_CENTRAL) \
              $(LTS_RULES_VALENCIA) \
              $(REDUCED_LEXICON_VALENCIA) \
              $(POSLEX) \
              festival/lib/dicts/upc/upc_freeling_catalan.tri.ngrambin \
              $(LIB_DICTS_UPCLEX)
LANGUAGE_FILES =  festival/lib/languages/language_catalan.scm \
              festival/lib/languages.scm

BUILD_FILES = $(LIB_UPCCATALAN_FILES) $(LIB_DICTS_FILES) $(LANGUAGE_FILES)

# Test files:
TESTTOKENIZERTXT = test/characters.txt test/full_llista.txt test/CheckPunctuation.txt
TESTTOKENIZEROUT = $(addsuffix .out,$(TESTTOKENIZERTXT))
TESTTOKENIZEROUT2 = $(addsuffix .out2,$(TESTTOKENIZERTXT))
TESTTOKENIZERDIFF = $(addsuffix .diff,$(TESTTOKENIZERTXT))
TESTTOKENIZERDIFF2 = $(addsuffix .diff2,$(TESTTOKENIZERTXT))
TESTTOKENIZERREF = $(addsuffix .ref,$(TESTTOKENIZERTXT))
TESTTOKENIZERREF2 = $(addsuffix .ref2,$(TESTTOKENIZERTXT))
TESTSRCFILES = $(TESTTOKENIZERTXT) $(TESTTOKENIZERREF) $(TESTTOKENIZERREF2) test/print_utterance

# Distribution variables:
DOCUMENTATIONFILES = AUTHORS AUTHORS.ca COPYRIGHT LICENSE-gpl-2.0.txt \
                     LICENSE-lgpl-2.1.txt README.ca.md README.md \
                     THANKS.md THANKS.ca.md USAGE.ca.md USAGE.md \
                     INSTALL.md INSTALL.ca.md

DISTBINFILES = Makefile.in Makefilevars.inc configure.ac configure \
               install-sh $(DOCUMENTATIONFILES) $(BUILD_FILES)
DISTSRCFILES = Makefile.in Makefilevars.inc configure.ac configure \
               install-sh $(DOCUMENTATIONFILES) $(BUILD_FILES) $(TESTSRCFILES)

CLEAN += $(TESTTOKENIZEROUT) $(TESTTOKENIZEROUT2) $(TESTTOKENIZERDIFF) $(TESTTOKENIZERDIFF2)

DISTCLEAN += Makefile config.status config.log

# Source directories
BUILDDICT = src/00-Dictionary
BUILDLTS = src/02-LTSMake
BUILDPOSLEX = src/03-Poslex
BUILDNGRAM = src/04-ngram
BUILDDATA = src/data
BUILDFUNC = src/functions

DICTANDTRANSCRIPTIONS = src/data/withtranscriptions/ca.orto
DICTANDTRANSCRIPTIONS_CENTRAL  = src/data/withtranscriptions/ca-central.phon
DICTANDTRANSCRIPTIONS_VALENCIA = src/data/withtranscriptions/ca-valencia.phon

.PHONY: all build install dist-bin dist-src test test-lts test-tokenizer

ifeq ($(ONLYINSTALL),yes)
  DEFAULTACTION = install
else
  DEFAULTACTION = build
endif

all: $(DEFAULTACTION)

build: $(BUILD_FILES)

# Files to be copied from src/data/festival...:
$(LANGUAGE_FILES) $(LIB_UPCCATALAN_FILES) $(LIB_DICTS_UPCLEX):
	$(MKDIR_P) $(@D) && \
	cp "$(BUILDDATA)/$@" "$@" || exit 1

# This rules are to perform the phonetic transcriptions of the Freeling Dictionary
# using an in-house tool at UPC.
# Phonetic transcriptions are distributed under a free license and 
# editing and correcting them is quite easy if you find mistakes or you would
# like to adapt them to other Catalan dialects.
src/data/withtranscriptions/ca.orto src/data/withtranscriptions/ca-central.phon:
	if [ -f `which phonetic_trn` ]; then \
	cd $(BUILDDATA) && \
	( ./transcribe.sh "withtranscriptions/from_phonetic_trn" from_freeling/*  || exit 1) && \
	( ./concatenate.sh "withtranscriptions" "withtranscriptions/from_phonetic_trn" || exit 1 ) ; \
	fi

####################################################################################
####################################################################################
## The Catalan Letter to Sound Rules and the Catalan dictionary are built as follows:
## 1. Using the freeling dictionary and some other complementary dictionaries, a lexicon 
##    with words and their phonetic transcriptions is built.
## 2. Using the upc_catalan_lts_rules, Letter to Sound rules are trained from the lexicon 
##    built in step 1. 
## 3. The lexicon is reduced removing the words that can already be predicted with LTS rules.
##    This is done by comparing the phonetic transcription from the Lexicon and the phonetic
##    transcription predicted by the LTS rules. Iff there is a mismatch between the transcriptions
##    the word with the correct transcription is kept in the lexicon, otherwise it is removed. 

# Clean and distclean and src files for dialect-independent:
CLEAN+= $(BUILDDICT)/merged $(BUILDDICT)/final
DISTCLEAN+= $(BUILDDICT)/CompileDictionary.sh

DISTSRCFILES+= $(BUILDDICT)/CreateInternalFormat.pl \
               $(BUILDDICT)/CompileDictionary.sh.in \
               $(BUILDDICT)/list2festival.pl \
               $(BUILDDICT)/allo2pho.pl

###############################################################################
###############################################################################
###############################################################################
##########################   CENTRAL   ########################################
###############################################################################
###############################################################################
###############################################################################

## Catala central:

DISTSRCFILES+= $(BUILDDICT)/addfinalentries-central.sh
COMPLETE_LEXICON_CENTRAL=$(BUILDDICT)/final/central/upcdict_catalan.out
# Step 1:
$(COMPLETE_LEXICON_CENTRAL): $(DICTANDTRANSCRIPTIONS) $(DICTANDTRANSCRIPTIONS_CENTRAL) $(BUILDDICT)/CreateInternalFormat.pl $(BUILDDICT)/CompileDictionary.sh $(BUILDDICT)/addfinalentries-central.sh $(BUILDDICT)/allo2pho.pl $(BUILDDICT)/list2festival.pl
	echo "Creating dictionary in internal format" && \
	cd $(BUILDDICT) && \
	( $(PERL) ./CreateInternalFormat.pl "../data/withtranscriptions/ca.orto" "../data/withtranscriptions/ca-central.phon" "merged/central/freeling.dic" || exit 1 ) && \
	echo "Adding other dictionaries" && \
	[ -d "../data/otherdic/central" ] && [ -e ../data/otherdic/central/*.dic ] && cp ../data/otherdic/central/*.dic "merged/central/" ; \
	echo "Compiling dictionary" && \
	( ./CompileDictionary.sh "final/central/" "central" merged/central/*.dic || exit 1 ) && \
	echo "Adding final entries" && \
	( ./addfinalentries-central.sh "final/central/upcdict_catalan.out" || exit 1 ) && \
	echo "Removing duplicates" && \
	cat "final/central/upcdict_catalan.out" | LANG=C uniq > "final/central/upcdict_catalan.out2" && \
	rm -f "final/central/upcdict_catalan.out" && mv "final/central/upcdict_catalan.out2" "final/central/upcdict_catalan.out" || exit 1

###############################################################################
###############################################################################
###############################################################################
##########################   VALENCIA   #######################################
###############################################################################
###############################################################################
###############################################################################

DISTSRCFILES+= $(BUILDDICT)/addfinalentries-valencia.sh

COMPLETE_LEXICON_VALENCIA=$(BUILDDICT)/final/valencia/upcdict_catalan.out
# Step 1:
$(COMPLETE_LEXICON_VALENCIA): $(DICTANDTRANSCRIPTIONS) $(DICTANDTRANSCRIPTIONS_VALENCIA) $(BUILDDICT)/CreateInternalFormat.pl $(BUILDDICT)/CompileDictionary.sh $(BUILDDICT)/addfinalentries-valencia.sh $(BUILDDICT)/allo2pho.pl $(BUILDDICT)/list2festival.pl
	echo "Creating dictionary in internal format" && \
	cd $(BUILDDICT) && \
	( $(PERL) ./CreateInternalFormat.pl "../data/withtranscriptions/ca.orto" "../data/withtranscriptions/ca-valencia.phon" "merged/valencia/freeling.dic" || exit 1 ) && \
	echo "Adding other dictionaries" && \
	[ -d "../data/otherdic/valencia" ] && [ -e ../data/otherdic/valencia/*.dic ] && cp ../data/otherdic/valencia/*.dic "merged/valencia/" ; \
	echo "Compiling dictionary" && \
	( ./CompileDictionary.sh "final/valencia/" "valencia" merged/valencia/*.dic || exit 1 ) && \
	echo "Adding final entries" && \
	( ./addfinalentries-valencia.sh "final/valencia/upcdict_catalan.out" || exit 1 ) && \
	echo "Removing duplicates" && \
	cat "final/valencia/upcdict_catalan.out" | LANG=C uniq > "final/valencia/upcdict_catalan.out2" && \
	rm -f "final/valencia/upcdict_catalan.out" && mv "final/valencia/upcdict_catalan.out2" "final/valencia/upcdict_catalan.out" || exit 1


##############################################################################
##############################################################################
##############################################################################
######################  CENTRAL: LTS   #######################################
##############################################################################
##############################################################################
##############################################################################
# Step 2: 

# Just to make thinks harder, Festival creates invalid filenames that cannot be represented in a UTF-8 Makefile.
# Makefilevars.inc contains the required makefile variables that must be encoded in ISO-8859-15
include Makefilevars.inc

ltsdataTRAINfeats_central = $(addprefix $(BUILDLTS)/lts_scratch/central/,$(ltsdataTRAINfeatsfn))
ltsdataTESTfeats_central  = $(addprefix $(BUILDLTS)/lts_scratch/central/,$(subst ltsdataTRAIN,ltsdataTEST,$(ltsdataTRAINfeatsfn)))
ltsNYtree_central         = $(addprefix $(BUILDLTS)/lts_scratch/central/,$(ltsNYtreefn))
ltsNYtreesrc_central      = $(BUILDLTS)/lts.ny.tree
ltsNYTRAINfeats_central        = $(addprefix $(BUILDLTS)/lts_scratch/central/,$(subst lts,ltsdataTRAIN,$(ltsNYtreefn:.tree=.feats)))
ltsNYTESTfeats_central        = $(addprefix $(BUILDLTS)/lts_scratch/central/,$(subst lts,ltsdataTEST,$(ltsNYtreefn:.tree=.feats)))
ltsdataTRAINtree_central  = $(subst ltsdataTRAIN,lts,$(ltsdataTRAINfeats_central:.feats=.tree))  $(ltsNYtree_central)
laNY_central              = $(subst lts.,,$(ltsNYtreefn:.tree=))

# Building the LTS rules requires some intermediate files.
ALLOWABLES += $(BUILDLTS)/allowables-1.1-central.scm
$(BUILDLTS)/lts_scratch/central/lex_entries.out: $(COMPLETE_LEXICON_CENTRAL)
	$(MKDIR_P) $(BUILDLTS)/lts_scratch/central && \
	$(BUILDLTS)/purge_fest_dic < $(COMPLETE_LEXICON_CENTRAL) | $(BUILDLTS)/fest2wagon > $(BUILDLTS)/lts_scratch/central/lex_entries.out

$(BUILDLTS)/lts_scratch/central/lex-pl-tablesp.scm: $(BUILDLTS)/lts_scratch/central/lex_entries.out $(BUILDLTS)/allowables-1.1-central.scm
	cd $(BUILDLTS) && ./build_lts cummulate  "allowables-1.1-central.scm" "lts_scratch/central" || exit 1

$(BUILDLTS)/lts_scratch/central/lex.feats: $(BUILDLTS)/lts_scratch/central/lex-pl-tablesp.scm $(BUILDLTS)/lts_scratch/central/lex_entries.out $(BUILDLTS)/allowables-1.1-central.scm
	cd $(BUILDLTS) && ./build_lts align "allowables-1.1-central.scm" "lts_scratch/central" || exit 1

$(BUILDLTS)/lts_scratch/central/lex.feats.train $(BUILDLTS)/lts_scratch/central/lex.feats.test \
$(BUILDLTS)/lts_scratch/central/lex.align.train $(BUILDLTS)/lts_scratch/central/lex.align.test: $(BUILDLTS)/lts_scratch/central/lex.feats
	cd $(BUILDLTS) && ./build_lts traintest "allowables-1.1-central.scm" "lts_scratch/central" || exit 1


$(BUILDLTS)/lts_scratch/central/ltsLTS.desc: $(BUILDLTS)/lts_scratch/central/lex.feats $(BUILDLTS)/lts_scratch/central/lex.feats.train
	echo "Creating ltsLTS.desc"
	export LANG=C; \
	echo "((Relation.LTS.down.name" > $@ ; \
	$(AWK) '{print $$1}' $< | sort -u >> $@ ; \
	echo ') (p.p.p.p.name ' >> $@ ; \
	$(AWK) '{print $$2}' $< | sort -u >> $@ ; \
	echo ') (p.p.p.name ' >> $@ ; \
	$(AWK) '{print $$3}' $< | sort -u >> $@ ; \
	echo ') (p.p.name ' >> $@ ; \
	$(AWK) '{print $$4}' $< | sort -u >> $@ ; \
	echo ') (p.name ' >> $@ ; \
	$(AWK) '{print $$5}' $< | sort -u >> $@ ; \
	echo ') (name ' >> $@ ; \
	$(AWK) '{print $$6}' $< | sort -u >> $@ ; \
	echo ') (n.name ' >> $@ ; \
	$(AWK) '{print $$7}' $< | sort -u >> $@ ; \
	echo ') (n.n.name ' >> $@ ; \
	$(AWK) '{print $$8}' $< | sort -u >> $@ ; \
	echo ') (n.n.n.name ' >> $@ ; \
	$(AWK) '{print $$9}' $< | sort -u >> $@ ; \
	echo ') (n.n.n.n.name ' >> $@ ; \
	$(AWK) '{print $$10}' $< | sort -u >> $@ ; \
	echo ') (pos ignore' >> $@ ; \
	$(AWK) 'BEGIN {print "n"} {print $$11}' $< | sort -u >> $@ ; \
	echo '))' >> $@
	echo "ltsLTS.desc created"

$(ltsdataTESTfeats_central): $(BUILDLTS)/lts_scratch/central/lex.feats.test
	echo "Building $@"
	$(eval LETRA := $(subst .,,$(suffix $(basename $@))))
	echo "Letra: $(LETRA)"
	export LANG=C; \
	cat $< | \
	$(AWK) '{if ($$6 == "$(LETRA)") print $$0}' > $@; 

$(ltsdataTRAINfeats_central): $(BUILDLTS)/lts_scratch/central/lex.feats.train
	echo "Building $@"
	$(eval LETRA := $(subst .,,$(suffix $(basename $@))))
	echo "Letra: $(LETRA)"
	export LANG=C; \
	cat $< | \
	$(AWK) '{if ($$6 == "$(LETRA)") print $$0}' > $@; 

$(ltsNYTRAINfeats_central) $(ltsNYTESTfeats_central):
	echo "La ny castellana ja se sap quin so té i no cal entrenar-la"
	$(MKDIR_P) $(@D) && \
	touch $@

#$(ltstreeNY_central): $(ltsNYtreesrc_central)
#	echo "This is copied"
#	cp $< $@ || exit 1
	
$(BUILDLTS)/lts_scratch/central/lts.%.tree: $(BUILDLTS)/lts_scratch/central/ltsdataTRAIN.%.feats $(BUILDLTS)/lts_scratch/central/ltsdataTEST.%.feats
	echo "Wagon with $*"
	if [ $(laNY_central) = "$*" ]; then \
		cp $(ltsNYtreesrc_central) $@ || exit 1; \
	else \
	$(WAGON) -data $(BUILDLTS)/lts_scratch/central/ltsdataTRAIN.$*.feats -test $(BUILDLTS)/lts_scratch/central/ltsdataTEST.$*.feats \
          -desc $(BUILDLTS)/lts_scratch/central/ltsLTS.desc -stop 3 -output $(BUILDLTS)/lts_scratch/central/lts.$*.tree || exit 1; \
	fi \

$(BUILDLTS)/lts_scratch/central/lex_lts_rules.scm: $(BUILDLTS)/lts_scratch/central/ltsLTS.desc  $(ltsdataTRAINtree_central)
	cd $(BUILDLTS) && ./build_lts merge  "allowables-1.1-central.scm" "lts_scratch/central" || exit 1

$(BUILDLTS)/lts_scratch/central/lts_test.log: $(BUILDLTS)/lts_scratch/central/lex_lts_rules.scm $(BUILDLTS)/lts_scratch/central/lex.align $(BUILDLTS)/lts_scratch/central/lex.align.test $(BUILDLTS)/allowables-1.1-central.scm
	cd $(BUILDLTS) && ./build_lts test  "allowables-1.1-central.scm" "lts_scratch/central"


$(LTS_RULES_CENTRAL): $(BUILDLTS)/lts_scratch/central/lex_lts_rules.scm
	$(MKDIR_P) "$(BUILDLTS)/final" && \
	$(SED) -e 's/·/"·"/g' -e 's/lex_lts_rules/upc_catalan_lts_rules-central/g' "$(BUILDLTS)/lts_scratch/central/lex_lts_rules.scm" > "$(BUILDLTS)/final/upc_catalan_lts_rules-central.scm" && \
	echo "(provide 'upc_catalan_lts_rules-central)" >> "$(BUILDLTS)/final/upc_catalan_lts_rules-central.scm" && \
	$(MKDIR_P) $(@D) && \
	cp $(BUILDLTS)/final/upc_catalan_lts_rules-central.scm $@ || exit 1

TESTLTS += $(BUILDLTS)/lts_scratch/central/lts_test.log

CLEAN+= $(BUILDLTS)/lex_lts_rules-central.scm $(BUILDLTS)/lts_scratch/central/ $(BUILDLTS)/final
DISTCLEAN+= $(BUILDLTS)/build_lts

DISTSRCFILES+= $(ALLOWABLES) \
               $(BUILDLTS)/build_lts.in \
               $(BUILDLTS)/build_lts_rules \
               $(BUILDLTS)/createmultiphonemes \
               $(BUILDLTS)/fest2wagon \
               $(BUILDLTS)/lts.ny.tree \
               $(BUILDLTS)/purge_fest_dic \
               $(BUILDLTS)/readme \
               $(BUILDLTS)/reduce_dict \
               $(BUILDLTS)/traintest \
               $(BUILDLTS)/upc_catalan_lts_rules

##############################################################################
##############################################################################
##############################################################################
######################  VALENCIA: LTS   ######################################
##############################################################################
##############################################################################
##############################################################################

# Just to make thinks harder, Festival creates invalid filenames that cannot be represented in a UTF-8 Makefile.
# Makefilevars.inc contains the required makefile variables that must be encoded in ISO-8859-15
include Makefilevars.inc

ltsdataTRAINfeats_valencia = $(addprefix $(BUILDLTS)/lts_scratch/valencia/,$(ltsdataTRAINfeatsfn))
ltsdataTESTfeats_valencia  = $(addprefix $(BUILDLTS)/lts_scratch/valencia/,$(subst ltsdataTRAIN,ltsdataTEST,$(ltsdataTRAINfeatsfn)))
ltsNYtree_valencia         = $(addprefix $(BUILDLTS)/lts_scratch/valencia/,$(ltsNYtreefn))
ltsNYtreesrc_valencia      = $(BUILDLTS)/lts.ny.tree
ltsNYTRAINfeats_valencia        = $(addprefix $(BUILDLTS)/lts_scratch/valencia/,$(subst lts,ltsdataTRAIN,$(ltsNYtreefn:.tree=.feats)))
ltsNYTESTfeats_valencia        = $(addprefix $(BUILDLTS)/lts_scratch/valencia/,$(subst lts,ltsdataTEST,$(ltsNYtreefn:.tree=.feats)))
ltsdataTRAINtree_valencia  = $(subst ltsdataTRAIN,lts,$(ltsdataTRAINfeats_valencia:.feats=.tree))  $(ltsNYtree_valencia)
laNY_valencia              = $(subst lts.,,$(ltsNYtreefn:.tree=))

# Building the LTS rules requires some intermediate files.
ALLOWABLES += $(BUILDLTS)/allowables-1.1-valencia.scm
$(BUILDLTS)/lts_scratch/valencia/lex_entries.out: $(COMPLETE_LEXICON_VALENCIA)
	$(MKDIR_P) $(BUILDLTS)/lts_scratch/valencia && \
	$(BUILDLTS)/purge_fest_dic < $(COMPLETE_LEXICON_VALENCIA) | $(BUILDLTS)/fest2wagon > $(BUILDLTS)/lts_scratch/valencia/lex_entries.out

$(BUILDLTS)/lts_scratch/valencia/lex-pl-tablesp.scm: $(BUILDLTS)/lts_scratch/valencia/lex_entries.out $(BUILDLTS)/allowables-1.1-valencia.scm
	cd $(BUILDLTS) && ./build_lts cummulate  "allowables-1.1-valencia.scm" "lts_scratch/valencia" || exit 1

$(BUILDLTS)/lts_scratch/valencia/lex.feats: $(BUILDLTS)/lts_scratch/valencia/lex-pl-tablesp.scm $(BUILDLTS)/lts_scratch/valencia/lex_entries.out $(BUILDLTS)/allowables-1.1-valencia.scm
	cd $(BUILDLTS) && ./build_lts align "allowables-1.1-valencia.scm" "lts_scratch/valencia" || exit 1

$(BUILDLTS)/lts_scratch/valencia/lex.feats.train $(BUILDLTS)/lts_scratch/valencia/lex.feats.test \
$(BUILDLTS)/lts_scratch/valencia/lex.align.train $(BUILDLTS)/lts_scratch/valencia/lex.align.test: $(BUILDLTS)/lts_scratch/valencia/lex.feats
	cd $(BUILDLTS) && ./build_lts traintest "allowables-1.1-valencia.scm" "lts_scratch/valencia" || exit 1


$(BUILDLTS)/lts_scratch/valencia/ltsLTS.desc: $(BUILDLTS)/lts_scratch/valencia/lex.feats $(BUILDLTS)/lts_scratch/valencia/lex.feats.train
	echo "Creating ltsLTS.desc"
	export LANG=C; \
	echo "((Relation.LTS.down.name" > $@ ; \
	$(AWK) '{print $$1}' $< | sort -u >> $@ ; \
	echo ') (p.p.p.p.name ' >> $@ ; \
	$(AWK) '{print $$2}' $< | sort -u >> $@ ; \
	echo ') (p.p.p.name ' >> $@ ; \
	$(AWK) '{print $$3}' $< | sort -u >> $@ ; \
	echo ') (p.p.name ' >> $@ ; \
	$(AWK) '{print $$4}' $< | sort -u >> $@ ; \
	echo ') (p.name ' >> $@ ; \
	$(AWK) '{print $$5}' $< | sort -u >> $@ ; \
	echo ') (name ' >> $@ ; \
	$(AWK) '{print $$6}' $< | sort -u >> $@ ; \
	echo ') (n.name ' >> $@ ; \
	$(AWK) '{print $$7}' $< | sort -u >> $@ ; \
	echo ') (n.n.name ' >> $@ ; \
	$(AWK) '{print $$8}' $< | sort -u >> $@ ; \
	echo ') (n.n.n.name ' >> $@ ; \
	$(AWK) '{print $$9}' $< | sort -u >> $@ ; \
	echo ') (n.n.n.n.name ' >> $@ ; \
	$(AWK) '{print $$10}' $< | sort -u >> $@ ; \
	echo ') (pos ignore' >> $@ ; \
	$(AWK) 'BEGIN {print "n"} {print $$11}' $< | sort -u >> $@ ; \
	echo '))' >> $@
	echo "ltsLTS.desc created"

$(ltsdataTESTfeats_valencia): $(BUILDLTS)/lts_scratch/valencia/lex.feats.test
	echo "Building $@"
	$(eval LETRA := $(subst .,,$(suffix $(basename $@))))
	echo "Letra: $(LETRA)"
	export LANG=C; \
	cat $< | \
	$(AWK) '{if ($$6 == "$(LETRA)") print $$0}' > $@; 

$(ltsdataTRAINfeats_valencia): $(BUILDLTS)/lts_scratch/valencia/lex.feats.train
	echo "Building $@"
	$(eval LETRA := $(subst .,,$(suffix $(basename $@))))
	echo "Letra: $(LETRA)"
	export LANG=C; \
	cat $< | \
	$(AWK) '{if ($$6 == "$(LETRA)") print $$0}' > $@; 

$(ltsNYTRAINfeats_valencia) $(ltsNYTESTfeats_valencia):
	echo "La ny castellana ja se sap quin so té i no cal entrenar-la"
	$(MKDIR_P) $(@D) && \
	touch $@

#$(ltstreeNY_valencia): $(ltsNYtreesrc_valencia)
#	echo "This is copied"
#	cp $< $@ || exit 1
	
$(BUILDLTS)/lts_scratch/valencia/lts.%.tree: $(BUILDLTS)/lts_scratch/valencia/ltsdataTRAIN.%.feats $(BUILDLTS)/lts_scratch/valencia/ltsdataTEST.%.feats
	echo "Wagon with $*"
	if [ $(laNY_valencia) = "$*" ]; then \
		cp $(ltsNYtreesrc_valencia) $@ || exit 1; \
	else \
	$(WAGON) -data $(BUILDLTS)/lts_scratch/valencia/ltsdataTRAIN.$*.feats -test $(BUILDLTS)/lts_scratch/valencia/ltsdataTEST.$*.feats \
          -desc $(BUILDLTS)/lts_scratch/valencia/ltsLTS.desc -stop 3 -output $(BUILDLTS)/lts_scratch/valencia/lts.$*.tree || exit 1; \
	fi \

$(BUILDLTS)/lts_scratch/valencia/lex_lts_rules.scm: $(BUILDLTS)/lts_scratch/valencia/ltsLTS.desc  $(ltsdataTRAINtree_valencia)
	cd $(BUILDLTS) && ./build_lts merge  "allowables-1.1-valencia.scm" "lts_scratch/valencia" || exit 1

$(BUILDLTS)/lts_scratch/valencia/lts_test.log: $(BUILDLTS)/lts_scratch/valencia/lex_lts_rules.scm $(BUILDLTS)/lts_scratch/valencia/lex.align $(BUILDLTS)/lts_scratch/valencia/lex.align.test $(BUILDLTS)/allowables-1.1-valencia.scm
	cd $(BUILDLTS) && ./build_lts test  "allowables-1.1-valencia.scm" "lts_scratch/valencia"


$(LTS_RULES_VALENCIA): $(BUILDLTS)/lts_scratch/valencia/lex_lts_rules.scm
	$(MKDIR_P) "$(BUILDLTS)/final" && \
	$(SED) -e 's/·/"·"/g' -e 's/lex_lts_rules/upc_catalan_lts_rules-valencia/g' "$(BUILDLTS)/lts_scratch/valencia/lex_lts_rules.scm" > "$(BUILDLTS)/final/upc_catalan_lts_rules-valencia.scm" && \
	echo "(provide 'upc_catalan_lts_rules-valencia)" >> "$(BUILDLTS)/final/upc_catalan_lts_rules-valencia.scm" && \
	$(MKDIR_P) $(@D) && \
	cp $(BUILDLTS)/final/upc_catalan_lts_rules-valencia.scm $@ || exit 1



TESTLTS += $(BUILDLTS)/lts_scratch/valencia/lts_test.log

CLEAN+= $(BUILDLTS)/lex_lts_rules-valencia.scm $(BUILDLTS)/lts_scratch/valencia/ $(BUILDLTS)/final
DISTCLEAN+= $(BUILDLTS)/build_lts

DISTSRCFILES+= $(ALLOWABLES) \
               $(BUILDLTS)/build_lts.in \
               $(BUILDLTS)/build_lts_rules \
               $(BUILDLTS)/createmultiphonemes \
               $(BUILDLTS)/fest2wagon \
               $(BUILDLTS)/lts.ny.tree \
               $(BUILDLTS)/purge_fest_dic \
               $(BUILDLTS)/readme \
               $(BUILDLTS)/reduce_dict \
               $(BUILDLTS)/traintest \
               $(BUILDLTS)/upc_catalan_lts_rules

##############################################################################
##############################################################################
##############################################################################
######################  CENTRAL: LTS   #######################################
##############################################################################
##############################################################################
##############################################################################
# Step 3:
$(BUILDLTS)/upclex_%.scm:  $(BUILDDATA)/festival/lib/dicts/upc/upclex_%.scm
	cp $< $@ || exit 1

$(REDUCED_LEXICON_CENTRAL): $(LTS_RULES_CENTRAL) $(addprefix $(BUILDLTS)/,$(notdir $(LIB_DICTS_UPCLEX)))
	echo "Reducing dictionary" && \
	cd $(BUILDLTS) && \
	$(MKDIR_P) "final_dict_reduced_central/dicttmp" && \
	cp $(addprefix ../../$(BUILDDATA)/festival/lib/dicts/upc/,$(notdir $(LIB_DICTS_UPCLEX))) "final_dict_reduced_central/dicttmp" && \
	cp "final/upc_catalan_lts_rules-central.scm" "final_dict_reduced_central/dicttmp/upc_catalan_lts_rules-central.scm" && \
	cp ../../$(BUILDDICT)/final/central/upcdict_catalan.out "final_dict_reduced_central/dicttmp/upcdict_catalan-1.0-central.out" && \
	$(FESTIVAL) --script "./reduce_dict" "final_dict_reduced_central/dicttmp/" "central" || exit 1
	$(MKDIR_P) $(@D) || exit 1
	cp $(BUILDLTS)/final_dict_reduced_central/dicttmp/upcdict_catalan.out  $@ || exit 1

CLEAN+= $(BUILDLTS)/final_dict_reduced_central $(addprefix $(BUILDLTS)/,$(notdir $(LIB_DICTS_UPCLEX)))


##############################################################################
##############################################################################
##############################################################################
######################  VALENCIA: LTS   ######################################
##############################################################################
##############################################################################
##############################################################################
# Step 3:
$(REDUCED_LEXICON_VALENCIA): $(LTS_RULES_VALENCIA) $(addprefix $(BUILDLTS)/,$(notdir $(LIB_DICTS_UPCLEX)))
	echo "Reducing dictionary" && \
	cd $(BUILDLTS) && \
	$(MKDIR_P) "final_dict_reduced_valencia/dicttmp" && \
	cp $(addprefix ../../$(BUILDDATA)/festival/lib/dicts/upc/,$(notdir $(LIB_DICTS_UPCLEX))) "final_dict_reduced_valencia/dicttmp" && \
	cp "final/upc_catalan_lts_rules-valencia.scm" "final_dict_reduced_valencia/dicttmp/upc_catalan_lts_rules-valencia.scm" && \
	cp ../../$(BUILDDICT)/final/valencia/upcdict_catalan.out "final_dict_reduced_valencia/dicttmp/upcdict_catalan-1.0-valencia.out" && \
	$(FESTIVAL) --script "./reduce_dict" "final_dict_reduced_valencia/dicttmp/" "valencia" || exit 1
	$(MKDIR_P) $(@D) || exit 1
	cp $(BUILDLTS)/final_dict_reduced_valencia/dicttmp/upcdict_catalan.out  $@ || exit 1

CLEAN+= $(BUILDLTS)/final_dict_reduced_valencia

#### END OF LEXICON AND LTS RULES #####
#######################################

############################
#### CREATE POS DATA #######
BUILDDATAPOS=$(BUILDDATA)/pos
CORPUSCA=$(BUILDDATAPOS)/corpus-ca
DICTAG=$(BUILDDATAPOS)/dictag.txt
VOCABULARY=$(BUILDDATAPOS)/voca.txt
CORPUSCASOURCES3LB = $(wildcard $(BUILDDATAPOS)/ancora-ca/3LB-CAT/*.xml)
CORPUSCASOURCESCATA = $(wildcard $(BUILDDATAPOS)/ancora-ca/CESS-CAT-A/*.xml)
CORPUSCASOURCESCATAAP =  $(wildcard $(BUILDDATAPOS)/ancora-ca/CESS-CAT-AAP/*.xml)
CORPUSCASOURCES = $(CORPUSCASOURCES3LB) $(CORPUSCASOURCESCATA) $(CORPUSCASOURCESCATAAP)
CORPUSCATXT = $(subst .xml,.txt,$(subst ancora-ca,work,$(CORPUSCASOURCES)))
BUILDDATAPOSWORK = $(BUILDDATAPOS)/work


$(BUILDDATAPOSWORK)/3LB-CAT/%.tbf.txt: $(BUILDDATAPOS)/ancora-ca/3LB-CAT/%.tbf.xml $(BUILDDATAPOS)/crea_corpusca.pl
	$(MKDIR_P) $(@D) || exit 1
	cat $< | $(PERL) $(BUILDDATAPOS)/crea_corpusca.pl > $@ || exit 1

$(BUILDDATAPOSWORK)/CESS-CAT-A/%.tbf.txt: $(BUILDDATAPOS)/ancora-ca/CESS-CAT-A/%.tbf.xml $(BUILDDATAPOS)/crea_corpusca.pl
	$(MKDIR_P) $(@D) || exit 1
	cat $< | $(PERL) $(BUILDDATAPOS)/crea_corpusca.pl > $@ || exit 1

$(BUILDDATAPOSWORK)/CESS-CAT-AAP/%.tbf.txt: $(BUILDDATAPOS)/ancora-ca/CESS-CAT-AAP/%.tbf.xml $(BUILDDATAPOS)/crea_corpusca.pl
	$(MKDIR_P) $(@D) || exit 1
	cat $< | $(PERL) $(BUILDDATAPOS)/crea_corpusca.pl > $@ || exit 1



$(CORPUSCA):  $(CORPUSCATXT)
	cat $(CORPUSCATXT) > $@

$(DICTAG): $(CORPUSCA) $(BUILDDATAPOS)/crea_dictag.pl
	$(PERL) $(BUILDDATAPOS)/crea_dictag.pl < $< > $@ || exit 1

CLEAN+= $(BUILDDATAPOSWORK) $(CORPUSCA) $(DICTAG)

#### END CREATE POS DATA ###
############################



#### BEGIN POSLEX TRAINING #####
################################
$(POSLEX): $(BUILDPOSLEX)/final/dicTAGGERcomplet.poslexR 
	cp "$(BUILDPOSLEX)/final/dicTAGGERcomplet.poslexR" $@

$(BUILDPOSLEX)/final/dicTAGGERcomplet.poslexR: $(BUILDPOSLEX)/final/dicTAGGERcomplet.poslexR_desord
	echo "MNCL" > $@ || exit 1
	export LANG=C; cat $< | sort | uniq >> $@ || exit 1

$(BUILDPOSLEX)/final/dicTAGGERcomplet.poslexR_desord $(VOCABULARY): $(BUILDPOSLEX)/simplifiedpos.orto $(DICTAG) $(BUILDPOSLEX)/02-FormatTaggerAmbAmpliació
	$(MKDIR_P) "$(BUILDPOSLEX)/final" || exit 1
	$(BUILDPOSLEX)/02-FormatTaggerAmbAmpliació "$(BUILDPOSLEX)/final/dicTAGGERcomplet.poslexR_desord" "$(DICTAG)" "$(BUILDPOSLEX)/simplifiedpos.orto" "$(BUILDPOSLEX)/log.txt" "$(VOCABULARY)" || exit 1

$(BUILDPOSLEX)/01-FormatDiccComplet.pl: $(BUILDFUNC)/functions_perl.pl
	touch $@

$(BUILDPOSLEX)/simplifiedpos.orto: $(BUILDDATA)/withtranscriptions/ca.orto $(BUILDPOSLEX)/01-FormatDiccComplet.pl
	$(MKDIR_P) $(@D)
	cat $(BUILDDATA)/withtranscriptions/ca.orto | \
	$(BUILDPOSLEX)/01-FormatDiccComplet.pl | \
	LANG=C sort | uniq >  $@ || exit 1

CLEAN+= $(BUILDPOSLEX)/simplifiedpos.orto $(BUILDPOSLEX)/final/ $(BUILDPOSLEX)/log.txt

DISTSRCFILES+= $(BUILDPOSLEX)/01-FormatDiccComplet.pl \
               $(BUILDPOSLEX)/02-FormatTaggerAmbAmpliació

#### END POSLEX TRAINING ####
#############################

#### BEGIN NGRAM ####
#####################

$(BUILDNGRAM)/01-ReducTAGCorpus.pl: $(BUILDFUNC)/functions_perl.pl
	touch $@

$(BUILDNGRAM)/work/corpus-ca-reduitsTAGS.out: $(CORPUSCA) $(BUILDNGRAM)/01-ReducTAGCorpus.pl
	$(MKDIR_P) $(BUILDNGRAM)/work && \
	$(PERL) $(BUILDNGRAM)/01-ReducTAGCorpus.pl < $< > $@ || exit 1

$(BUILDNGRAM)/work/corpus-ca.out $(BUILDNGRAM)/work/corpus-ca-frase.out: $(BUILDNGRAM)/work/corpus-ca-reduitsTAGS.out $(VOCABULARY) $(BUILDNGRAM)/02-FormatCorpus
	cd $(BUILDNGRAM) && \
	./02-FormatCorpus || exit 1

$(BUILDNGRAM)/work/corpus.train  $(BUILDNGRAM)/work/corpus.test $(BUILDNGRAM)/work/corpus_frases.train $(BUILDNGRAM)/work/corpus_frases.test:  $(BUILDNGRAM)/work/corpus-ca-frase.out $(BUILDNGRAM)/work/corpus-ca.out $(VOCABULARY) $(BUILDNGRAM)/03-SplitterCorpus.pl
	cd $(BUILDNGRAM) && \
	$(PERL) ./03-SplitterCorpus.pl || exit 1

$(BUILDNGRAM)/final/upc_freeling_catalan.tri.ngrambin: $(BUILDNGRAM)/work/corpus.train $(VOCABULARY) $(BUILDNGRAM)/04-ngrambuild.sh
	$(MKDIR_P) $(@D)
	export NGRAM_BUILD="$(NGRAM_BUILD)" ; \
	$(BUILDNGRAM)/04-ngrambuild.sh $(BUILDNGRAM)/work/corpus.train $(VOCABULARY) $@ || exit 1	

festival/lib/dicts/upc/upc_freeling_catalan.tri.ngrambin: $(BUILDNGRAM)/final/upc_freeling_catalan.tri.ngrambin
	$(MKDIR_P) $(@D) && \
	cp $< $@ || exit 1

CLEAN+= $(BUILDNGRAM)/work $(BUILDNGRAM)/final $(VOCABULARY)

DISTSRCFILES+= $(BUILDNGRAM)/01-ReducTAGCorpus.pl \
               $(BUILDNGRAM)/02-FormatCorpus \
               $(BUILDNGRAM)/03-SplitterCorpus.pl \
               $(BUILDNGRAM)/04-ngrambuild.sh


#### END NGRAM ####
###################

# Data files for distribution:
DISTSRCFILES+= $(BUILDFUNC)/functions_perl.pl \
               $(BUILDDATA)/concatenate.sh \
               $(BUILDDATA)/transcribe.sh \
               $(BUILDDATA)/unicode2sampa.sh \
               $(BUILDDATA)/festival/lib/dicts/upc/upclex_catalan.scm \
               $(BUILDDATA)/festival/lib/dicts/upc/upclex_central.scm \
               $(BUILDDATA)/festival/lib/dicts/upc/upclex_valencia.scm \
               $(BUILDDATA)/festival/lib/dicts/upc/upclex_syl.scm \
               $(BUILDDATA)/festival/lib/dicts/upc/upclex_postlex.scm \
               $(BUILDDATA)/festival/lib/languages/language_catalan.scm \
               $(BUILDDATA)/festival/lib/languages.scm \
               $(BUILDDATA)/festival/lib/upc_catalan \
               $(BUILDDATA)/festvox.orig \
               $(BUILDDATA)/from_freeling \
               $(BUILDDATA)/otherdic \
               $(BUILDDATA)/pos/crea_dictag.pl \
               $(BUILDDATA)/pos/crea_corpusca.pl \
               $(BUILDDATA)/pos/ancora-ca \
               $(BUILDDATA)/withtranscriptions/ca.orto \
               $(BUILDDATA)/withtranscriptions/ca-central.phon \
               $(BUILDDATA)/withtranscriptions/ca-valencia.phon

# Create directories, copy files and then:
# If we have a libdir/languages directory, 
# copy the language_catalan.scm inside.
# If we have a standard 2.1 installation
# edit languages.scm using sed
install:
	$(MKDIR_P) "$(FESTDATADIR)/dicts/upc" && \
	$(MKDIR_P) "$(FESTDATADIR)/upc_catalan" && \
	$(INSTALL_DATA) festival/lib/dicts/upc/* "$(FESTDATADIR)/dicts/upc" && \
	$(INSTALL_DATA) festival/lib/upc_catalan/* "$(FESTDATADIR)/upc_catalan" && \
	if test "$(LANGUAGESSCMVERSION)" = "directory" ; then \
	    $(MKDIR_P) "$(FESTDATADIR)/languages" && \
	    $(INSTALL_DATA) festival/lib/languages/language_catalan.scm "$(FESTDATADIR)/languages" ; \
	fi ; \
	if [ "$(LANGUAGESSCMVERSION)" = "2.1" ] ; then \
	     $(GREP) "catalan" "$(LANGUAGESSCMFILE)" > /dev/null && DOINSTALL="no" ||  DOINSTALL="yes"; \
	     if [ "$$DOINSTALL" = "yes" ]; then \
	        mv $(LANGUAGESSCMFILE) $(LANGUAGESSCMFILE).orig || exit 1; \
	     else \
	         if [ -f "$(LANGUAGESSCMFILE).orig" ]; then \
	             rm -f "$(LANGUAGESSCMFILE)"; \
		     DOINSTALL="yes"; \
		 else \
		     DOINSTALL="no"; \
		 fi ; \
	     fi ; \
	     if [ "$$DOINSTALL" = "yes" ]; then \
	         $(SED) "s|(define (select_language language)|\n\n;; Modification: Added language_catalan\n(define (language_catalan)\n\"(language_catalan)\nSet up language parameters for Catalan.\"\n\n   (defvar catalan-path (path-append (if (boundp 'datadir) datadir libdir) \"upc_catalan/\"))\n   (if (not (member_string catalan-path load-path))\n                      (set! load-path (cons catalan-path load-path)))(set! female1 (lambda () (voice_upc_ca_ona_hts)))\n (set! male1 ( lambda () (voice_upc_ca_pau_hts)))\n (female1)\n (Param.set 'Language 'catalan)\n)\n\n(define (select_language language)|g" $(LANGUAGESSCMFILE).orig | \
	         $(SED) "s/   ((equal? language 'klingon)/   ((equal? language 'catalan)\n    (language_catalan))\n   ((equal? language 'klingon)/g"  > $(LANGUAGESSCMFILE) ; \
	     fi; \
	fi ;

dist-bin: clean
	rm -rf "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)"
	mkdir -p "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)"
	for thing in $(DISTBINFILES);do \
	       thingdir=$$(dirname "$$thing") && \
	       thingname=$$(basename "$$thing") && \
	       $(MKDIR_P) "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)/$$thingdir" && \
	       cp -r "$$thing" "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)/$$thingdir" || exit 1; \
	done
	tar czf ../$(PACKAGE_TARNAME)-$(PACKAGE_VERSION).tgz $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)
	rm -rf "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)"

dist-src: clean
	rm -rf "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src"
	mkdir -p "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src"
	for thing in $(DISTSRCFILES);do \
	       thingdir=$$(dirname "$$thing") && \
	       thingname=$$(basename "$$thing") && \
	       $(MKDIR_P) "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src/$$thingdir" && \
	       cp -r "$$thing" "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src/$$thingdir" || exit 1; \
	done
	tar czf ../$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src.tgz $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src
	rm -rf "$(PACKAGE_TARNAME)-$(PACKAGE_VERSION)-src"

test/%.txt.out test/%.txt.out2  test/%.txt.diff  test/%.txt.diff2: test/%.txt test/%.txt.ref test/%.txt.ref2 
	$(FESTIVAL) --script test/print_utterance "test" "$<" > "$@" 2> "$<".out2 || exit 1
	diff "$<".ref "$@" > "$<".diff ; diff "$<".ref2 "$@2" > "$<".diff2; \
	test -s "$<".diff -a -s "$<".diff2 && (echo "TEST $< FAILED" ; exit 1) || echo "TEST $< OK"

TEST = $(TESTTOKENIZEROUT) $(TESTLTS)


test-lts: $(TESTLTS)

test-tokenizer: $(TESTTOKENIZEROUT)

test: test-tokenizer test-lts

clean:
	rm -rf $(CLEAN)

distclean: clean
	rm -rf $(DISTCLEAN)


