示例#1
0
 /// <summary>
 /// Initializes a new <see cref="MarkovChainsNameGenerator"/> instance with specified
 /// <see cref="MarkovChainMap{T}"/>, minimum frequency, minimum and maximum lengths, with specified capitalization
 /// and whitespace-handling modes, with specified <see cref="Random"/> used to generate outputs, and eventually a
 /// list of forbidden and/or already-produced outputs.
 /// </summary>
 /// <param name="map"><see cref="MarkovChainMap{T}"/> of strings used to produce outputs.</param>
 /// <param name="random"><see cref="Random"/> which will be used for generating outputs. If null, a new
 /// <see cref="Random"/> will be created automatically.</param>
 /// <param name="minFrequency">The minimum frequency considered while producing random outputs. Please see
 /// <see cref="Sieve{T}.MinFrequency"/>.</param>
 /// <param name="minLength">The minimum length of produced outputs. Will be lower-bounded to one, eventually.
 /// </param>
 /// <param name="maxLength">The maximum length of produced outputs. Will be lower-bounded to
 /// <paramref name="minLength"/>, eventually.</param>
 /// <param name="capitalize">Whether or not outputs will be capitalized.</param>
 /// <param name="skipWhitespace">Whether or not whitespaces will be ignored while parsing the sample file into a
 /// <see cref="MarkovChainMap{T}"/>. Please see <see cref="MarkovChainStringMapBuilder.SkipWhitespace"/>.</param>
 /// <param name="blacklistedNames">A list of forbiden and/or already produced outputs. This list will be qualified
 /// further while new outputs are generated.</param>
 /// <exception cref="ArgumentNullException"><paramref name="map"/> is null.</exception>
 public MarkovChainsNameGenerator(MarkovChainMap <string> map, Random random = null,
                                  int minFrequency = DefaultMinFrequency, int minLength = DefaultMinLength, int maxLength             = DefaultMaxLength,
                                  bool capitalize  = true, bool skipWhitespace          = true, IEnumerable <string> blacklistedNames = null)
     : this(random, map.Depth, minFrequency, minLength, maxLength, capitalize, skipWhitespace, blacklistedNames)
 {
     _map = map;
 }
示例#2
0
        /// <summary>
        /// Merges the contents of a specified <see cref="MarkovChainMap{T}"/> with those of this one.
        /// </summary>
        /// <param name="other"><see cref="MarkovChainMap{T}"/> whose contents to merge to those of this map.</param>
        /// <exception cref="NullReferenceException"><paramref name="other"/> is null.</exception>
        public void Merge(MarkovChainMap <T> other)
        {
            T key;

            foreach (var pair in other)
            {
                key = pair.Key;
                EventuallyAdd(key, new FrequencyMap <T>());
                this[key].Merge(pair.Value);
            }
        }
示例#3
0
        /// <summary>
        /// Builds a new <see cref="MarkovChainMap{T}"/> from specified list of samples, and returns the result.
        /// </summary>
        /// <param name="samples">A collection of <typeparamref name="T"/> samples used to build the
        /// <see cref="MarkovChainMap{T}"/>.</param>
        /// <returns>A new <see cref="MarkovChainMap{T}"/> built from <paramref name="samples"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="samples"/> is null.</exception>
        public MarkovChainMap <T> Train(IEnumerable <T> samples)
        {
            MarkovChainMap <T> map = new MarkovChainMap <T>(_depth);

            foreach (T sample in samples)
            {
                if (sample != null)
                {
                    Train(map, sample);
                }
            }
            return(map);
        }
示例#4
0
        /// <summary>
        /// Qualifies specified map with new data from specified sample.
        /// </summary>
        /// <param name="map"><see cref="MarkovChainMap{T}"/> to qualify.</param>
        /// <param name="sample"><typeparamref name="T"/> value to train <paramref name="map"/> with.</param>
        protected virtual void Train(MarkovChainMap <T> map, T sample)
        {
            T[] array  = _partition(sample);
            int length = array.Length;
            int depth  = 1;
            FrequencyMap <T> frequencyMap;
            StateFrequency   stateFrequency;
            T   key, value;
            int position, current, limit;

            while (depth <= _depth)
            {
                if (depth < length)
                {
                    limit = length - depth;
                    for (position = 0; (current = position + depth) < length; position++)
                    {
                        key   = _aggregate(array, position, depth);
                        value = array[position + depth];
                        map.EventuallyAdd(key, new FrequencyMap <T>());
                        frequencyMap = map[key];
                        frequencyMap.EventuallyAdd(value, new StateFrequency());
                        stateFrequency = frequencyMap[value];
                        if (position == 0)
                        {
                            stateFrequency.IncrementFirst();
                        }
                        else if (current < limit)
                        {
                            stateFrequency.IncrementInner();
                        }
                        else
                        {
                            stateFrequency.IncrementLast();
                        }
                    }
                }
                depth++;
            }
        }
示例#5
0
 /// <summary>
 /// Builds a new <see cref="MarkovChainMap{T}"/> from specified samples file. Please see
 /// <see cref="MarkovChainStringMapBuilder.TrainFromFile(string, string)"/>.
 /// </summary>
 /// <param name="path">Path to the file containing the samples.</param>
 /// <param name="commentSpecifier">An eventual comment specifier. Lines in the file beginning with this value
 /// will not be parsed.</param>
 public void TrainMapBuilder(string path, string commentSpecifier = "%")
 {
     _map = _mapBuilder.TrainFromFile(path, commentSpecifier);
 }