Example #1
0
        // Summary:
        //     Generates the poem schema. Parses the syntax rules from the file
        //     and uses the repository objects to access the database and retrieve
        //     the schema by id.
        //
        // Parameters:
        //   schemaId:
        //     The id by which to find the poem schema.
        // Returns:
        //     The string containing the generated poem.
        public string GeneratePoem(int schemaId)
        {
            IPoemSyntaxRuleDocument doc = parser.Parse(config.SyntaxConfigFile);

            PoemSchema schema = poemRepository.GetSchemaById(schemaId);

            string generatedPoem = poemGenerator.GeneratePoem(schema, doc);

            return(generatedPoem);
        }
Example #2
0
        // Summary:
        //     Main method to generate a poem text by the poem schema and syntax rules.
        //
        // Parameters:
        //   schema:
        //     The schema object by which the poem should be constructed.
        //   document:
        //     The object which represents the syntax rules to build a poem.
        // Returns:
        //     The string containing the generated poem text. The line
        //     breaks separated by '\n' character.
        // Exceptions:
        //   System.ArgumentNullException:
        //     document is null.
        public string GeneratePoem(PoemSchema schema, IPoemSyntaxRuleDocument document)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document", "Argument cannot be null.");
            }

            List <string> poemLines = new List <string>();

            foreach (string line in schema.GetLines())
            {
                string generatedLine = this.GeneratePoemLine(line, document);

                poemLines.Add(generatedLine);
            }

            return(string.Join("\n", poemLines));
        }
Example #3
0
        // Summary:
        //     Generates a poem line by the poem schema and syntax rules.
        //
        // Parameters:
        //   schemaLine:
        //     The schema by which the poem line should be constructed.
        //   document:
        //     The object which represents the syntax rules to build a poem.
        // Returns:
        //     The string containing the generated poem line.
        // Exceptions:
        //   System.ArgumentNullException:
        //     document is null.
        //   PoemGenerationException:
        //     failed to convert string type to PartOfSpeech enum type.
        public virtual string GeneratePoemLine(string schemaLine, IPoemSyntaxRuleDocument doc)
        {
            if (doc == null)
            {
                throw new ArgumentNullException("document", "Argument cannot be null.");
            }

            int maxStep = config.MaxSyllablesCount;

            List <string> poemWords = new List <string>();
            List <string> nodes     = doc.GetRootElements();
            string        nextNode  = String.Empty;

            int i = 0;

            while (i < schemaLine.Length)
            {
                string foot     = String.Empty;
                int    footSize = default(int);

                List <Word> matchedWords = new List <Word>();

                do
                {
                    footSize = footRandomGenerator.Generate(1, maxStep + 1);

                    if (i + footSize >= schemaLine.Length)
                    {
                        foot = schemaLine.Substring(i);

                        //in case the last word in the line, retrive only the elements contaning END node
                        List <string> nodes1 = nodes.Intersect(doc.GetAllEndElements()).ToList();
                        nextNode = nodes1[nodeRandomGenerator.Generate(0, nodes1.Count())];
                    }
                    else
                    {
                        foot     = schemaLine.Substring(i, footSize);
                        nextNode = nodes[nodeRandomGenerator.Generate(0, nodes.Count())];
                    }

                    //in case one syllable it can be both stressed and unstressed
                    if (foot.Length == 1)
                    {
                        foot = "x,-";
                    }

                    PartOfSpeech partOfSpeech;

                    if (converter.ConvertToEnumType(nextNode, out partOfSpeech))
                    {
                        matchedWords = dictionaryRepository.GetWords(foot, partOfSpeech);
                    }
                    else
                    {
                        throw new PoemGenerationException("Unable to convert the part of speech from string type to enum type. String: " + nextNode);
                    }
                }while (matchedWords.Count() == 0);

                string w = matchedWords[wordRandomGenerator.Generate(0, matchedWords.Count())].Value;
                poemWords.Add(w);

                i    += footSize;
                nodes = doc.GetNextElements(nextNode);
            }

            return(string.Join(" ", poemWords));
        }
Example #4
0
 public override string GeneratePoemLine(string schemaLine, IPoemSyntaxRuleDocument document)
 {
     return("word1 word2 word3");
 }
        public void ParseTestRulesSuccessTest()
        {
            //Arrange
            string testFile  = "rule.txt";
            string testRules =
                "LINE: NOUN|PREPOSITION|PRONOUN\n" +
                "ADJECTIVE: NOUN|ADJECTIVE|$END\n" +
                "NOUN: VERB|PREPOSITION|$END\n" +
                "PRONOUN: NOUN|ADJECTIVE\n" +
                "VERB: PREPOSITION|PRONOUN|$END\n" +
                "PREPOSITION: NOUN|PRONOUN|ADJECTIVE|$END\n" +
                "CONJUCTION: NOUN";

            Dictionary <string, ISet <string> > rules = new Dictionary <string, ISet <string> >();

            rules["LINE"] = new HashSet <string>()
            {
                "NOUN",
                "PREPOSITION",
                "PRONOUN"
            };

            rules["ADJECTIVE"] = new HashSet <string>()
            {
                "NOUN",
                "ADJECTIVE",
                "$END"
            };

            rules["NOUN"] = new HashSet <string>()
            {
                "VERB",
                "PREPOSITION",
                "$END"
            };

            rules["PRONOUN"] = new HashSet <string>()
            {
                "NOUN",
                "ADJECTIVE"
            };

            rules["VERB"] = new HashSet <string>()
            {
                "PREPOSITION",
                "PRONOUN",
                "$END"
            };

            rules["PREPOSITION"] = new HashSet <string>()
            {
                "NOUN",
                "PRONOUN",
                "ADJECTIVE",
                "$END"
            };

            rules["CONJUCTION"] = new HashSet <string>()
            {
                "NOUN",
            };

            PoemSyntaxRuleDocument expectedDocument = new PoemSyntaxRuleDocument();

            expectedDocument.Rules = rules;

            Mock <FileReader> mockFileReader = new Mock <FileReader>();

            mockFileReader.Setup(m => m.ReadTextFromFile(testFile)).Returns(testRules);

            IPoemSyntaxRulesParser parser = new PoemSyntaxRulesParser(mockFileReader.Object);

            //Act
            IPoemSyntaxRuleDocument actualDocument = parser.Parse(testFile);

            //Assert
            Assert.AreEqual(expectedDocument, actualDocument);
        }