public void ParseQuestion(Question question)
 {
     if (question.IsParsable)
     {
         PhraseParser parser = new PhraseParser(m_keyTermsTable, m_phraseSubstitutions,
                                                m_questionWordsLookupTable, question, GetOrCreatePart);
         foreach (ParsedPart part in parser.Parse())
         {
             question.ParsedParts.Add(part);
         }
     }
 }
Example #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the questions/phrases
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private IEnumerable <TranslatablePhrase> GetPhrases()
        {
            var processedCategories = new Dictionary <string, int>();

            for (var iSection = 0; iSection < m_sections.Items.Length; iSection++)
            {
                var section = m_sections.Items[iSection];
                Debug.Assert(section.Categories.Distinct(CategoryComparer.AreSame).Count() == section.Categories.Length,
                             "Section contains a repeated category.");
                foreach (var category in section.Categories)
                {
                    TranslatablePhrase phrase;

                    // In the event of an unnamed non-overview category (only possible in tests),
                    // treat it as "Details" (or whatever Category 1 happens to be).
                    var categoryIndex = category.IsOverview ? 0 : 1;
                    if (category.Type != null)
                    {
                        var lcCategory = category.Type.ToLowerInvariant();
                        if (!processedCategories.TryGetValue(lcCategory, out categoryIndex))
                        {
                            phrase = new TranslatablePhrase(new SimpleQuestionKey(category.Type), -1, -1, processedCategories.Count);
                            phrase.m_parts.Add(m_manager.GetOrCreatePart(PhraseParser.GetWordsInString(lcCategory), phrase, false));
                            yield return(phrase);

                            // 0 is reserved for "overview", so we can't use that for non-overview categories.
                            categoryIndex = category.IsOverview ? 0 : Math.Max(1, processedCategories.Count);
                            processedCategories[lcCategory] = categoryIndex;
                        }
                    }

                    for (int iQuestion = 0; iQuestion < category.Questions.Count; iQuestion++)
                    {
                        Question q = category.Questions[iQuestion];
                        if (q.ScriptureReference == null)
                        {
                            q.ScriptureReference = section.ScriptureReference;
                            q.StartRef           = section.StartRef;
                            q.EndRef             = section.EndRef;
                        }

                        Debug.Assert(categoryIndex >= 0);
                        phrase = new TranslatablePhrase(q, iSection, categoryIndex, iQuestion);
                        if (!phrase.IsExcluded)
                        {
                            InitializePhraseParts(phrase);
                        }
                        yield return(phrase);
                    }
                }
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Gets the questions/phrases
        /// </summary>
        /// ------------------------------------------------------------------------------------
        private IEnumerable <TranslatablePhrase> GetPhrases()
        {
            HashSet <string>   processedCategories = new HashSet <string>();
            TranslatablePhrase phrase;
            int categoryType;

            foreach (Section section in m_sections.Items)
            {
                for (int iCat = 0; iCat < section.Categories.Length; iCat++)
                {
                    Category category = section.Categories[iCat];
                    categoryType = iCat;
                    if (iCat == 0 && !category.IsOverview)                     // 0 is reserved for "overview", so we can't use that for non-overview categories.
                    {
                        categoryType++;
                    }

                    if (category.Type != null)
                    {
                        string lcCategory = category.Type.ToLowerInvariant();
                        if (!processedCategories.Contains(lcCategory))
                        {
                            phrase = new TranslatablePhrase(new SimpleQuestionKey(category.Type), -1, processedCategories.Count);
                            phrase.m_parts.Add(m_manager.GetOrCreatePart(PhraseParser.GetWordsInString(lcCategory), phrase, false));
                            yield return(phrase);

                            processedCategories.Add(lcCategory);
                        }
                    }

                    for (int iQuestion = 0; iQuestion < category.Questions.Count; iQuestion++)
                    {
                        Question q = category.Questions[iQuestion];
                        if (q.ScriptureReference == null)
                        {
                            q.ScriptureReference = section.ScriptureReference;
                            q.StartRef           = section.StartRef;
                            q.EndRef             = section.EndRef;
                        }
                        phrase = new TranslatablePhrase(q, categoryType, iQuestion + 1);
                        if (!phrase.IsExcluded)
                        {
                            InitializePhraseParts(phrase);
                        }
                        yield return(phrase);
                    }
                }
            }
        }
 public bool ParseNewOrModifiedQuestion(Question question, Action <KeyTermMatch> processKeyTermMatch)
 {
     if (question.IsParsable)
     {
         PhraseParser parser = new PhraseParser(m_keyTermsTable, m_phraseSubstitutions,
                                                m_questionWordsLookupTable, question, GetOrCreatePart);
         foreach (ParsedPart part in parser.Parse())
         {
             question.ParsedParts.Add(part);
         }
         foreach (var match in parser.KeyTermsUsedForPhrase)
         {
             processKeyTermMatch(match);
         }
         return(true);
     }
     return(false);
 }
Example #5
0
        public void EnumeratePhrases_EnsureSharedPartsAreHookedUpCorrectly()
        {
            ParsedQuestions pq = new ParsedQuestions();

            pq.KeyTerms = new [] {
                new KeyTermMatchSurrogate("luke", "lucas"),
                new KeyTermMatchSurrogate("jesus", "jesu"),
                new KeyTermMatchSurrogate("disciple", "dicipulo")
            };
            QuestionSections qs = pq.Sections = new QuestionSections();

            qs.Items = new Section[2];
            int iS = 0;

            qs.Items[iS] = MasterQuestionParserTests.CreateSection("ACT 1.1-5", "Acts 1:1-5 Introduction to the book.", 44001001,
                                                                   44001005, 2, 1);
            int      iC = 0;
            Question q  = qs.Items[iS].Categories[iC].Questions[0];

            q.Text = "What information did Luke give?";
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("what information did")));
            q.ParsedParts.Add(new ParsedPart(pq.KeyTerms[0]));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("give")));
            q      = qs.Items[iS].Categories[iC].Questions[1];
            q.Text = "What is a disciple of Jesus?";
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("what is a")));
            q.ParsedParts.Add(new ParsedPart(pq.KeyTerms[2]));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("of")));
            q.ParsedParts.Add(new ParsedPart(pq.KeyTerms[1]));

            iC     = 1;
            q      = qs.Items[iS].Categories[iC].Questions[0];
            q.Text = "What is a book?";
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("what is a")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("book")));

            iS           = 1;
            qs.Items[iS] = MasterQuestionParserTests.CreateSection("ACT 1.6-10", "Acts 1:6-10 The continuing saga.", 44001006, 44001010, 1, 2);
            iC           = 0;
            q            = qs.Items[iS].Categories[iC].Questions[0];
            q.Text       = "Which of the disciples wanted to give away land?";
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("which")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("of")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("the")));
            q.ParsedParts.Add(new ParsedPart(pq.KeyTerms[2]));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("wanted to")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("give")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("away land")));

            iC = 1;
            q  = qs.Items[iS].Categories[iC].Questions[0];
            q.ScriptureReference = "ACT 1.6";
            q.StartRef           = 44001006;
            q.EndRef             = 44001006;
            q.Text = "What information did jesus want to give the disciples?";
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("what information did")));
            q.ParsedParts.Add(new ParsedPart(pq.KeyTerms[1]));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("want to")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("give")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("the")));
            q.ParsedParts.Add(new ParsedPart(pq.KeyTerms[2]));

            q = qs.Items[iS].Categories[iC].Questions[1];
            q.ScriptureReference = "ACT 1.7";
            q.StartRef           = 44001007;
            q.EndRef             = 44001007;
            q.Text = "Do you want to read this book again?";
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("do you")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("want to")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("read this")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("book")));
            q.ParsedParts.Add(new ParsedPart(PhraseParser.GetWordsInString("again")));

            pq.TranslatableParts = new []
            {
                "what information did",
                "give",
                "what is a",
                "of",
                "book",
                "which",
                "the",
                "wanted to",
                "away land",
                "do you",
                "want to",
                "read this",
                "again"
            };
            QuestionProvider          qp      = new QuestionProvider(pq);
            List <TranslatablePhrase> phrases = qp.ToList();

            Assert.AreEqual(8, phrases.Count);

            TranslatablePhrase phrase = phrases[0];

            Assert.AreEqual("Overview", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "overview", 1);

            phrase = phrases[1];
            Assert.AreEqual("What information did Luke give?", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "what information did", 2, "give", 3);

            phrase = phrases[2];
            Assert.AreEqual("What is a disciple of Jesus?", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "what is a", 2, "of", 2);

            phrase = phrases[3];
            Assert.AreEqual("Details", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "details", 1);

            phrase = phrases[4];
            Assert.AreEqual("What is a book?", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "what is a", 2, "book", 2);

            phrase = phrases[5];
            Assert.AreEqual("Which of the disciples wanted to give away land?", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "which", 1, "of", 2, "the", 2, "wanted to", 1, "give", 3, "away land", 1);

            phrase = phrases[6];
            Assert.AreEqual("What information did jesus want to give the disciples?", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "what information did", 2, "want to", 2, "give", 3, "the", 2);

            phrase = phrases[7];
            Assert.AreEqual("Do you want to read this book again?", phrase.PhraseInUse);
            VerifyTranslatablePhrase(phrase, "do you", 1, "want to", 2, "read this", 1, "book", 2, "again", 1);
        }