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
| #define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#define DICO "lefff-2.1.txt"
#define TAILLE_LIGNE_MAX 200
/*
Extrait une partie du mot, comprise entre les indices i et j. On suppose:
i < j;
j <= (strlen(mot) - 1);
*/
char* extractPart(char mot[], int i, int j, char partie[])
{
partie = strndup(mot+i, j+1);
return partie;
}
/*
Fonction concaténant en un mot deux mots prix en argument.
*/
char* concateneMots(char mot1[], char mot2[], char concatenation[])
{
strcpy(concatenation, mot1);
strcat(concatenation, mot2);
return concatenation;
}
int nombreLignes(char chemin[])
{
int nombre_lignes = 0;
char c[200];
FILE* fichier = NULL;
fichier = fopen(chemin, "r");
while (fgets(c, TAILLE_LIGNE_MAX, fichier) != NULL)
{
nombre_lignes++;
}
fclose(fichier);
return nombre_lignes;
}
/*
accoutrée v [pred='accoutrer_____1<obj:(sn),objde:(de-sn),suj:(par-sn)>',cat=v,@passive,@être,@Kfs]
*/
char* partOfSpeech(char ligne_dico[], char classeg[])
{
char* mot = (char*)malloc(TAILLE_LIGNE_MAX * sizeof(char));
int* nombre = (int*)malloc(TAILLE_LIGNE_MAX * sizeof(int));
sscanf(ligne_dico, "%s %d %s", mot, nombre, classeg);
if (*nombre == 0)
{
sscanf(ligne_dico, "%s %s", mot, classeg);
}
free(nombre);
free(mot);
return classeg;
}
/*
verbe, adjectif, pronom, nom commun, (nom propre), adverbe, *conjonction de coordinations, *préposition
v adj pro nc np adv coo prep
conjonction de subordination, determinant,
csu(afin que) cld(lui) det
*/
/*
Fonction qui extrait le mot de la ligne du dictionnaire correspondante.
*/
char* getMot(char ligne_dico[], char mot_recup[])
{
sscanf(ligne_dico, "%s", mot_recup);
return mot_recup;
}
char* rechercheDichotomique(char mot[], FILE* dictionnaire, char ligne_renvoyee[])
{
int stop = 0;
int position_current = 0;
int n = 2;
int signe = 1;
int etape = 0;
int modificateur = 0;
int k = 0;
int l = 0;
int leng = 0;
char aux[TAILLE_LIGNE_MAX];
char mot_lu[TAILLE_LIGNE_MAX];
char c[1];
fseek(dictionnaire, 0, SEEK_END);
int taille_dico = ftell(dictionnaire);
fseek(dictionnaire, 0, SEEK_SET);
while (stop != 1)
{
if (position_current%2 == 0)
{
modificateur = taille_dico/n;
}
else
{
modificateur = ((int)taille_dico/(n)) + 1;
}
if (modificateur < k)
{
modificateur = k;
}
position_current = position_current + signe*(modificateur);
if (position_current < 0)
{
position_current = position_current + modificateur - l;
}
etape = etape + 1;
n = n * 2;
fseek(dictionnaire, position_current, SEEK_SET);
l = 0;
while((c[0] = fgetc(dictionnaire)) != '\n')
{
fseek(dictionnaire, -2, SEEK_CUR);
l = l + 1;
}
position_current = position_current - l;
fscanf(dictionnaire, "%s", mot_lu);
leng = strlen(mot_lu);
/*
printf("etape %d, position dans le fichier: %d, pas de la recherche: %d\n", etape, position_current, modificateur);
printf("mot lu: %s\n", mot_lu);
printf("comparaison entre %s et '%s': %d\n\n", mot_lu, mot, strcmp(mot_lu, mot));
*/
if (etape > 25)
{
fseek(dictionnaire, 0, SEEK_SET);
return "*error";
}
if (strcmp(mot_lu, mot) == 1)
{
signe = -1;
k = 24;
while ((c[0] = fgetc(dictionnaire)) != '\n')
{
k = k + 1;
}
fseek(dictionnaire, -k, SEEK_CUR);
}
if (strcmp(mot_lu, mot) == -1)
{
signe = 1;
k = 24;
while ((c[0] = fgetc(dictionnaire)) != '\n')
{
k = k + 1;
}
fseek(dictionnaire, -k, SEEK_CUR);
}
if (strcmp(mot_lu, mot) == 0)
{
/* printf("%s = %s, recherche terminee en %d etapes!\n", mot_lu, mot, etape);*/
stop = 1;
}
}
fseek(dictionnaire, -leng, SEEK_CUR);
fgets(aux, TAILLE_LIGNE_MAX, dictionnaire);
strcpy(ligne_renvoyee, aux);
/*
printf("taille du fichier: %d octets\n", taille_dico);
*/
fseek(dictionnaire, 0, SEEK_SET);
return ligne_renvoyee;
}
void chercheAmbiguite(char mot[], char exception1[], char exception2[], FILE* dictionnaire, char ligne_renvoyee[])
{
int i = 0;
int length = strlen(mot);
char* part1;
char* part2;
char* partie = (char*)malloc(TAILLE_LIGNE_MAX * sizeof(char));
for (i = 0; i < length; i++)
{
part1 = extractPart(mot, 0, i, partie);
part2 = extractPart(mot, i+1, length-1, partie);
if ((strcmp(part1, exception1) != 0) && (strcmp(part1, exception2) != 0) && (strcmp(part2, exception1) != 0) && (strcmp(part2, exception2) != 0))
{
/*
printf("%s %s\n", part1, part2);
*/
if ((rechercheDichotomique(part1, dictionnaire, ligne_renvoyee) != "*error") && (strlen(part1) > 1))
{
if ((rechercheDichotomique(part2, dictionnaire, ligne_renvoyee) != "*error") && (strlen(part2) > 1))
{
printf("%s %s <<< concaténation initiale\n%s %s <<< partitionnement alternatif; URL équivoque valable!\n", exception1, exception2, part1, part2);
}
}
}
}
free(partie);
}
int main(int argc, char ** argv)
{
if (argc != 2)
{
printf("Un seul mot en argument à la fois, merci.\n");
return 0;
}
int i = 0;
int nombre_lignes;
char* result;
char* result2;
char ligne_lue[TAILLE_LIGNE_MAX];
char* mot_recup = (char*)malloc(TAILLE_LIGNE_MAX * sizeof(char));
/*Affectation des variables relatives à rechercheDichotomique */
FILE* FICHIER_DICO;
FICHIER_DICO = fopen(DICO, "r");
char* LIGNE_RENVOYEE = (char*)malloc(TAILLE_LIGNE_MAX * sizeof(char));
/*Affectation des variables relatives à concateneMots */
char* CONCATENATION = (char*)malloc(TAILLE_LIGNE_MAX * sizeof(char));
/*Déclaration des variables relatives à partOfSpeech */
char* classeg = (char*)malloc(TAILLE_LIGNE_MAX * sizeof(char));
result = rechercheDichotomique(argv[1], FICHIER_DICO, LIGNE_RENVOYEE);
if (strcmp(result, "*error") != 0)
{
result2 = partOfSpeech(result, classeg);
printf("%s", result);
printf("Classe grammaticale = %s\n", result2);
}
else
{
printf("Le mot propose ne fait pas partie du dictionnaire :'-)\n");
}
nombre_lignes = nombreLignes(DICO);
FILE* fichier = NULL;
fichier = fopen(DICO, "r");
for (i = 0; i < nombre_lignes; i++)
{
fgets(ligne_lue, TAILLE_LIGNE_MAX, fichier);
getMot(ligne_lue, mot_recup);
if (i%1000 == 0)
{
printf("%dème mot lu = %s\n", i, mot_recup);
}
chercheAmbiguite(concateneMots(argv[1], mot_recup, CONCATENATION), argv[1], mot_recup, FICHIER_DICO, LIGNE_RENVOYEE);
chercheAmbiguite(concateneMots(mot_recup, argv[1], CONCATENATION), mot_recup, argv[1], FICHIER_DICO, LIGNE_RENVOYEE);
}
printf("Recherche terminée! :'-)\n");
fclose(FICHIER_DICO);
fclose(fichier);
free(LIGNE_RENVOYEE);
free(CONCATENATION);
free(classeg);
return 0;
} |
Partager