예제 #1
0
파일: Generator.cs 프로젝트: ekolis/Markov
        /// <summary>
        /// Parses a chain from a sentence or phrase.
        /// Does not attempt to learn links from it (only learns tokens in isolation);
        /// learning links is done by calling <see cref="LearnToWrite(Chain)"/>.
        /// Parsing a chain without using spaces or by guessing at capitalization rules isn't really possible...
        /// Well maybe DromedaryCaseEnterpriseyJavaBeanAppletFactoryFactoryStuffImpl, but that will have to wait...
        /// </summary>
        /// <returns></returns>
        public Chain ParseSentence(string sentence, CultureInfo c = null)
        {
            // split on whitespace
            // note that we're leaving punctuation as part of the words
            // in addition to being lazy, it also makes for more realistic sentences when generating chains :D
            var words = sentence.Split(' ', '\r', '\n', '\t');

            // make a chain!
            var chain = new Chain(Capitalization.FirstToken, true, c);

            // start!
            chain.Append(BeginEndToken);

            // add ye tokens
            foreach (var word in words)
            {
                LearnToken(word);
                chain.Append(Lexicon[word]);
            }

            // end!
            chain.Append(BeginEndToken);

            // ITS OVAR
            return(chain);
        }
예제 #2
0
파일: Generator.cs 프로젝트: ekolis/Markov
        /// <summary>
        /// Builds a randomized chain using weighted values.
        /// </summary>
        /// <param name="cap"></param>
        /// <param name="spaces"></param>
        /// <param name="c"></param>
        /// <param name="preferStopAfter">How long until we ask to stop ASAP?</param>
        /// <param name="demandStopAfter">How long until we call an immediate halt to generation?</param>
        /// <returns></returns>
        public Chain WriteChain(Capitalization cap, bool spaces, int preferStopAfter, int demandStopAfter, CultureInfo c = null)
        {
            var  chain = new Chain(cap, spaces, c);
            var  cur   = BeginEndToken;
            bool runon = false;             // did we get cut off?

            do
            {
                if (!cur.Links.Any())
                {
                    throw new InvalidOperationException($"Token \"{cur}\" has no outgoing links.");
                }
                cur = cur.Links.PickWeighted(Rng);
                if (chain.Tokens.Count() >= demandStopAfter)
                {
                    cur   = BeginEndToken;
                    runon = true;
                }
                if (chain.Tokens.Count() >= preferStopAfter && cur.Links.ContainsKey(BeginEndToken))
                {
                    cur   = BeginEndToken;
                    runon = true;
                }
                chain.Append(cur);
            } while (cur != BeginEndToken);
            chain.IsRunon = runon;
            return(chain);
        }