lia_liblex.c 16.8 KB
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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
/* Managing a lexicon with IDs  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

/* ................................................................ */

#define False	0
#define True	1

#define TailleLigne	4000

#define LIA_MAX_TAILLE_MESSAGE	4000
#define VERBOSE	0

int             LIA_AVL_NB_NODE;

void 
LIA_AVL_ERREUR(char *ch1, char *ch2)
{
	fprintf(stderr, "LIA_AVL_ERREUR : %s %s\n", ch1, ch2);
	exit(0);
}

/* ................................................................ */


typedef struct {
	char           *key_string;
	int             code;
}               type_info;

type_info      *
new_type_info(char *key, int code)
{
	type_info      *pt;
	pt = (type_info *) malloc(sizeof(type_info));
	if (key)
		pt->key_string = (char*)strdup(key);
	else
		pt->key_string = NULL;
	pt->code = code;
	return pt;
}

void 
free_type_info(type_info * pt)
{
	if (pt) {
		if (pt->key_string)
			free(pt->key_string);
		free(pt);
	}
}

void 
print_type_info(type_info * info)
{
	printf("%s\t%d\n", info->key_string, info->code);
}

/* ................................................................ */

/* declaration du type noeud des arbres AVL */

typedef struct lia_avl_type {
	signed char     dq;
	type_info      *info;
	struct lia_avl_type *fg, *fd;
}              *lia_avl_t;

/* ................................................................ */

/* les rotations */

/*
 * parametres : 1- lia_avl_t = racine de l'arbre
 */
/* retour : True=rotation effectuee / False=rotation impossible */
int             lia_rotation_avl_droite(lia_avl_t);
int             lia_rotation_avl_gauche(lia_avl_t);
int             lia_rotation_avl_gauche_droite(lia_avl_t);
int             lia_rotation_avl_droite_gauche(lia_avl_t);

/* ................................................................ */

/* l'insertion d'un element */

/*
 * parametres : 1- lia_avl_t = racine de l'arbre 2- char * = info a ajouter a
 * l'arbre 3- int = booleen (True ou False) avec True=ajout avec
 * reequilibrage 4- char * = chaine de caractere recevant la trace de l'ajout
 */
/* retour : lia_avl_t = racine de l'arbre modifie */
lia_avl_t       lia_ajoute_element_avl(lia_avl_t, type_info *, int, char *);

/* ................................................................ */

/* la liberation de la place memoire de l'arbre */

/*
 * parametres : 1- lia_avl_t = racine de l'arbre
 */
void            lia_libere_avl(lia_avl_t);

/* ................................................................ */

/* la recherche d'un element */

/*
 * parametres : 1- lia_avl_t = racine de l'arbre 2- char * = info a
 * rechercher dans l'arbre 3- int * = nb d'occurence de la chaine
 */
/* retour : the node containing the info or NULL */
lia_avl_t       lia_recherche_avl(lia_avl_t, type_info *);

/* ................................................................ */

/* l'affichage de l'arbre */

/*
 * parametres : 1- lia_avl_t = racine de l'arbre a afficher
 */
void            lia_affiche_avl(lia_avl_t);

void            lia_affiche_avl_simple(lia_avl_t, FILE *);

/* ................................................................ */

/*
 * copy all the nodes of a tree into an array and sort them according to
 * their frequency
 */

/*
 * parameters: 1- lia_avl_t = root of the tree to copy 2- int * = return
 * value containing the size of the array
 */
/* return : the adress of the table containing all the nodes sorted */

lia_avl_t      *lia_avl_tree2table_freq(lia_avl_t, int *);

/* ................................................................ */

/*
 * binary search, according to the code (or freq) on the table of nodes: 1-
 * lia_avl_t = adress of the node table (obtained with
 * lia_avl_tree2table_freq) 2- int = size of the table (# of elements) 3- int
 * = code or freq looked for
 */
lia_avl_t       lia_avl_code2word(lia_avl_t *, int, int);

/* ................................................................ */

/* Info  */
/*
typedef struct
	{
    char *key_string,*field;
	} type_info;
*/

int 
compare_info(type_info * pt1, type_info * pt2)
{
	return strcmp(pt1->key_string, pt2->key_string);
}

/* ................................................................ */

