1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "table_hash.h"
#define TAILLEHASH 307
int main(void)
{
// Declaration des variables
int a;
FILE *F; // Descripteur du fichier texte
char mot[100]; // variable utilisée pour stocker le mot en cours de lecture
int i; // variable utilisée lors du parcours de la TableHash
unsigned int cle,pt; // variables pour stocker les cles du mot en cours et la clé du caractère '.'
char *temp; // variable pour stocker le mot en cours de lecture
char c; // ??
Liste **TableHash; // tableau de liste
TableHash = (Liste **) malloc (TAILLEHASH * sizeof(Liste *)); // allocation en memoire du tableau
for(i=0;i<TAILLEHASH;++i) // Initialisation des
TableHash[i] = NULL; // pointeurs contenus dans le tableau
pt=hash_cle("."); // calcul de la cle equivalent à un point
printf("debut du programme \n----------------------------------\n");
F=fopen("C:\\Users\\text.txt","r"); // ouverture du fichier texte
if(F!= NULL) // si le fichier existe
{
while(fscanf(F,"%s",mot)==1) // boucle de lecture du fichier
{
temp = (char*)mot; // recuperation du mot lu
cle = hash_cle(temp); // calcul de la cle equivalente
if ( (cle!=pt ) && (!ChercherMotDansTableHash(TableHash,temp))) // si le mot n'est pas un point et qu'il n'est pas deja stocke
TableHash[cle] = InsertionEnTete(TableHash[cle],temp); // on l'insere en debut du tableau
}
fclose(F ); // on ferme le descripteur de fichier
F=fopen("C:\\Users\\text.txt","r"); // on rouvre le descripteur de fichier pour revenir au debut
PosLigne(F,TableHash); // recherche des coordonnées des diferents mots extraits
fclose(F ); // on ferme le descripteur de fichier
//filtrage
printf("\nla liste non filtree :\n----------------------------------\n");
AfficherTableHash(TableHash); // affichage de la table NON FILTREE
printf("\nla liste filtree (2 occurrences au moins) :\n----------------------------------\n");
FiltreListe(TableHash,2); // filtrage de la table
AfficherTableHash(TableHash); // affichage de la table FILTREE
//géneration des 2seq
Liste *seq=NULL;
Generer2seq(TableHash, 2, &seq);
printf("\nla liste des 2seq :\n----------------------------------\n");
AfficherListe(seq);
//printf("Chercher mot : ");
//scanf("%s",mot);
//if(cle = ChercherMotDansTableHash(TableHash,mot))
// printf("%s existant dans le conteneur %u\n",mot,hash_cle(mot));
//else
// printf("%s inexistant dans la table de hash\n");
//scanf("%c",c);
// elagage des séquence
//AfficherTableHash(TableHash);
elaguer2seq(seq, TableHash);
//printf("%i", a);
}
//}
else printf("Le fichier texte n'existe pas");
system("pause");
return 0;
}
void ParcourirElementTableHash(Liste **TableHash,char *mot){
int cle;
Liste *p;
cle = hash_cle(mot);
for(p=TableHash[0];p!=NULL;p=p->suivant)
if(strcmp(mot,p->mot) == 0){
printf("%s : ",mot);
AfficherCoordonnees(p->c);
}
}
Liste *InsertionEnTete(Liste *L,char *mot){
Liste *nouveau;
nouveau = (Liste *) malloc (sizeof(Liste));
strcpy(nouveau->mot,mot);
nouveau->suivant = L;
nouveau->c = NULL;
return nouveau;
}
Coordonnees *InsertionEnTeteCoordonnee(Coordonnees *C,int nl ,int pos){
Coordonnees *nouveau;
nouveau = (Coordonnees *) malloc (sizeof(Coordonnees));
nouveau->pos = pos;
nouveau->nl = nl;
nouveau->suivant = C;
return nouveau;
}
void AfficherListe(Liste *L){
Liste *p;
for(p=L;p!=NULL;p=p->suivant){
AfficheMot(p->mot);
AfficherCoordonnees(p->c);
}
}
void AfficherCoordonnees(Coordonnees *C){
Coordonnees *p;
for(p=C;p!=NULL;p=p->suivant)
printf(" (%d,%d) ",p->nl,p->pos);
printf("\n");
}
void AfficheMot(char *mot){
char *espace;
espace =" ";
printf("%s",mot );
printf("%s",espace);
}
unsigned int hash_cle(char *mot){
unsigned int val = 0;
for(;*mot!='\0';++mot)
val = *mot + 31 * val;
return val % TAILLEHASH;
}
void AfficherTableHash(Liste **TableHash){
int i;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i] != NULL){
printf("Conteneur %d \n",i);
AfficherListe(TableHash[i]);
printf("----------------------------------\n");
}
}
unsigned int ChercherMotDansTableHash(Liste **TableHash,char *mot){
Liste *p;
unsigned int cle;
cle = hash_cle(mot);
for(p=TableHash[cle];p!=NULL;p=p->suivant)
if(strcmp(p->mot,mot)==0)
return 1;
return 0;
}
void FiltreListe(Liste **TableHash, int seuil)
{
Liste *p ,*last;
Coordonnees *c;
int i,supp=0,nb=0;
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i] != NULL){
nb=0;
last=TableHash[i];
for(p=TableHash[i];p!=NULL;p=p->suivant){
supp=0;
nb++;
for(c=p->c;c!=NULL;c=c->suivant)
supp++;
if( supp <seuil){ // mot n'est pas frequent
if (last==p) // cas du première mot
{
TableHash[i]=p->suivant;
last=p->suivant;
}
else
last->suivant=p->suivant;
nb--;
}else
last=p;
}
if (nb==0)
TableHash[i] = NULL;
}
}
void Generer2seq(Liste **TableHash, int seuil, Liste **seq){
Liste *p1, *p2, *ss,*t;
Coordonnees *c1,*c2;
int i,j,supp1=0,supp2=0,nb=0;
char s1[50], s2[50],*t1,*t2;
char *stmp1,*stmp2;
ss=*seq;
printf("\nconstruction des 2seq \n---------------\n");
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i] != NULL)
for(p1=TableHash[i];p1!=NULL;p1=p1->suivant)
for(j=i;j<TAILLEHASH;++j)
if(TableHash[j] != NULL){
if (j==i)
t=p1;
else
t=TableHash[j];
for(p2=t;p2!=NULL;p2=p2->suivant){
if (strcmp(p1->mot,p2->mot)!=0){ // si 2 mots differents trouvés
Liste *tmp1=NULL, *tmp2=NULL;
strcpy(s1,p1->mot);
strcpy(s2,p2->mot);
stmp1 = strcat(s1," ");
stmp2 = strcat(s2," ");
t1=strcat(stmp1,p2->mot);
t2=strcat(stmp2,p1->mot);
tmp1=InsertionEnTete(tmp1,stmp1);
tmp2=InsertionEnTete(tmp2,stmp2);
printf("creation TMP1 et TMP2 : %s ,%s\n",stmp1,stmp2);
supp1=0;supp2=0;
for(c1=p1->c;c1!=NULL;c1=c1->suivant) // calcule de supp
for(c2=p2->c;c2!=NULL;c2=c2->suivant){
if (c1->nl == c2->nl){
if(c1->pos < c2->pos){
tmp1->c=InsertionEnTeteCoordonnee(tmp1->c,c1->nl ,c2->pos);
supp1++;
}
else{
tmp2->c=InsertionEnTeteCoordonnee(tmp2->c,c1->nl ,c1->pos);
supp2++;
}
}
} // fin calcul supp
if(supp1>=seuil){
//printf("%d ",supp1);
if (*seq==NULL)
*seq=tmp1;
else
ss->suivant=tmp1;
ss=tmp1;
//printf("%s : OK1\n",tmp1->mot);
}
if(supp2>=seuil){
//printf("%d ",supp2);
if (*seq==NULL)
*seq=tmp2;
else
ss->suivant=tmp2;
ss=tmp2;
//printf("%s : OK2\n",tmp2->mot);
}
} // fin de traitement des mot
//printf("fin");
}
}
printf("////fin calcule 2seq\n");
}
int chercher_mot(const char *phrase,const char *mot){
int last;
const size_t size = strlen(mot);
char *find = strstr(phrase,mot);
char *p;
while(find){
// le mot a été trouvé, mais est ce le dernier mot ?
if(strlen(find) == size) return 1;
else{ // le mot est peut être en dernière position mais suivi de caractères autre que des mots (ponctuation...)
last = 1; // 1 si mot est bien le dernier dans la séquence, 0 sinon
for(p = &(find[size]);*p != '\0';++p){
if (isalpha(*p)){ // on a trouvé un caractère alphanumérique derrière donc ça ne va pas
last = 0;
break;
}
}
// le mot n'est pas suivi de caractère alphanumérique donc c'est bon
if (last) return 1;
}
find = strstr(&(find[1]),mot);
}
return 0;
}
void elaguer2seq(Liste *L, Liste **TableHash)
{
Liste *p;
Liste *q;
char *mot;
int i=0;
int a;
Liste *maillon_temp;
Liste *last;
printf(" seq : %s\n",L->mot);
//printf("TableHash : %s\n",TableHash[1]->mot);
last=TableHash[i];
for(p=TableHash[i];p!=NULL;p=p->suivant){
//if(TableHash[i] != NULL)
//printf("TableHash : %s\n",TableHash[i]->mot);
if (chercher_mot(TableHash[i]->mot,p->mot)==0)
printf("%s","condition vérifiée");
TableHash[i]=p->suivant;
last=p->suivant;
//maillon_temp=TableHash[i];
//TableHash[i]=TableHash[i]->suivant ;
//free(maillon_temp);
// supprimerMot(p->mot);
AfficherTableHash(TableHash);
//printf("TableHash : %s\n",TableHash[i]->mot);
}
//AfficherTableHash(TableHash);
// for(p=L;p!=NULL;p=p->suivant){
// AfficherTableHash(TableHash);
// for(q=TableHash ;q!=NULL;q=q->suivant){
// printf ("%s", "aaaaaaaaaaaaaaaaaaaaaaa");
// printf ("%s", q->mot);
//
// }
//}
}
void PosLigne(FILE *F,Liste **TableHash){
char s[50];
int nl,pos,i;
Liste *p;
nl=pos=1;
while(fscanf(F,"%s",s)==1){
for(i=0;i<TAILLEHASH;++i)
if(TableHash[i]!=NULL)
for(p=TableHash[i];p!=NULL;p=p->suivant)
if(strcmp(p->mot,s)==0)
p->c=InsertionEnTeteCoordonnee(p->c,nl ,pos);
if(fgetc(F)=='\n'){
++nl;
pos=0;
}
++pos;
}
printf("\n");
} |
Partager