Exemple #1
0
        private static void Main(string[] args)
        {
            Console.WriteLine("First silly prototype");

            Console.WriteLine("\nBuild by hand");
            for (int i = 1; i <= 5; i++)
            {
                List <IWord> words = new List <IWord>();
                words.Add(Literal.GetLiteral("The"));
                words.Add(Literal.Space());
                words.Add(Noun.RandomNoun());
                words.Add(Literal.Space());
                words.Add(Literal.GetLiteral("is"));
                words.Add(Literal.Space());
                words.Add(Adjective.RandomAdjective());
                words.Add(Literal.Period());
                IPhrasePattern p = new PhrasePattern(words);
                Console.WriteLine(i + ": " + p.Value);
            }

            Console.WriteLine("\nBuild by pattern");
            for (int i = 1; i <= 5; i++)
            {
                IPhrasePattern p = new PhrasePattern("The {ADJ} {N} and the {ADJ} {ADJ} {N} were {ADJ}.");
                Console.WriteLine(i + ": " + p.Value);
            }

            Console.WriteLine("\nBuild by pattern");
            for (int i = 1; i <= 5; i++)
            {
                IPhrasePattern p = new PhrasePattern("{ADJ} {N} + {ADJ} {N} = {ADJ} {ADJ} {N}");
                Console.WriteLine(i + ": " + p.Value);
            }

            Console.WriteLine("\nBuild by dynamic pattern");
            for (int i = 1; i <= 5; i++)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("The");
                for (int j = 1; j <= i; j++)
                {
                    sb.Append(" {ADJ} {N} " + (j < i ? "and the" : ""));
                }
                sb.Append(" changed from {ADJ} to {ADJ} as if the {ADJ} {N} was {ADJ}.");
                IPhrasePattern p = new PhrasePattern(sb.ToString());
                Console.WriteLine(i + ": " + p.Value);
            }

            Console.WriteLine("\nDone");
            Console.ReadLine();
        }
        /// <summary>
        /// Convert string representation of pattern into a list of IWords
        /// </summary>
        /// <param name="pattern">String pattern</param>
        /// <returns>List of IWords</returns>
        protected List <IWord> ParsePattern(string pattern)
        {
            List <IWord> ret = new List <IWord>();

            //string[] units = pattern.Split(' ');
            string[] units = pattern.Split(new Char[] { ' ', '.', ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < units.Length; i++)
            {
                string unit = units[i];
                if (unit.StartsWith("{") && unit.EndsWith("}"))
                {
                    switch (unit)
                    {
                    case "{N}":
                        ret.Add(Noun.RandomNoun());
                        break;

                    case "{ADJ}":
                        ret.Add(Adjective.RandomAdjective());
                        break;

                    default:
                        ret.Add(Literal.GetLiteral(unit));     //Unable to parse unknown symbol
                        break;
                    }
                }
                else
                {
                    ret.Add(Literal.GetLiteral(unit));
                }
                if (i < units.Length - 1)
                {
                    ret.Add(Literal.Space()); //Add space unless this is the last unit
                }
                else
                {
                    ret.Add(Literal.Period());
                }
            }

            return(ret);
        }