/* les rotations */

int 
lia_rotation_avl_droite(lia_avl_t pt)
{
	lia_avl_t       tmpfgfd, tmpfd;
	type_info      *tmpinfo;
	char            tmpdq;

	if ((pt == NULL) || (pt->fg == NULL))
		return False;	/* la rotation n'est pas definie */

	/* On echange pt et fg  */
	tmpinfo = pt->info;
	tmpdq = pt->dq;
	pt->info = pt->fg->info;
	pt->dq = pt->fg->dq;
	pt->fg->info = tmpinfo;
	pt->fg->dq = tmpdq;

	tmpfgfd = pt->fg->fd;
	tmpfd = pt->fd;

	pt->fd = pt->fg;
	pt->fg = pt->fg->fg;
	pt->fd->fg = tmpfgfd;
	pt->fd->fd = tmpfd;

	return True;
}

int 
lia_rotation_avl_gauche(lia_avl_t pt)
{
	lia_avl_t       tmpfdfg, tmpfg;
	type_info      *tmpinfo;
	char            tmpdq;

	if ((pt == NULL) || (pt->fd == NULL))
		return False;	/* la rotation n'est pas definie */

	/* On echange pt et fd  */
	tmpinfo = pt->info;
	tmpdq = pt->dq;
	pt->info = pt->fd->info;
	pt->dq = pt->fd->dq;
	pt->fd->info = tmpinfo;
	pt->fd->dq = tmpdq;

	tmpfdfg = pt->fd->fg;
	tmpfg = pt->fg;

	pt->fg = pt->fd;
	pt->fd = pt->fd->fd;
	pt->fg->fd = tmpfdfg;
	pt->fg->fg = tmpfg;

	return True;
}

int 
lia_rotation_avl_gauche_droite(lia_avl_t pt)
{
	return ((lia_rotation_avl_gauche(pt->fg)) && (lia_rotation_avl_droite(pt))) ? True : False;
}

int 
lia_rotation_avl_droite_gauche(lia_avl_t pt)
{
	return ((lia_rotation_avl_droite(pt->fd)) && (lia_rotation_avl_gauche(pt))) ? True : False;
}

/* ................................................................ */

/* la creation d'un noeud */

lia_avl_t 
new_tree_mot_node(type_info * info)
{
	lia_avl_t       pt;
	pt = (lia_avl_t) malloc(sizeof(struct lia_avl_type));
	pt->dq = 0;
	pt->info = info;
	pt->fg = pt->fd = NULL;
	LIA_AVL_NB_NODE++;
	return pt;
}

/* ................................................................ */

/* reequilibrage */

int 
lia_reequilibre_droite(lia_avl_t racine, char *mesg, char *si_modif)
{				/* racine->dq=+2 */
	char           *r_noeud;

	if (racine == NULL) {
		if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : rotation impossible : racine==NULL");
		return False;
	}
	if (racine->fg == NULL) {
		if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : rotation droite impossible : [%s]->fg==NULL", racine->info->key_string);
		return False;
	}
	r_noeud = racine->info->key_string;

	*si_modif = racine->fg->dq == 0 ? 0 : 1;

	if (racine->fg->dq >= 0) {	/* 0 ou +1 */
		if (lia_rotation_avl_droite(racine)) {
			if (VERBOSE)
				sprintf(mesg + strlen(mesg), " rotation droite sur le noeud [%s]", r_noeud);
			if (racine->dq == 1)
				racine->dq = racine->fd->dq = 0;
			else {
				racine->dq = -1;
				racine->fd->dq = 1;
			}
			return True;
		} else if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : rotation droite impossible sur le noeud [%s]", racine->info->key_string);
	} else {
		if (lia_rotation_avl_gauche_droite(racine)) {
			if (VERBOSE)
				sprintf(mesg + strlen(mesg), " rotation gauche-droite sur le noeud [%s]", r_noeud);
			switch (racine->dq) {
			case 1:
				racine->fg->dq = 0;
				racine->fd->dq = -1;
				break;
			case -1:
				racine->fg->dq = 1;
				racine->fd->dq = 0;
				break;
			case 0:
				racine->fg->dq = racine->fd->dq = 0;
				break;
			}
			racine->dq = 0;
			return True;
		} else if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : gauche-droite impossible sur le noeud [%s]", racine->info->key_string);
	}
	return False;
}

