Esempio n. 1
0
        /// <summary>
        /// Returns a single name with no regard for how many times that name may have been generated before.
        /// </summary>
        /// <returns>A single Markov-generated name</returns>
        public string GenerateWord(int wordLength = 0)
        {
            string nextName = GetRandomKey();

            // get a random name from the input samples
            // then generate a word length to aim at
            int minLength  = tokenLength + nextName.Length;
            int nameLength = wordLength;

            if (wordLength <= 0)
            {
                wordLength = RandomNumber.Next(minLength + tokenLength, GetRandomSampleWord().Length + minLength);
                nameLength = RandomNumber.Next(minLength, wordLength);
            }

            // generate the next name: a random substring of the random sample name
            // then get a random next letter based on the previous ngram

            while (nextName.Length < nameLength)
            {
                string token = nextName.Substring(nextName.Length - tokenLength, tokenLength);
                if (MarkovChain.ContainsKey(token))
                {
                    nextName += NextLetter(token);
                }
                else
                {
                    break;
                }
            }

            nextName = textInfo.ToTitleCase(nextName.ToLower());

            return(nextName);
        }