コード例 #1
0
        /// <summary>
        /// Generate fixed number of non punctuation words
        /// All words will be lower case
        /// </summary>
        /// <param name="numWords"></param>
        /// <returns></returns>
        public string GenerateWords(int numWords)
        {
            if (numWords < 1)
            {
                throw new Exceptions.InvalidArguments("Number of words generated must be positive");
            }
            StringBuilder sb = new StringBuilder();

            if (_wordGenerator.SubchainsInitialized)
            {
                _wordGenerator.ResetReadOnly();
            }
            else
            {
                _wordGenerator.ResetSubchains();
            }

            for (int i = 0; i < numWords; i++)
            {
                if (i != 0)
                {
                    sb.Append(" ");
                }

                sb.Append(_wordGenerator.GetNextWord(w => !IsPunctuation(w), Defs.MAX_ITERATIONS));
            }
            return(sb.ToString());
        }
コード例 #2
0
        /// <summary>
        /// Generate a new sentece that contains a minimum number of non punctuation words
        /// This minimum number of words is not guaranteed, but it is a best try
        /// </summary>
        /// <param name="minLength">Minimum length of sentence.  Use 0 to indicate
        /// no minimum</param>
        /// <returns></returns>
        public string GenerateSentence(int minLength)
        {
            if (minLength < 0)
            {
                throw new Exceptions.InvalidArguments("Number of words generated must be positive");
            }

            if (minLength > Defs.MAX_SENTENCE_LENGTH)
            {
                throw new Exceptions.InvalidArguments("Minimum sentence length greater than default maximum of " +
                                                      Defs.MAX_SENTENCE_LENGTH);
            }

            StringBuilder sb = new StringBuilder();

            //Word generator should reset to beginning of a sentence if needed
            ChainCondition sentenceStartCondition = new ChainCondition(
                ck => IsSentenceEnd(ck.Words[ck.Words.Length - 1]) ||
                ck.Words[ck.Words.Length - 1] == null, w => !IsPunctuation(w));

            //redundant for clarity
            if (!_sentenceGenerator.SubchainsInitialized)
            {
                _sentenceGenerator.ResetSubchains(sentenceStartCondition, null);
            }
            else if (IsSentenceEnd(_sentenceGenerator.CurrentWord))
            {
                //We are already at the beginning of a new sentence
                //This is to try to optimize perfomance so that we don't do a reset more than necessary
                //A reset is an expensive operation as all chains need to be parsed
                _sentenceGenerator.GetNextWord(sentenceStartCondition.WordCondition, Defs.MAX_ITERATIONS);
            }

            if (!(sentenceStartCondition.KeyCondition(_sentenceGenerator.CurrKey) &&
                  (sentenceStartCondition.WordCondition(_sentenceGenerator.CurrentWord))))
            {
                _sentenceGenerator.ResetReadOnly();
            }

            int countWords;

            for (countWords = 0; countWords < Defs.MAX_SENTENCE_LENGTH; countWords++)
            {
                string nextWord = _sentenceGenerator.CurrentWord;

                if (!(countWords == 0 || IsPunctuation(nextWord)))
                {
                    //prepend space if not first word or punctuation
                    sb.Append(" ");
                    sb.Append(nextWord);
                }
                else if (countWords == 0)
                {
                    //capitalize first word
                    if (!string.IsNullOrEmpty(nextWord))
                    {
                        sb.Append(nextWord.Capitalize());
                    }
                }
                else
                {
                    sb.Append(nextWord);
                }

                //If we get to sentence end, we are done
                if (IsSentenceEnd(nextWord))
                {
                    break;
                }

                if (countWords < minLength)
                {
                    _sentenceGenerator.GetNextWord(w => !IsSentenceEnd(w), Defs.MAX_ITERATIONS);
                }
                else
                {
                    _sentenceGenerator.GetNextWord();
                }
            }

            if (countWords == Defs.MAX_SENTENCE_LENGTH)
            {
                //shouldn't get here, but if we do, put any random sentence end
                sb.Append(SentenceEnd.FirstOrDefault());
            }
            return(sb.ToString());
        }