int 
lia_reequilibre_gauche(lia_avl_t racine, char *mesg, char *si_modif)
{				/* racine->dq=-2 */
	char           *r_noeud;

	if (racine == NULL) {
		if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : rotation impossible : racine==NULL");
		return False;
	}
	if (racine->fd == NULL) {
		if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : rotation gauche impossible : [%s]->fd==NULL", racine->info->key_string);
		return False;
	}
	r_noeud = racine->info->key_string;

	*si_modif = racine->fd->dq == 0 ? 0 : 1;

	if (racine->fd->dq < 1) {	/* -1 ou 0 */
		if (lia_rotation_avl_gauche(racine)) {
			if (VERBOSE)
				sprintf(mesg + strlen(mesg), " rotation gauche sur le noeud [%s]", r_noeud);
			if (racine->dq == -1)
				racine->dq = racine->fg->dq = 0;
			else {
				racine->dq = 1;
				racine->fg->dq = -1;
			}
			return True;
		} else if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : rotation gauche impossible sur le noeud [%s]", racine->info->key_string);
	} else {
		if (lia_rotation_avl_droite_gauche(racine)) {
			if (VERBOSE)
				sprintf(mesg + strlen(mesg), " rotation droite-gauche sur le noeud [%s]", r_noeud);
			switch (racine->dq) {
			case 1:
				racine->fd->dq = -1;
				racine->fg->dq = 0;
				break;
			case -1:
				racine->fd->dq = 0;
				racine->fg->dq = 1;
				break;
			case 0:
				racine->fg->dq = racine->fd->dq = 0;
				break;
			}
			racine->dq = 0;
			return True;
		} else if (VERBOSE)
			sprintf(mesg, "LIA_AVL_ERREUR : droite-gauche impossible sur le noeud [%s]", racine->info->key_string);
	}
	return False;
}

/* ................................................................ */

/* l'insertion d'un element */

lia_avl_t 
lia_insere_avl(lia_avl_t racine, type_info * info, char *si_augm, int avec_reequilibrage, char *mesg)
{
	int             comp;

	if (racine == NULL) {
		*si_augm = 1;
		return new_tree_mot_node(info);
	}
	comp = compare_info(racine->info, info);

	if (comp == 0) {
		/* message -> le noeud est deja dans l'arbre */
		if (VERBOSE)
			sprintf(mesg, "noeud [%s] deja present", info->key_string);
		*si_augm = 0;
	} else if (comp > 0) {	/* sur le fils gauche */
		racine->fg = lia_insere_avl(racine->fg, info, si_augm, avec_reequilibrage, mesg);
		if (*si_augm) {
			if (racine->dq < 0)
				*si_augm = 0;
			racine->dq++;
		}
		/* eventuelle rotation */
		if ((avec_reequilibrage) && (racine->dq == 2)) {
			lia_reequilibre_droite(racine, mesg, si_augm);
			*si_augm = 0;
		}
	} else {		/* sur le fils droit */
		racine->fd = lia_insere_avl(racine->fd, info, si_augm, avec_reequilibrage, mesg);
		if (*si_augm) {
			if (racine->dq > 0)
				*si_augm = 0;
			racine->dq--;
		}
		/* eventuelle rotation */
		if ((avec_reequilibrage) && (racine->dq == -2)) {
			lia_reequilibre_gauche(racine, mesg, si_augm);
			*si_augm = 0;
		}
	}
	return racine;
}

lia_avl_t 
lia_ajoute_element_avl(lia_avl_t racine, type_info * info, int avec_reequilibrage, char *mesg)
{
	char            si_augm;
	if (VERBOSE)
		mesg[0] = '\0';
	return lia_insere_avl(racine, info, &si_augm, avec_reequilibrage, mesg);
}

/* ................................................................ */

/* la liberation de la place memoire de l'arbre */

void 
lia_libere_avl(lia_avl_t racine)
{
	if (racine) {
		lia_libere_avl(racine->fg);
		lia_libere_avl(racine->fd);
		free_type_info(racine->info);
		free(racine);
	}
}

/* ................................................................ */

/* la recherche d'un element */

