	#!/bin/bash

# Effectue un remplacement dans un texte à partir d'un dictionnaire de transformations
# Paramètres (fichiers) :
# $1 : le texte
# $2 : dictionnaire des transformations
# Si le dictionnaire est une simple liste de tmp-mots, ces tmp-mots seront supprimés dans le texte final
#   (utile pour supprimer les tmp-mots trop fréquent d'une langue).
# Le résultat est dans le fichier 'new'

# Ex.1 : Pour supprimer les tmp-mots courants
#erg-reduc notes-phrases-filtre.txt tmp-mots-frq-100.txt
#txt=notes-phrases-filtre.txt
#dic=tmp-mots-frq-100.txt

# Ex.2 : Pour effectuer la lemmatisation (séparation : tabulation !)
# erg-reduc notes-reduit dic-lemmes-fr.csv
# (par exemple après la suppression des tmp-mots courants ci-dessous suivi d'un cp new notes-reduit)
#txt=notes-reduit
#dic=dic-lemmes-fr.csv

# Pour entrer les paramètres en ligne de commande
txt=$1
dic=$2
log=$3

if [ "$dic" = "" ]
then
	echo "\$1 : texte ; \$2 : dictionnaire des transformations ; \$3 : log ou rien"
	exit
fi

# cat notes-phrases.txt | sed -e "s/[()0123456789\/:–.\"’,\->\/]/ /g" -e "s/  / /g" -e "s/^ //g" -e "s/ $//g" > notes-phrases-filtre.txt

cat $txt | erg-mots | uniq -i > tmp-mots

tmpmots=tmp-mots

cat $txt |erg-minuscules > new

if [ -n "$log" ]
then
	echo "# Journal  activé !" > log
	echo "# Journal  activé !"
fi
touch not_delete

cat tmp-mots | while read origin
do
	# test pour savoir si le mot est commun aux deux listes (à supprimer ou à remplacer)
	rule=$(cat "$dic" | grep -iw "$origin" | head -n 1 )
	if [ -n "$rule" ]
	then
		if [ -n "$log" ]
			then
				echo -e "\n" RULE $rule
				echo ORIGIN $origin
		fi
		# test2 : rule contient-il une tabulation ; s'agit-il d'un mot à remplacer ou d'un mot à supprimer ? Test un peu lourd...	
		# séparation : TABULATION
		test="$(echo -e "$rule" | sed -e "s/\t/@/g" | grep "@")"
		#echo [TEST]$test
		if [ "$test" != "" ]
		then
			target="$(echo -e "$rule" | cut -f2 )"
			# mot à remplacer : transformation
			if [ "$target" != "$origin" ]
			then
				if [ -n "$log" ]
				then
					echo Transformation : $origin =\> $target
					echo Transformation : $origin =\> $target >> log
				fi
				# AWK
				# Échoue avec awk si le mot se termine par une virgule : 
				# cat new | awk -v origin="$origin" -v target="$target" '{for(i=1;i<=NF;i++)if($i==origin){$i=target}}1' > new2
				# SEC
				# la ligne suivante échoue avec les noms composés :
				# cat new | sed -e "s/\<$origin\>/$target/gI" > new2
				# Bug avec deux fois le même mot :
				# cat new | sed "s/\([^-]\|^\)\<$origin\>\([^-]\|$\)/\1$target\2/gI"  > new2
				# PYTHON :
				cat new | erg-replace.perl "$origin" "$target" > new2
				# PERL
# 				export origin=$origin; export target=$target; 
# 				cat new | \
# 				perl -pe 's/(^|[^-])\b$ENV{"origin"}\b(?=$|[^-])/$1$ENV{"target"}/ig' > new2
				# cat new#!/bin > new2
				cat new2 > new
			fi
		else
			# mot à supprimer : suppression
			if [ -n "$log" ]
			then
				echo  -e "SUPPR($origin) "
				echo Suppression de : $origin >> log
			fi
			# suppression du mot en question (cf. notes en transformation)
			# cat new | awk -v origin=$origin '{for(i=1;i<=NF;i++)if($i==origin){$i=""}}1' > new2
			# cat new | sed -e "s/\<$origin\>//gI" > new2
			#cat new | sed "s/\([^-]\|^\)\<$origin\>\([^-]\|$\)/\1\2/gI" > new2
			# cat new |grep -vw $origin > new2
			# PERL
# 			export origin=$origin; export target=""; 
# 			cat new | \
# 			perl -pe 's/(^|[^-])\b$ENV{"origin"}\b(?=$|[^-])/$1$ENV{"target"}/ig' > new2
			# cat new#!/bin > new2
			# cat new | perl -pe '$this="the"; $that="a"; s/(^|[^-])\b$origin\b(?=$|[^-])/$1/ig' > new2
			# PYTHON :
			cat new | erg-replace.perl "$origin" "" > new2
			cat new2 > new			
		fi
	fi
done

# nettoyage... 
cat new | erg-nettoie > new_tmp
cat new_tmp > new

if [ -n "$log" ]
then
	echo FIN
  echo "$(wc -l $tmpmots |cut -f1 -d " ") lignes au départ de $tmpmots"
  echo "$(wc -l new |cut -f1 -d " ") lignes à l'arrivée de new"
  echo "FIN"
#	erg-mots new
else
	cat new
fi