|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn this article, I will guide you in creation of a nickname generator that will allow the user to generate a nickname of arbitrary length. It will also offer a possibility to look up the generated nickname at Google to see if it has any Google search result. If the nickname has 0 results, it should be unique. We often register ourselves at message boards and various sites, but we don't always want to use our real name or "official" nickname. Sometimes it's desirable to have a unique nickname that can't be traced back to us. There are more reasons for this - either to do some undercover work, or to discuss some private matters anonymously. This is the occasion where a nickname generator comes in handy. The easiest way to generate a nickname would seem to be to generate a random string. But the result would look like tmxoit, rdyari, qefoauh, uatyni - not too good. It really seems like a random bunch of characters and might be hard to memorize. A good way to generate a nickname might be to alternate vowels and consonants, so we will get results like Zobona, Osavir, Ecumol, or Obunu. ImplementationNickname generation will be encapsulated in a class with the following class diagram:
That class would contain arrays of all available vowels and consonants: char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u', 'y' };
char[] consonants = new char[] { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k',
'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z' };
The constructor of class The class contains one public string Generate(int length)
{
StringBuilder sb = new StringBuilder();
//initialize our vowel/consonant flag
bool flag = (random.Next(2) == 0);
for (int i = 0; i < length; i++)
{
sb.Append(GetChar(flag));
flag = !flag; //invert the vowel/consonant flag
}
return sb.ToString();
}
It first decides if the starting character would be a vowel or a consonant, then in a loop adds other vowels and consonants (in turns) to the result. The method that gets the random character is as follows: private char GetChar(bool vowel)
{
if (vowel)
{
return vowels[random.Next(vowels.Length)];
}
return consonants[random.Next(consonants.Length)];
}
The final step would be to check if the generated nickname is unique. The class provides a method public static int GetWebResultsForNickname(string text)
{
string address = http://www.google.com/xhtml/search?mrestrict=xhtml&hl=en&q=
+ text;
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
string input = client.DownloadString(address);
//regex
Match match = new Regex
("Results [0-9] - [0-9]+ of .*?([0-9|,]+)\\.").Match(input);
if (match.Success)
{
return int.Parse(match.Groups[1].Value.Replace(",", ""));
}
return 0;
}
The method connects to Google's server and retrieves a result page for our nickname. Then we check with regular expression " For the sake of simplicity, the code is not user-proof and could crash under exceptional conditions (e.g. negative length of nickname). Demo ProgramThe demo program consists of a simple Windows Form, and it uses this class to generate the nickname. The length of the result can be chosen from the Further StepsYou can try to improve this nickname generator to yield more realistic results. There is definitely room for improvement, because in human languages, some characters tend to be grouped with some others more often, and some groupings almost never occur (for example yqi, etc.). Also, some characters should be less likely to be the first character of a generated nickname (results that begin with q and y sound strange to me most of the time). I think that you can easily enhance results by adding some extra logic to the method that generates the nickname - like add some factors to each character that will specify how likely it is to appear and also how likely it is so the resulting word will begin or start with it. Further improvements - to make the results look like human language words - would possibly require knowledge of linguistics, which I am unfortunately no expert at. History
|
||||||||||||||||||||||||||||||||||||||||