lia_avl_t 
lia_recherche_avl(lia_avl_t racine, type_info * info)
{
	int             comp;
	if (racine == NULL)
		return NULL;
	if ((comp = compare_info(racine->info, info)) == 0)
		return racine;
	if (comp > 0)
		return lia_recherche_avl(racine->fg, info);
	else
		return lia_recherche_avl(racine->fd, info);
}

/* ................................................................ */

/* l'affichage de l'arbre */

void 
lia_affiche_avl_simple(lia_avl_t racine, FILE * file)
{
	if (racine) {
		lia_affiche_avl_simple(racine->fg, file);
		print_type_info(racine->info);
		lia_affiche_avl_simple(racine->fd, file);
	}
}

/* ................................................................ */

/*
 * copy all the nodes of a tree into an array and sort them according to
 * their frequency
 */

int 
compare_freq(const void *a, const void *b)
{
	lia_avl_t      *c, *d;
	c = (lia_avl_t *) a;
	d = (lia_avl_t *) b;
	return ((*d)->info->code - (*c)->info->code);
}

void 
copy_tree2table(lia_avl_t racine, lia_avl_t * tabl, int *i)
{
	if (racine != NULL) {
		tabl[(*i)++] = racine;
		copy_tree2table(racine->fg, tabl, i);
		copy_tree2table(racine->fd, tabl, i);
	}
}

int 
lia_avl_size(lia_avl_t racine)
{
	if (racine == NULL)
		return 0;
	else
		return 1 + lia_avl_size(racine->fg) + lia_avl_size(racine->fd);
}

lia_avl_t      *
lia_avl_tree2table_freq(lia_avl_t racine, int *nb)
{
	lia_avl_t      *tabl;
	int             i;
	*nb = lia_avl_size(racine);
	tabl = (lia_avl_t *) malloc(sizeof(lia_avl_t) * (*nb));
	i = 0;
	copy_tree2table(racine, tabl, &i);
	qsort(tabl, *nb, sizeof(lia_avl_t), compare_freq);
	return tabl;
}

lia_avl_t 
lia_avl_code2word(lia_avl_t * tabl, int nb, int code)
{
	struct lia_avl_type tkey;
	lia_avl_t      *resu, key;
	type_info       info;
	info.code = code;
	tkey.info = &info;
	key = (lia_avl_t) (&tkey);
	resu = (lia_avl_t *) bsearch(&key, tabl, nb, sizeof(lia_avl_t), compare_freq);
	return *resu;
}

/* ................................................................ */

/* managing lexicon */

#define MAX_LEXICON_AVL	100

lia_avl_t       T_avl_lexicon[MAX_LEXICON_AVL];
lia_avl_t      *T_tabl_avl_lexicon[MAX_LEXICON_AVL];
int             T_tabl_avl_lexicon_size[MAX_LEXICON_AVL];
int             Nb_Avl_Lexicon = 0;

void 
add_word_lexicon(int lexid, char *ch, int code)
{
	T_avl_lexicon[lexid] = lia_ajoute_element_avl(T_avl_lexicon[lexid], new_type_info(ch, code), True, NULL);
}

int 
new_lexicon()
{
	if (Nb_Avl_Lexicon == MAX_LEXICON_AVL)
		LIA_AVL_ERREUR("cste 'MAX_LEXICON_AVL' too small", "");
	T_avl_lexicon[Nb_Avl_Lexicon] = NULL;
	return Nb_Avl_Lexicon++;
}

int 
load_lexicon(char *filename)
{
	FILE           *file;
	static char     ch[TailleLigne], *pt;
	int             code;

	if (Nb_Avl_Lexicon == MAX_LEXICON_AVL)
		LIA_AVL_ERREUR("cste 'MAX_LEXICON_AVL' too small", "");
	if (!(file = fopen(filename, "rt")))
		LIA_AVL_ERREUR("can't open:", filename);
	for (code = 0, T_avl_lexicon[Nb_Avl_Lexicon] = NULL; fgets(ch, TailleLigne, file);) {
		pt = strtok(ch, " \t\n");
		if (pt)
			pt = strtok(NULL, " \t\n");
		if (!pt)
			code++;
		else if (sscanf(pt, "%d", &code) != 1)
			LIA_AVL_ERREUR("bad format in:", filename);
		T_avl_lexicon[Nb_Avl_Lexicon] = lia_ajoute_element_avl(T_avl_lexicon[Nb_Avl_Lexicon], new_type_info(ch, code), True, NULL);
	}
	T_tabl_avl_lexicon[Nb_Avl_Lexicon] = lia_avl_tree2table_freq(T_avl_lexicon[Nb_Avl_Lexicon],
				&(T_tabl_avl_lexicon_size[Nb_Avl_Lexicon]));
	fclose(file);
	return Nb_Avl_Lexicon++;
}

