Exemplo n.º 1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="input">The arguments to the lorem function</param>
        public LoremGenerator(string input)
        {
            var parser = new LoremGeneratorArgs(input);

            (int count, LoremUnit unit)result = parser.Parse();
            _count = result.count;
            _unit  = result.unit;
        }
Exemplo n.º 2
0
        public static string Generate(int count, LoremUnit unit)
        {
            var result     = new StringBuilder();
            var paragraphs = new List <Paragraph>();

            if (unit == LoremUnit.Word)
            {
                paragraphs.Add(new Paragraph(1, count));
            }
            else if (unit == LoremUnit.Sentence)
            {
                paragraphs.Add(new Paragraph(count, 0));
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    var sc = ThreadSafeRandom.Next(MinSentenceCount, MaxSentenceCount);
                    paragraphs.Add(new Paragraph(sc, 0));
                }
            }

            var first = true;

            foreach (var p in paragraphs)
            {
                if (!first)
                {
                    result.AppendLine();
                }

                foreach (var sentence in p)
                {
                    if (!first)
                    {
                        result.Append("  ");
                    }

                    first = false;
                    for (int i = 0; i < sentence.WordCount; i++)
                    {
                        var nextWord = Words[ThreadSafeRandom.Next(0, Words.Count)];
                        if (i == 0)
                        {
                            nextWord = nextWord.Substring(0, 1).ToUpperInvariant() + nextWord.Substring(1);
                        }

                        result.Append(nextWord);
                        if (i != sentence.WordCount - 1)
                        {
                            result.Append(" ");
                        }
                    }

                    if (unit != LoremUnit.Word)
                    {
                        result.Append(".");
                    }
                }
            }

            return(result.ToString());
        }