Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#51 Le 27/11/2007, à 21:49

jeanvalery

Re : Créer son pseudo... comment ?

smile j'ai failli le rajouter... "monde de merde!"


Membre du club des beaux Ubunteros

Hors ligne

#52 Le 27/11/2007, à 23:15

Link31

Re : Créer son pseudo... comment ?

jpaul a écrit :

md5(rand())

Ah, voilà  une magnifique occasion de caser un bout de code.

Le but de ces 600 lignes est de générer des mots prononçables (à  peu près) en français. Ne vous attendez pas à  ce que les mots aient un sens ou "sonnent bien", l'algorithme n'a pas été conçu pour ça.

à‡a peut toujours être utile pour trouver un pseudo wink

/***************************************************************************
 *   Copyright (C) 2007 by Link31                                          *
 *   [killall -9 spambot]                                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License                  *
 *   as published by the Free Software Foundation; either version 2        *
 *   of the License, or (at your option) any later version.                *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>

#define DEFAULT_MAX_LENGTH 10
#define DEFAULT_MIN_LENGTH 4
#define DEFAULT_VERBOSE FALSE
#define DEFAULT_HELP FALSE
#define DEFAULT_VERSION FALSE
#define DEFAULT_USE_ACCENTS FALSE
#define DEFAULT_ACCENT_PB 18
#define DEFAULT_WORD_COUNT 1
#define DEFAULT_CUSTOM_SEED -1

typedef struct
{
	char vowel;
	float probability;
	char *accents; // les accents les plus "nombreux" dans cette chaà®ne sont censés sortir le plus souvent
} vowel;

typedef struct
{
	char consonant;
	float probability;
	char *second;
	char *third;
	vowel preferred_vowel;
	float ending_pb;
} consonant;

typedef enum { FALSE=0, TRUE } BOOL;

static char *args[] =
{
	"--help",		// help
	"-h",			// help (short version)
	"--verbose",		// verbose
	"-v",			// verbose (short version)
	"--version",		// version
	"-c",			// count (short version)
	"--count",		// count
	"-l",			// min length (short version)
	"--min-length",		// min length
	"-L",			// max length (short version)
	"--max-length",		// max length
	"-a",			// accents (short version)
	"--accents",		// accents
	"-p",			// accent probability (short version)
	"--accent-probability",	// accent probability
	"-s",			// custom seed (short version)
	"--seed",		// custom seed
};

static vowel vowels[] =
{
	{'e',	17.69,	"éééêêèèà«"},
	{'a',	8.11,	"à à à à¢à¢à¢à¤"},
	{'i',	7.24,	"iià®à®à®à¯"},
	{'u',	5.23,	"uuà¹à¹à¹ûûà¼"},
	{'o',	5.20,	"oà´à´à´à¶"},
	{'y',	0.26,	"yyyà¿"},
};

static consonant consonants[] =
{
	{'s',	8.87,	"bcdfghjklmnpqrtvw",	"br|ch|cl|cr|gr|ph|pl|pr|td|tf|th|tn|tp|tr",	{0,0,0},		80.0},
	{'n',	7.68,	"bcdfghjkmpqrstvxz",	"ch|cl|cr|cs|ct|db|dr|ds|fl|fr|gh|gl|gr|gs\
						|gt|ks|rh|sb|sc|sf|sg|sh|sl|sm|sp|ss|st|sv\
						|tg|th|tl|tr|ts|vr",				{0,0,0},		75.0}, // l
	{'t',	7.44,	"bdfghlmnprswz",	"ch|h|hl|hm|hn|hr|hs|sc",			{0,0,0},		75.0}, // c
	{'r',	6.43,	"bcdfghjklmnpqstvxz",	"bl|ch|cl|cm|cs|ct|ds|dt|fl|fs|gh|gl|gm|gn\
						|gs|ks|ls|ph|pl|ps|pt|sc|sh|sp|sq|st|th|ts\
						|tz",						{0,0,0},		75.0},
	{'l',	5.99,	"bcdfghkmnpqstvwz",	"ch|cr|cs|fr|fs|gr|kh|ks|ms|ph|pr|ps|pt|sh\
						|st|th|tm|tr|ts",				{0,0,0},		75.0},
	{'d',	4.28,	"bghjklmnrstvwz",	"",						{0,0,0},		60.0},
	{'c',	3.38,	"fghklmpqrstz",		"hl|hm|hn|hp|hr|hs|ht|hw|kb|kh|kp|ks|pt|tr|ts", {0,0,0},		30.0}, // dg
	{'p',	2.92,	"chlnrstx",		"hl|hr|ht|st|tr|ts",				{0,0,0},		25.0},
	{'m',	2.29,	"bklnpstw",		"bl|br|bs|kh|ph|pl|pr|ps|pt|st",		{0,0,0},		45.0},
	{'v',	1.28,	"nrs",			"",						{0,0,0},		25.0},
	{'g',	1.19,	"bdhlmnrstz",		"hs|ht|ns|st|ts",				{'u', 50.0, "à¹à¹à¹ûûà¼"},	25.0},
	{'f',	1.13,	"cjlrst",		"ts|tw",					{0,0,0},		25.0},
	{'q',	0.83,	"s",			"",						{'u', 90.0, "à¹à¹à¹ûûà¼"},	15.0},
	{'b',	0.81,	"cdhjlmnrstv",		"sc|st|tr",					{0,0,0},		25.0},
	{'h',	0.74,	"glmnprstw",		"ms|ts",					{0,0,0},		35.0},
	{'x',	0.53,	"cfhpqstw",		"cl|cr|pl|pr|tr",				{0,0,0},		50.0},
	{'j',	0.18,	"",			"",						{0,0,0},		20.0},
	{'z',	0.12,	"d",			"",						{0,0,0},		40.0},
	{'w',	0.06,	"hklnrs",		"ks|ns",					{0,0,0},		13.0},
	{'k',	0.02,	"bcghlmnprst",		"ch|hs",					{0,0,0},		25.0},
};

BOOL is_verbose = DEFAULT_VERBOSE;
BOOL use_accents = DEFAULT_USE_ACCENTS;
int accent_pb = DEFAULT_ACCENT_PB;
int seed = DEFAULT_CUSTOM_SEED;

BOOL init_random(void);
BOOL build_word(char *word, int length, vowel language_vowels[], consonant language_consonants[], size_t vowels_size, size_t consonants_size);

BOOL init_random(void)
{
	if (seed == DEFAULT_CUSTOM_SEED)
	{
		struct timeval tv;
		if (gettimeofday(&tv, NULL) != 0)
			return FALSE;
		seed = tv.tv_usec;
		if (is_verbose)
			printf("Random seed set to: %lu\n", tv.tv_usec);
	}
	else
	{
		if (is_verbose)
			printf("using given seed value: %u\n", seed);
	}
	srand(seed);
	return TRUE;
}

int myrand(int min, int max)
{
	if (min > max)
	{
		if (is_verbose)
			printf("WARNING: invalid limits in myrand()\n");
		return 0;
	}

	return min + (rand() % ((max+1)-min));
}

BOOL build_word(char *p_word, int length, vowel language_vowels[], consonant language_consonants[], size_t vowels_size, size_t consonants_size)
{
	char *word = 0;
	short int consonant_nb = 0;
	short int vowel_nb = 0;
	BOOL *c_or_v_table = 0;

	if (!p_word)
	{
		if (is_verbose)
			printf("FATAL: Invalid pointer to word\n");
		return FALSE;
	}
	if (length <= 0)
	{
		if (is_verbose)
			printf("FATAL: Invalid length: %u\n", length);
		return FALSE;
	}

	c_or_v_table = (BOOL*)malloc((length+1)*sizeof(BOOL));
	if (!c_or_v_table)
	{
		printf("FATAL: malloc() failed");
		return FALSE;
	}

	word = p_word;

	while (word - p_word < length)
	{
		BOOL is_vowel;
	
		// si les trois premières lettre ne sont pas toutes des consonnes (FALSE=0)
		// TODO: plus de tests
		if (word - p_word == 3 && c_or_v_table[0] + c_or_v_table[1] + c_or_v_table[2] == 0)
		{
			if (is_verbose)
				printf("Found three consonants as the three first letters, retrying...\n");
			word = p_word;
			consonant_nb = 0;
			vowel_nb = 0;
		}

		if (vowel_nb >= 2) // TODO: 2 => parameter (?) // 2
			is_vowel = FALSE;
		else if (consonant_nb >= 3) // TODO: 3 => parameter (?)
			is_vowel = TRUE;
		else
			// tout ça pour qu'il y ait peu de doubles voyelles (=> paramètre ?)
			is_vowel = myrand(0, 100) > vowel_nb*45 + 50;

		c_or_v_table[word - p_word] = is_vowel;

		if (is_vowel)
		{
			vowel *vw = 0;
			BOOL accent = FALSE;
			while (!vw)
			{
				unsigned int i, r;

				// on privilégie l'utilisation des voyelles les plus adaptées à  cette consonne
				if (consonant_nb > 0)
				{
					consonant *old_cs = language_consonants;
					while (old_cs->consonant != *(word-1))
						old_cs++;
					
					r = myrand(0, 10000);
					if (old_cs->preferred_vowel.vowel && r < old_cs->preferred_vowel.probability*100)
						vw = &(old_cs->preferred_vowel);
				}
				// sinon, on la choisit au hasard
				if (!vw)
				{
					for (i = 0; i < vowels_size; i++)
					{
						r = myrand(0, 10000);
						if (r < language_vowels[i].probability*100)
						{
							vw = &language_vowels[i];
							break;
						}
					}
				}
			}
			accent = myrand(0, 100) < accent_pb;
			if (use_accents && accent)
				*word++ = vw->accents[myrand(0, strlen(vw->accents)-1)];
			else
				*word++ = vw->vowel;
			consonant_nb = 0;
			vowel_nb++;
		}
		else
		{
			consonant *cs = 0;
			while (!cs && consonant_nb < 3)
			{
				unsigned int i, r;
				for (i = 0; i < consonants_size; i++)
				{
					// passe à  la consonne suivante si on est à  la fin du mot et que cette consonne
					// n'est pas très souvent rencontrée à  la fin d'un mot
					if (word - p_word == length - 1)
					{
						r = myrand(0, 10000);
						if (r >= language_consonants[i].ending_pb*100)
							break;
					}

					r = myrand(0, 10000);
					if (r < language_consonants[i].probability*100.0)
					{
						if (consonant_nb == 0)
						{
							cs = &language_consonants[i];
							break;
						}
						else if (consonant_nb == 1)
						{
							BOOL is_second = FALSE;
							char tmp_cs = language_consonants[i].consonant;
							char *second = 0;
							consonant *old_cs = language_consonants;

							while (old_cs->consonant != *(word-1))
								old_cs++;

							second = old_cs->second;
							while (*second && !is_second)
								is_second = (tmp_cs == *second++);

							if (is_second)
							{
								cs = &language_consonants[i];
								break;
							}
							else
							{
								// no second consonant possible, force the
								// next letter to be a vowel
								consonant_nb = 3;
								break;
							}
						}
						else if (consonant_nb == 2)
						{
							BOOL is_third = FALSE;
							char tmp_cs = language_consonants[i].consonant;
							char *third = 0;
							consonant *first_cs = language_consonants;

							while (first_cs->consonant != *(word-2))
								first_cs++;

							third = first_cs->third;
							while (*third && !is_third)
							{
								is_third = (*(word-1) == third[0] && tmp_cs == third[1]);
								while (*third && *third != '|')
								{
									third++;
								}
								if (*third == '|') third++;
							}

							if (is_third)
							{
								cs = &language_consonants[i];
								break;
							}
							else
							{
								// no third consonant possible, force the
								// next letter to be a vowel
								consonant_nb++;
								break;
							}
						}
					}
				}
			}
			if (cs)
			{
				*word++ = cs->consonant;
				vowel_nb = 0;
				consonant_nb++;
			}
		}
	}

	*word = '\0';
	free(c_or_v_table);
	return TRUE;
}

int main(int argc, char* argv[])
{
	int max_length = DEFAULT_MAX_LENGTH;
	int min_length = DEFAULT_MIN_LENGTH;
	int word_count = DEFAULT_WORD_COUNT;

	int i;
	unsigned int j;

	BOOL everything_valid = TRUE;
	BOOL display_help = DEFAULT_HELP;
	BOOL display_version = FALSE;

	for (i = 1; i < argc; i++)
	{
		BOOL valid = FALSE;

		for (j = 0; j < sizeof(args)/sizeof(args[0]); j++)
		{
			if (strcmp(argv[i], args[j]) == 0)
			{
				valid = TRUE;
				break;
			}
		}
		if (!valid)
		{
			everything_valid = FALSE;
			printf("%s: unrecognized option: `%s'\n", argv[0], argv[i]);
		}

		if (is_verbose == DEFAULT_VERBOSE)
			is_verbose = (strcmp(argv[i], args[2]) == 0) || (strcmp(argv[i], args[3]) == 0);
		if (display_help == DEFAULT_HELP)
			display_help = (strcmp(argv[i], args[0]) == 0) || (strcmp(argv[i], args[1]) == 0);
		if (use_accents == DEFAULT_USE_ACCENTS)
			use_accents = (strcmp(argv[i], args[11]) == 0) || (strcmp(argv[i], args[12]) == 0);
		if (display_version == DEFAULT_VERSION)
			display_version = (strcmp(argv[i], args[4]) == 0);

		if (strcmp(argv[i], args[7]) == 0 || strcmp(argv[i], args[8]) == 0)
		{
			char **endptr = 0;
			if (i < argc-1)
			{
				endptr = &argv[i+1];
				min_length = strtol(argv[i+1], endptr, 10);
				i++;
			}
			else
			{
				endptr = &argv[i];
				*endptr += strlen(argv[i]); // so its value is '\0', and the warning message (see below) is not displayed
				printf("%s: min length: value expected\n", argv[0]);
				display_help = TRUE;
			}

			if (**endptr != '\0' || i >= argc)
			{
				printf("WARNING: Invalid min length, using default value\n");
				min_length = DEFAULT_MIN_LENGTH;
			}
		}
		if (strcmp(argv[i], args[9]) == 0 || strcmp(argv[i], args[10]) == 0)
		{
			char **endptr = 0;
			if (i < argc-1)
			{
				endptr = &argv[i+1];
				max_length = strtol(argv[i+1], endptr, 10);
				i++;
			}
			else
			{
				endptr = &argv[i];
				*endptr += strlen(argv[i]);
				printf("%s: max length: value expected\n", argv[0]);
				display_help = TRUE;
			}

			if (**endptr != '\0' || i >= argc)
			{
				printf("WARNING: Invalid max length, using default value\n");
				max_length = DEFAULT_MAX_LENGTH;
			}
		}
		if (strcmp(argv[i], args[13]) == 0 || strcmp(argv[i], args[14]) == 0)
		{
			char **endptr = 0;
			if (i < argc-1)
			{
				endptr = &argv[i+1];
				accent_pb = strtol(argv[i+1], endptr, 10);
				i++;
			}
			else
			{
				endptr = &argv[i];
				*endptr += strlen(argv[i]);
				printf("%s: accent probability: percentage expected\n", argv[0]);
				display_help = TRUE;
			}

			if (accent_pb < 0 || accent_pb > 100 || **endptr != '\0' || i >= argc)
			{
				printf("WARNING: Invalid accent probability, using default value\n");
				accent_pb = DEFAULT_ACCENT_PB;
			}
		}
		if (strcmp(argv[i], args[15]) == 0 || strcmp(argv[i], args[16]) == 0)
		{
			char **endptr = 0;
			if (i < argc-1)
			{
				endptr = &argv[i+1];
				seed = strtol(argv[i+1], endptr, 10);
				i++;
			}
			else
			{
				endptr = &argv[i];
				*endptr += strlen(argv[i]);
				printf("%s: custom seed: value expected\n", argv[0]);
				display_help = TRUE;
			}

			if ((seed < 0 && seed != DEFAULT_CUSTOM_SEED) || **endptr != '\0' || i >= argc)
			{
				printf("WARNING: Invalid custom seed, using default value\n");
				seed = DEFAULT_CUSTOM_SEED;
			}
		}
		if (strcmp(argv[i], args[5]) == 0 || strcmp(argv[i], args[6]) == 0)
		{
			char **endptr = 0;
			if (i < argc-1)
			{
				endptr = &argv[i+1];
				word_count = strtol(argv[i+1], endptr, 10);
				i++;
			}
			else
			{
				endptr = &argv[i];
				*endptr += strlen(argv[i]);
				printf("%s: word count: value expected\n", argv[0]);
				display_help = TRUE;
			}

			if (**endptr != '\0' || i >= argc)
			{
				printf("WARNING: Invalid word count, using default value\n");
				word_count = DEFAULT_WORD_COUNT;
			}
		}
	}
	if (!everything_valid && !display_help)
	{
		printf("Try `%s %s' for more information.\n", argv[0], args[0]);
		return EXIT_FAILURE;
	}

	if (display_help)
	{
		printf("Usage: %s [OPTIONS]\n", argv[0]);
		printf("Generate random pronounceable words.\n");
		printf("\n");
		printf("  %s N, %s N\t generate N words\n", args[5], args[6]);
		printf("  %s <length>, %s <length>\t do not generate words shorter than <length>\n", args[7], args[8]);
		printf("  %s <length>, %s <length>\t do not generate words longer than <length>\n", args[9], args[10]);
		printf("  %s, %s\t use accents\n", args[11], args[12]);
		printf("  %s <percentage>, %s <percentage>\t set accent probability\n", args[13], args[14]);
		printf("  %s <value>, %s <value>\t use a custom seed\n", args[15], args[16]);
		printf("  %s, %s\t display detailed information\n", args[3], args[2]);
		printf("  %s, %s\t display this help and exit\n", args[1], args[0]);
		printf("      %s\t output version information and exit\n", args[4]);
		return EXIT_SUCCESS;
	}

	if (display_version)
	{
		printf("%s (Name) 0.9\n\
Copyright (C) 2007 Link31\n\
This is free software.  You may redistribute copies of it under the terms of\n\
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n\
There is NO WARRANTY, to the extent permitted by law.\n\
\n\
Written by Link31.\n", argv[0]);
		return EXIT_SUCCESS;
	}

	char *word = 0;
	int length = 0;

	if (!init_random())
	{
		printf("FATAL: Unable to get a random seed from the time of day\n");
		return EXIT_FAILURE;
	}

	if (min_length < 0 || max_length < 0 || min_length >= max_length)
	{
		printf("WARNING: Invalid values for min/max length, using default values\n");
		max_length = DEFAULT_MAX_LENGTH;
		min_length = DEFAULT_MIN_LENGTH;
	}

	if (is_verbose)
		printf("Min length = %d, Max length = %d\n", min_length, max_length);

	for (i = 0; i < word_count; i++)
	{
		length = myrand(min_length, max_length);
		
		if (is_verbose)
			printf("Length: %u\n", length);
	
		word = (char*)malloc((length+1)*sizeof(char));
		if (!word)
		{
			printf("FATAL: malloc() failed");
			return EXIT_FAILURE;
		}
	
		if (!build_word(word, length, vowels, consonants,
			sizeof(vowels)/sizeof(vowels[0]), sizeof(consonants)/sizeof(consonants[0])))
		{
			printf("FATAL: Unable to generate the word\n");
			return EXIT_FAILURE;
		}
	
		printf("%s\n", word);
		free(word);
	}

	return EXIT_SUCCESS;
}

Hors ligne

#53 Le 14/07/2015, à 13:17

Nicoliux-Hack-bug

Re : Créer son pseudo... comment ?

Salut tout se que vous m'avez dis mon pas du tout aider ! svp ! AIder moi a créer mon propre pseudo personnel


Modération

Bonjour,

Merci de respecter les règles du forum, notamment un effort de rédaction (orthographe et ponctuation) et pas d'abus des couleurs. Le genre kikoolol n'est pas le bienvenu ici.

Et comme ce sujet est plutôt ancien, on ferme !

Dernière modification par cqfd93 (Le 14/07/2015, à 18:06)

Hors ligne