int 
load_lexicon_first_field(char *filename)
{
	FILE           *file;
	static char     ch[TailleLigne], *pt;
	int             code;

	if (Nb_Avl_Lexicon == MAX_LEXICON_AVL)
		LIA_AVL_ERREUR("cste 'MAX_LEXICON_AVL' too small", "");
	if (!(file = fopen(filename, "rt")))
		LIA_AVL_ERREUR("can't open:", filename);
	for (code = 0, T_avl_lexicon[Nb_Avl_Lexicon] = NULL; fgets(ch, TailleLigne, file);) {
		pt = strtok(ch, " \t\n");
		code++;
		T_avl_lexicon[Nb_Avl_Lexicon] = lia_ajoute_element_avl(T_avl_lexicon[Nb_Avl_Lexicon], new_type_info(ch, code), True, NULL);
	}
	T_tabl_avl_lexicon[Nb_Avl_Lexicon] = lia_avl_tree2table_freq(T_avl_lexicon[Nb_Avl_Lexicon],
				&(T_tabl_avl_lexicon_size[Nb_Avl_Lexicon]));
	fclose(file);
	return Nb_Avl_Lexicon++;
}

void 
delete_lexicon(int lexid)
{
	lia_libere_avl(T_avl_lexicon[lexid]);
	free(T_tabl_avl_lexicon[lexid]);
	T_avl_lexicon[lexid] = NULL;
	T_tabl_avl_lexicon[lexid] = NULL;
}

/* ................................................................ */

/* new FEB07 */

void 
lia_avl_reset(lia_avl_t racine)
{
	if (racine) {
		racine->info->code = 0;
		lia_avl_reset(racine->fg);
		lia_avl_reset(racine->fd);
	}
}

void 
reset_value_tree(int lexid)
{
	lia_avl_reset(T_avl_lexicon[lexid]);
}

int 
inc_freq_word(int lexid, char *word)
{
	lia_avl_t       resu;
	type_info       info;
	info.key_string = word;
	resu = lia_recherche_avl(T_avl_lexicon[lexid], &info);
	if (resu)
		resu->info->code++;
	return resu ? True : False;
}

void 
lia_avl_print_freq_tree(lia_avl_t racine)
{
	if (racine) {
		lia_avl_print_freq_tree(racine->fg);
		if (racine->info->code > 0)
			printf("%s %d ", racine->info->key_string, racine->info->code);
		lia_avl_print_freq_tree(racine->fd);
	}
}

void 
lia_avl_print_distrib_tree(lia_avl_t racine)
{
	if (racine) {
		lia_avl_print_distrib_tree(racine->fg);
		printf("%d ", racine->info->code);
		lia_avl_print_distrib_tree(racine->fd);
	}
}

void 
print_scored_text_tree(int lexid)
{
	lia_avl_print_freq_tree(T_avl_lexicon[lexid]);
}

void 
print_distrib_text_tree(int lexid)
{
	lia_avl_print_distrib_tree(T_avl_lexicon[lexid]);
}

/* ................................................................ */

int 
code2word(int lexid, int code, char **word)
{
	lia_avl_t       resu;
	if (code < 0)
		return False;
	resu = lia_avl_code2word(T_tabl_avl_lexicon[lexid], T_tabl_avl_lexicon_size[lexid], code);
	if (resu)
		*word = resu->info->key_string;
	return resu ? True : False;
}

int 
word2code(int lexid, char *word, int *code)
{
	lia_avl_t       resu;
	type_info       info;
	info.key_string = word;
	resu = lia_recherche_avl(T_avl_lexicon[lexid], &info);
	if ((resu) && (code))
		*code = resu->info->code;
	return resu ? True : False;
}