Esempio n. 1
0
        /// <summary>
        /// Outputs a random phrase using internally created <see cref="MarkovChainMap{T}"/>.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Internal <see cref="MarkovChainMap{T}"/> is null. Please call
        /// <see cref="TrainMapBuilder(string, string)"/> method before using <see cref="GetName"/> method.</exception>
        public string GetPhrase()
        {
            StringBuilder sb = _sb;

            sb.Clear();
            List <string> startLetters = _map.GetStartStates();
            List <string> nextCharacters = _map.GetNextStates();
            int           startLetterCount = startLetters.Count;
            int           nextCharacterCount = nextCharacters.Count;
            int           totalLength, currentLength, selectionLength;
            string        initial, key, result;

            do
            {
                totalLength = (int)(_minLength + RangeLength * _random.Next() / Maximum);
                initial     = startLetters[(int)(startLetterCount * (double)_random.Next() / Maximum)];
                sb.Append(initial);
                currentLength = initial.Length;
                while (currentLength < totalLength)
                {
                    selectionLength = _random.Next(1, Math.Min(currentLength, _mapDepth) + 1);
                    do
                    {
                        key = sb.ToString().Substring(currentLength - selectionLength, selectionLength);
                    } while (!_map.ContainsKey(key) && (--selectionLength > 0));
                    if (selectionLength == 0)
                    {
                        key = nextCharacters[(int)(nextCharacterCount * (double)_random.Next() / Maximum)];
                    }
                    sb.Append(_sieve.GetValue(_map[key],
                                              (currentLength == 1) ? Position.First :
                                              (currentLength == totalLength - 1) ? Position.Last
                                                                                                                                          : Position.Inner));
                    currentLength++;
                }
                result = sb.ToString();
                if (_capitalize)
                {
                    result = MakeProperCase(result);
                }
            } while (_blacklistedNames.Contains(result));
            _blacklistedNames.Add(result);

            // TODO: Make this work on words isntead of characters
            // For each phrase we return we want to leave the <NAME> tokens in, replacing those is up to the caller
            return("<NAME>'s Artifact of <NAME>");
        }
Esempio n. 2
0
        /// <summary>
        /// Outputs a random name using internally created <see cref="MarkovChainMap{T}"/>.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Internal <see cref="MarkovChainMap{T}"/> is null. Please call
        /// <see cref="TrainMapBuilder(string, string)"/> method before using <see cref="GetName"/> method.</exception>
        public string GetName()
        {
            StringBuilder sb = _sb;

            sb.Clear();
            List <string> startLetters = _map.GetStartStates();
            List <string> nextCharacters = _map.GetNextStates();
            int           startLetterCount = startLetters.Count;
            int           nextCharacterCount = nextCharacters.Count;
            int           totalLength, currentLength, selectionLength;
            string        initial, key, result;

            do
            {
                totalLength = (int)(_minLength + RangeLength * _random.Next() / Maximum);
                initial     = startLetters[(int)(startLetterCount * (double)_random.Next() / Maximum)];
                sb.Append(initial);
                currentLength = initial.Length;
                while (currentLength < totalLength)
                {
                    selectionLength = _random.Next(1, Math.Min(currentLength, _mapDepth) + 1);
                    do
                    {
                        key = sb.ToString().Substring(currentLength - selectionLength, selectionLength);
                    } while (!_map.ContainsKey(key) && (--selectionLength > 0));
                    if (selectionLength == 0)
                    {
                        key = nextCharacters[(int)(nextCharacterCount * (double)_random.Next() / Maximum)];
                    }
                    sb.Append(_sieve.GetValue(_map[key],
                                              (currentLength == 1) ? Position.First :
                                              (currentLength == totalLength - 1) ? Position.Last
                                                                                                                                          : Position.Inner));
                    currentLength++;
                }
                result = sb.ToString();
                if (_capitalize)
                {
                    result = MakeProperCase(result);
                }
            } while (_blacklistedNames.Contains(result));
            _blacklistedNames.Add(result);
            return(result);
        }