#
# File      : CompteCarac.pl
# Date      : 2003.06.18
# Author(s) : Fabien JOBIN
# Version   : 1.0
#
# Copyright : GNU GPL (http://www.gnu.org/copyleft/gpl.html)
#

#
# This library 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; either
# version 2 of the License, or (at your option) any later version.
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use strict;

# Fonction servant retournant vrai si un caractère
# est un caractère valide pour un digramme
sub CaractereValide
{
  my ($car) = @_;

  # Liste des caratères valides pour un digramme
  my @listcarac = ( "a".."z","A".."Z",",","." );

  foreach my $val (@listcarac) {
    if( $val eq $car){
      return 1;
    }
  }
  return 0;
}




my $ligne;
my $nbligne=0;                 # Nb de ligne du fichier
my $nbcarac = 0;               # Nb total de caractère du fichier
my $nbcarprisencompte=0;       # Nb total de caractère valide du fichier

my $FIC;

# Table de hashage pour compter chaque digramme
my %digramme = ();
my $nbdigramme = 0;

my $carcour;
my $carprec;

# Table de Hashage pour les occurences des caractères valides
# espace, return ne sont pas pris en compte par exemple
my %caractere = ( "a" => 0,
                  "b" => 0,
                  "c" => 0,
                  "d" => 0,
                  "e" => 0,
                  "f" => 0,
                  "g" => 0,
                  "h" => 0,
                  "i" => 0,
                  "j" => 0,
                  "k" => 0,
                  "l" => 0,
                  "m" => 0,
                  "n" => 0,
                  "o" => 0,
                  "p" => 0,
                  "q" => 0,
                  "r" => 0,
                  "s" => 0,
                  "t" => 0,
                  "u" => 0,
                  "v" => 0,
                  "w" => 0,
                  "x" => 0,
                  "y" => 0,
                  "z" => 0,
                  "A" => 0,
                  "B" => 0,
                  "C" => 0,
                  "D" => 0,
                  "E" => 0,
                  "F" => 0,
                  "G" => 0,
                  "H" => 0,
                  "I" => 0,
                  "J" => 0,
                  "K" => 0,
                  "L" => 0,
                  "M" => 0,
                  "N" => 0,
                  "O" => 0,
                  "P" => 0,
                  "Q" => 0,
                  "R" => 0,
                  "S" => 0,
                  "T" => 0,
                  "U" => 0,
                  "V" => 0,
                  "W" => 0,
                  "X" => 0,
                  "Y" => 0,
                  "Z" => 0,
                  "0" => 0,
                  "1" => 0,
                  "2" => 0,
                  "3" => 0,
                  "4" => 0,
                  "5" => 0,
                  "6" => 0,
                  "7" => 0,
                  "8" => 0,
                  "9" => 0,
                  "à" => 0,
                  "é" => 0,
                  "è" => 0,
                  "ê" => 0,
                  "ë" => 0,
                  "ï" => 0,
                  "î" => 0,
                  "ù" => 0,
                  "À" => 0,
                  "É" => 0,
                  "È" => 0,
                  "Ê" => 0,
                  "Ë" => 0,
                  "Î" => 0,
                  "Ï" => 0,
                  "Ù" => 0,
                  "," => 0,
                  "?" => 0,
                  ";" => 0,
                  "." => 0,
                  ":" => 0,
                  "!" => 0,
                  "<" => 0,
                  ">" => 0,
                  "+" => 0,
                  "-" => 0,
                  "*" => 0,
                  "/" => 0,
                  "§" => 0,
                  "%" => 0,
                  "µ" => 0,
                  "\$" => 0,
                  "£" => 0,
                  "&" => 0,
                  "~" => 0,
                  "#" => 0,
                  "{" => 0,
                  "}" => 0,
                  "(" => 0,
                  ")" => 0,
                  "[" => 0,
                  "]" => 0,
                  "|" => 0,
                  "\\" => 0,
                  "ç" => 0,
                  "Ç" => 0,
                  "@" => 0,
                  "=" => 0,
                  "\"" => 0,
                  "\'" => 0 );


die("Usage: $0 <fichier texte>\n") if( !defined($ARGV[0]) );

die("Le fichier $ARGV[0] n'existe pas\n") if( ! -e $ARGV[0] );

open( FIC, $ARGV[0]) or die("open: $!");

while( defined( $ligne=<FIC> ) ){
  $nbcarac += length( $ligne );
  $nbligne++;
  for( my $i=0; $i<length( $ligne ); $i++){
    $carcour = substr($ligne, $i, 1);

    # Calcul des occurences des caractères
    if( exists( $caractere{$carcour} ) ){
      $caractere{$carcour}++;
      $nbcarprisencompte++;
    }

    # Calcul des occurences des digrammes
    if( defined($carprec) ){
      if( CaractereValide($carcour) && CaractereValide($carprec) ){
        my $dicour=$carprec.$carcour;
        if( exists( $digramme{$dicour}) ){
          $digramme{$dicour}++;
        }
        else{
          $digramme{$dicour} = 1;
        }
        $nbdigramme++;
      }
    }

    $carprec = $carcour;

  }
}

close( FIC );

print "Nb carac: $nbcarac\n";
print "Nb ligne: $nbligne\n";
print "Nb car  : $nbcarprisencompte\n";
print "---------- Caractères Simple ----------\n";

#
# Affichage des caractères et leur nombre d'occurence
# trié par ordre décoissant du nombre d'occurence
#
foreach my $k ( sort{$caractere{$b} <=> $caractere{$a}} (keys(%caractere) ))
{
  my $pourcent = sprintf("%.3f", 100 * $caractere{$k} / $nbcarprisencompte);

#  print "Nb $k: $caractere{$k} \t ($pourcent)\n";
  print "$k \t $caractere{$k} \t $pourcent\n";
}


print "---------- Digrammes ----------\n";

#
# Affichage des digrammes et leur nombre d'occurence
# trié par ordre décoissant du nombre d'occurence
#
foreach my $k ( sort{$digramme{$b} <=> $digramme{$a}} (keys(%digramme)) )
#foreach my $k (sort{$b cmp $a} (keys(%digramme)) )
{
  my $pourcent = sprintf("%.3f", 100 * $digramme{$k} / $nbdigramme);
  print "$k \t $digramme{$k} \t $pourcent\n";
}

