#!/bin/bash
# Réduit un texte à une classes de mots
# FIXME : obsolète ! utiliser erg-classes2

# prépare le dossier de travail 'classes'
if [ -d classes ]
then
	mv classes classes.$(date +%F.%H.%M.%S)
fi
mkdir classes

cd classes

# param1 : texte à classer
txt="../$1"
if [ -d $txt ]
then
	txt=/home/hulin/Desktop/analyse_biophar/notes-reduit
fi

cp $txt ../cp_txt -f

# param2 : journalisation
log="$2"
if [ -n log ]
then
	if [ -f log ]
	then
		rm log
	fi
fi

# boucle sur le texte à découper
i=0
while [ "$i" -eq "0" ]
do

	# découpage en mots avec nb d'occurence (bug graph)
	cat ../cp_txt | sed 's/ /\n/g' | sed 's/.*/\L&/' | sed -n '/[:graph:]/p'| sed s/[\(\)\.\:\;\,\"]//g | sort | uniq  -c | sort -nr  > mots_occ-1
	cat mots_occ-1 | grep -v 1 > mots_occ


	# mots sans nombre d'occurence
	cat mots_occ | sed -n '/[:graph:]/p' | sed -e "s/[0123456789 ]//g" > mots

	test="$(cat mots)"
	if [ -n "$test" ]
	then

	
# découpage en lignes (bug graph)
	cat ../cp_txt  | sed 's/\./\n/g' | sed 's/.*/\L&/' | sed -n '/[:graph:]/p' | sed s/[\(\)\.\:\;\,\"]//g > lignes

	line="$(cat mots | head -n 1)"

	if [ -n log ]
	then
		echo "LINE =" $line  >> erg-classes.log
	fi

	cat lignes | grep -wi "$line" > voc

# Erreur, suppression de "c", je, etc... avec :graph: !
#	cat voc | sed 's/ /\n/g' | sed 's/.*/\L&/' | sed -n '/[:graph:]/p' | sed s/[\(\)\.\:\;\,\"]//g | sort | uniq  -c | sort -nr > $line-mots_occ

	cat voc | sed 's/ /\n/g' | sed 's/.*/\L&/' | grep -v "^$" | sed s/[\(\)\.\:\;\,\"]//g | sort | uniq  -c | sort -nr > $line-mots_occ
	cat $line-mots_occ | sed -e "s/[0123456789 ]//g" > $line-mots
	sh ~/bin/erg-reduc ../cp_txt $line-mots
	cat new | sed 's/.*/\L&/' > ../cp_txt
	wc -l $line-mots_occ

	if [ -n log ]
	then
		echo -e "\n$(wc -l $line-mots_occ)" >> erg-classes.log
		cat $line-mots_occ >> erg-classes.log
	fi
	else
		# exit
		i=1

	fi
done

# Nettoyage
for i in *
do
	rm "$(echo $i |grep  -v occ)"
done


echo "$(date)" > total
# fusion du résultat dans le fichier total
for i in *
do
	if [ $i != "total" ]
	then
		echo -e "\n----" >> total
		echo -" "\n---" >> total-1
		echo $i >> total
		echo $i >> total-1
		cat $i  | grep -wv "1" >> total-1
                cat $i   >> total
	fi
done
cat total
cd ..
