コード例 #1
0
        // different options for the same field
        public virtual void TestMixedOptions()
        {
            int numFields  = TestUtil.NextInt(Random(), 1, 3);
            var docFactory = new RandomDocumentFactory(this, numFields, 10);

            foreach (var options1 in ValidOptions())
            {
                foreach (var options2 in ValidOptions())
                {
                    if (options1 == options2)
                    {
                        continue;
                    }
                    using (Directory dir = NewDirectory())
                    {
                        using (var writer = new RandomIndexWriter(Random(), dir))
                        {
                            RandomDocument doc1 = docFactory.NewDocument(numFields, 20, options1);
                            RandomDocument doc2 = docFactory.NewDocument(numFields, 20, options2);
                            writer.AddDocument(AddId(doc1.ToDocument(), "1"));
                            writer.AddDocument(AddId(doc2.ToDocument(), "2"));
                            using (IndexReader reader = writer.Reader)
                            {
                                int doc1ID = DocID(reader, "1");
                                AssertEquals(doc1, reader.GetTermVectors(doc1ID));
                                int doc2ID = DocID(reader, "2");
                                AssertEquals(doc2, reader.GetTermVectors(doc2ID));
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
        public virtual void TestRandom()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int numDocs = AtLeast(100);

            RandomDocument[] docs = new RandomDocument[numDocs];
            for (int i = 0; i < numDocs; ++i)
            {
                docs[i] = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 3), TestUtil.NextInt32(Random, 10, 50), RandomOptions());
            }
            using (Directory dir = NewDirectory())
                using (RandomIndexWriter writer = new RandomIndexWriter(Random, dir, ClassEnvRule.similarity, ClassEnvRule.timeZone))
                {
                    for (int i = 0; i < numDocs; ++i)
                    {
                        writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
                    }
                    using (IndexReader reader = writer.GetReader())
                    {
                        for (int i = 0; i < numDocs; ++i)
                        {
                            int docID = DocID(reader, "" + i);
                            AssertEquals(docs[i], reader.GetTermVectors(docID));
                        }
                    } // reader.Dispose();
                }// writer.Dispose();, dir.Dispose();
        }
コード例 #3
0
        public void RandomDocumentTest(int minNumParagraphs, int maxNumParagraphs, int minNumSentencesPerParagraph, int maxNumSentencesPerParagraph, string documentSubject)
        {
            RandomNumber   rn            = new RandomNumber();
            RandomDocument rd            = new RandomDocument();
            int            numParagraphs = 0;
            string         doc           = string.Empty;

            try
            {
                _msg.Length = 0;
                _msg.Append("RandomDocumentTest started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                _str.Length = 0;

                numParagraphs = rn.GenerateRandomInt(minNumParagraphs, maxNumParagraphs);

                doc = rd.GenerateDocument(numParagraphs, minNumSentencesPerParagraph, maxNumSentencesPerParagraph, documentSubject);
                _str.Append(doc);
                Program._messageLog.WriteLine(_str.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... RandomDocumentTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #4
0
        public virtual void TestRandom()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int numDocs = AtLeast(100);

            RandomDocument[] docs = new RandomDocument[numDocs];
            for (int i = 0; i < numDocs; ++i)
            {
                docs[i] = docFactory.NewDocument(TestUtil.NextInt(Random(), 1, 3), TestUtil.NextInt(Random(), 10, 50), RandomOptions());
            }
            Directory         dir    = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), dir);

            for (int i = 0; i < numDocs; ++i)
            {
                writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
            }
            IndexReader reader = writer.Reader;

            for (int i = 0; i < numDocs; ++i)
            {
                int docID = DocID(reader, "" + i);
                AssertEquals(docs[i], reader.GetTermVectors(docID));
            }
            reader.Dispose();
            writer.Dispose();
            dir.Dispose();
        }
コード例 #5
0
        public void GenerateRandomBook()
        {
            RandomDocument randDoc = new RandomDocument();
            int            numChaptersToGenerate    = 3;
            int            numParagraphsPerChapter  = 10;
            int            numSentencesPerParagraph = 5;

            string[] chapterTitles = null;

            try
            {
                _msg.Length = 0;
                _msg.Append("GenerateRandomBook started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                numChaptersToGenerate    = Convert.ToInt32(_frm.txtNumChaptersToOutput.Text);
                numParagraphsPerChapter  = Convert.ToInt32(_frm.txtNumParagraphsToOutput.Text);
                numSentencesPerParagraph = Convert.ToInt32(_frm.txtNumSentencesToOutput.Text);

                if (numParagraphsPerChapter < 5)
                {
                    numParagraphsPerChapter = 5;
                }
                if (numSentencesPerParagraph < 5)
                {
                    numSentencesPerParagraph = 5;
                }

                chapterTitles    = new string[2];
                chapterTitles[0] = "First Chapter Is This";
                chapterTitles[1] = "The Second Chapter Makes an Appearance";

                string book = randDoc.GenerateBook("Test Program Book", "*** THE END ***", numChaptersToGenerate, chapterTitles, numParagraphsPerChapter - 3, numParagraphsPerChapter + 3, numSentencesPerParagraph - 2, numSentencesPerParagraph + 3);

                _msg.Length = 0;
                _msg.Append(book);
                Program._messageLog.WriteLine(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... GenerateRandomBook finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #6
0
        public virtual void TestLotsOfFields()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5000, 10);

            foreach (Options options in ValidOptions())
            {
                using Directory dir            = NewDirectory();
                using RandomIndexWriter writer = new RandomIndexWriter(Random, dir);
                RandomDocument doc = docFactory.NewDocument(AtLeast(100), 5, options);
                writer.AddDocument(doc.ToDocument());
                using IndexReader reader = writer.GetReader();
                AssertEquals(doc, reader.GetTermVectors(0));
            }
        }
コード例 #7
0
        public virtual void TestMerge()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int            numDocs           = AtLeast(100);
            int            numDeletes        = Random.Next(numDocs);
            HashSet <int?> deletes           = new HashSet <int?>();

            while (deletes.Count < numDeletes)
            {
                deletes.Add(Random.Next(numDocs));
            }
            foreach (Options options in ValidOptions())
            {
                RandomDocument[] docs = new RandomDocument[numDocs];
                for (int i = 0; i < numDocs; ++i)
                {
                    docs[i] = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 3), AtLeast(10), options);
                }
                using (Directory dir = NewDirectory())
                    using (RandomIndexWriter writer = new RandomIndexWriter(Random, dir, ClassEnvRule.similarity, ClassEnvRule.timeZone))
                    {
                        for (int i = 0; i < numDocs; ++i)
                        {
                            writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
                            if (Rarely())
                            {
                                writer.Commit();
                            }
                        }
                        foreach (int delete in deletes)
                        {
                            writer.DeleteDocuments(new Term("id", "" + delete));
                        }
                        // merge with deletes
                        writer.ForceMerge(1);
                        using (IndexReader reader = writer.GetReader())
                        {
                            for (int i = 0; i < numDocs; ++i)
                            {
                                if (!deletes.Contains(i))
                                {
                                    int docID = DocID(reader, "" + i);
                                    AssertEquals(docs[i], reader.GetTermVectors(docID));
                                }
                            }
                        } // reader.Dispose();
                    }// writer.Dispose();, dir.Dispose();
            }
        }
コード例 #8
0
        public void RandomBookTest(string bookTitle, int numChapters, string pChapterHeadings,
                                   int minNumParagraphsPerChapter, int maxNumParagraphsPerChapter,
                                   int minNumSentencesPerParagraph, int maxNumSentencesPerParagraph)
        {
            RandomNumber   rn         = new RandomNumber();
            RandomDocument rd         = new RandomDocument();
            string         theEndText = "Finis ... The End";
            string         book       = string.Empty;

            string[] chapterHeadings = null;

            try
            {
                _msg.Length = 0;
                _msg.Append("RandomBookTest started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                if (pChapterHeadings.Trim().Length > 0)
                {
                    chapterHeadings = pChapterHeadings.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                }

                if (chapterHeadings == null)
                {
                    book = rd.GenerateBook(bookTitle, numChapters, minNumParagraphsPerChapter, maxNumParagraphsPerChapter, minNumSentencesPerParagraph, maxNumSentencesPerParagraph);
                }
                else
                {
                    book = rd.GenerateBook(bookTitle, theEndText, numChapters, chapterHeadings,
                                           minNumParagraphsPerChapter, maxNumParagraphsPerChapter, minNumSentencesPerParagraph, maxNumSentencesPerParagraph);
                }

                Program._messageLog.WriteLine(book);
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... RandomBookTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #9
0
        // only one doc with vectors
        public virtual void TestRareVectors()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 10, 20);

            foreach (Options options in ValidOptions())
            {
                int      numDocs        = AtLeast(200);
                int      docWithVectors = Random.Next(numDocs);
                Document emptyDoc       = new Document();
                using (Directory dir = NewDirectory())
                    using (RandomIndexWriter writer = new RandomIndexWriter(Random, dir, ClassEnvRule.similarity, ClassEnvRule.timeZone))
                    {
                        RandomDocument doc = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 3), 20, options);
                        for (int i = 0; i < numDocs; ++i)
                        {
                            if (i == docWithVectors)
                            {
                                writer.AddDocument(AddId(doc.ToDocument(), "42"));
                            }
                            else
                            {
                                writer.AddDocument(emptyDoc);
                            }
                        }
                        using (IndexReader reader = writer.GetReader())
                        {
                            int docWithVectorsID = DocID(reader, "42");
                            for (int i = 0; i < 10; ++i)
                            {
                                int    docID  = Random.Next(numDocs);
                                Fields fields = reader.GetTermVectors(docID);
                                if (docID == docWithVectorsID)
                                {
                                    AssertEquals(doc, fields);
                                }
                                else
                                {
                                    Assert.IsNull(fields);
                                }
                            }
                            Fields fields_ = reader.GetTermVectors(docWithVectorsID);
                            AssertEquals(doc, fields_);
                        } // reader.Dispose();
                    }// writer.Dispose();, dir.Dispose();
            }
        }
コード例 #10
0
        // run random tests from different threads to make sure the per-thread clones
        // don't share mutable data
        public virtual void TestClone()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int numDocs = AtLeast(100);

            foreach (Options options in ValidOptions())
            {
                RandomDocument[] docs = new RandomDocument[numDocs];
                for (int i = 0; i < numDocs; ++i)
                {
                    docs[i] = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 3), AtLeast(10), options);
                }
                AtomicObject <Exception> exception = new AtomicObject <Exception>();
                using (Directory dir = NewDirectory())
                    using (RandomIndexWriter writer = new RandomIndexWriter(Random, dir, ClassEnvRule.similarity, ClassEnvRule.timeZone))
                    {
                        for (int i = 0; i < numDocs; ++i)
                        {
                            writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
                        }
                        using (IndexReader reader = writer.GetReader())
                        {
                            for (int i = 0; i < numDocs; ++i)
                            {
                                int docID = DocID(reader, "" + i);
                                AssertEquals(docs[i], reader.GetTermVectors(docID));
                            }

                            ThreadClass[] threads = new ThreadClass[2];
                            for (int i = 0; i < threads.Length; ++i)
                            {
                                threads[i] = new ThreadAnonymousInnerClassHelper(this, numDocs, docs, reader, exception, i);
                            }
                            foreach (ThreadClass thread in threads)
                            {
                                thread.Start();
                            }
                            foreach (ThreadClass thread in threads)
                            {
                                thread.Join();
                            }
                        } // reader.Dispose();
                    }// writer.Dispose();, dir.Dispose();
                Assert.IsNull(exception.Value, "One thread threw an exception");
            }
        }
コード例 #11
0
        public virtual void TestLotsOfFields()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 500, 10);

            foreach (Options options in ValidOptions())
            {
                Directory         dir    = NewDirectory();
                RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, ClassEnvRule.Similarity, ClassEnvRule.TimeZone);
                RandomDocument    doc    = docFactory.NewDocument(AtLeast(100), 5, options);
                writer.AddDocument(doc.ToDocument());
                IndexReader reader = writer.Reader;
                AssertEquals(doc, reader.GetTermVectors(0));
                reader.Dispose();
                writer.Dispose();
                dir.Dispose();
            }
        }
コード例 #12
0
        public void GenerateRandomChapter()
        {
            RandomDocument randDoc = new RandomDocument();
            int            numParagraphsToGenerate  = 3;
            int            numSentencesPerParagraph = 10;

            try
            {
                _msg.Length = 0;
                _msg.Append("GenerateRandomChapter started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                numParagraphsToGenerate  = Convert.ToInt32(_frm.txtNumParagraphsToOutput.Text);
                numSentencesPerParagraph = Convert.ToInt32(_frm.txtNumSentencesToOutput.Text);

                if (numSentencesPerParagraph < 5)
                {
                    numSentencesPerParagraph = 5;
                }


                string chapter = randDoc.GenerateChapter("Test Program Chapter", numParagraphsToGenerate, numSentencesPerParagraph - 2, numSentencesPerParagraph + 3);



                _msg.Length = 0;
                _msg.Append(chapter);
                Program._messageLog.WriteLine(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... GenerateRandomChapter finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #13
0
        public void RandomParagraphsTest(int minNumParagraphs, int maxNumParagraphs, int minNumSentencesPerParagraph, int maxNumSentencesPerParagraph)
        {
            RandomNumber   rn            = new RandomNumber();
            RandomDocument rd            = new RandomDocument();
            int            numParagraphs = 0;
            int            numSentences  = 0;
            string         paragraph     = string.Empty;

            try
            {
                _msg.Length = 0;
                _msg.Append("RandomParagraphsTest started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                _str.Length = 0;

                numParagraphs = rn.GenerateRandomInt(minNumParagraphs, maxNumParagraphs);

                for (int p = 0; p < numParagraphs; p++)
                {
                    numSentences = rn.GenerateRandomInt(minNumSentencesPerParagraph, maxNumSentencesPerParagraph);
                    paragraph    = rd.GenerateParagraph(numSentences);
                    _str.Append(paragraph);
                    _str.Append(Environment.NewLine);
                    _str.Append(Environment.NewLine);
                }

                Program._messageLog.WriteLine(_str.ToString());
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... RandomParagraphsTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #14
0
        public virtual void TestHighFreqs()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 3, 5);

            foreach (Options options in ValidOptions())
            {
                if (options == Options.NONE)
                {
                    continue;
                }
                using Directory dir            = NewDirectory();
                using RandomIndexWriter writer = new RandomIndexWriter(Random, dir);
                RandomDocument doc = docFactory.NewDocument(TestUtil.NextInt32(Random, 1, 2), AtLeast(20000),
                                                            options);
                writer.AddDocument(doc.ToDocument());
                using IndexReader reader = writer.GetReader();
                AssertEquals(doc, reader.GetTermVectors(0));
            }
        }
コード例 #15
0
        public virtual void TestHighFreqs()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 3, 5);

            foreach (Options options in ValidOptions())
            {
                if (options == Options.NONE)
                {
                    continue;
                }
                using (Directory dir = NewDirectory())
                    using (RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, ClassEnvRule.Similarity, ClassEnvRule.TimeZone))
                    {
                        RandomDocument doc = docFactory.NewDocument(TestUtil.NextInt(Random(), 1, 2), AtLeast(20000),
                                                                    options);
                        writer.AddDocument(doc.ToDocument());
                        using (IndexReader reader = writer.Reader)
                            AssertEquals(doc, reader.GetTermVectors(0));
                    }
            }
        }
コード例 #16
0
        protected virtual void AssertEquals(RandomDocument doc, Fields fields)
        {
            // compare field names
            Assert.AreEqual(doc is null, fields is null);
            Assert.AreEqual(doc.fieldNames.Length, fields.Count);
            ISet <string> fields1 = new JCG.HashSet <string>();
            ISet <string> fields2 = new JCG.HashSet <string>();

            for (int i = 0; i < doc.fieldNames.Length; ++i)
            {
                fields1.Add(doc.fieldNames[i]);
            }
            foreach (string field in fields)
            {
                fields2.Add(field);
            }
            Assert.IsTrue(fields1.SetEquals(fields2));

            for (int i = 0; i < doc.fieldNames.Length; ++i)
            {
                AssertEquals(doc.tokenStreams[i], doc.fieldTypes[i], fields.GetTerms(doc.fieldNames[i]));
            }
        }
コード例 #17
0
        protected internal virtual void AssertEquals(RandomDocument doc, Fields fields)
        {
            // compare field names
            Assert.AreEqual(doc == null, fields == null);
            Assert.AreEqual(doc.FieldNames.Length, fields.Size);
            HashSet <string> fields1 = new HashSet <string>();
            HashSet <string> fields2 = new HashSet <string>();

            for (int i = 0; i < doc.FieldNames.Length; ++i)
            {
                fields1.Add(doc.FieldNames[i]);
            }
            foreach (string field in fields)
            {
                fields2.Add(field);
            }
            Assert.IsTrue(fields1.SetEquals(fields2));

            for (int i = 0; i < doc.FieldNames.Length; ++i)
            {
                AssertEquals(doc.TokenStreams[i], doc.FieldTypes[i], fields.Terms(doc.FieldNames[i]));
            }
        }
コード例 #18
0
        public void GenerateRandomParagraphs()
        {
            RandomDocument randDoc = new RandomDocument();
            int            numParagraphsToGenerate  = 3;
            int            numSentencesPerParagraph = 10;

            try
            {
                _msg.Length = 0;
                _msg.Append("GenerateRandomParagraphs started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                numParagraphsToGenerate  = Convert.ToInt32(_frm.txtNumParagraphsToOutput.Text);
                numSentencesPerParagraph = Convert.ToInt32(_frm.txtNumSentencesToOutput.Text);

                for (int p = 0; p < numParagraphsToGenerate; p++)
                {
                    string paragraph = randDoc.GenerateParagraph(numSentencesPerParagraph);
                    _msg.Length = 0;
                    _msg.Append(paragraph);
                    Program._messageLog.WriteLine(_msg.ToString());
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... GenerateRandomParagraphs finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #19
0
        // [Test] // LUCENENET NOTE: For now, we are overriding this test in every subclass to pull it into the right context for the subclass
        // run random tests from different threads to make sure the per-thread clones
        // don't share mutable data
        public virtual void TestClone()
        {
            RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
            int numDocs = AtLeast(100);
            foreach (Options options in ValidOptions())
            {
                RandomDocument[] docs = new RandomDocument[numDocs];
                for (int i = 0; i < numDocs; ++i)
                {
                    docs[i] = docFactory.NewDocument(TestUtil.NextInt(Random(), 1, 3), AtLeast(10), options);
                }
                Directory dir = NewDirectory();
                RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, ClassEnvRule.Similarity, ClassEnvRule.TimeZone);
                for (int i = 0; i < numDocs; ++i)
                {
                    writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
                }
                IndexReader reader = writer.Reader;
                for (int i = 0; i < numDocs; ++i)
                {
                    int docID = DocID(reader, "" + i);
                    AssertEquals(docs[i], reader.GetTermVectors(docID));
                }

                AtomicObject<Exception> exception = new AtomicObject<Exception>();
                ThreadClass[] threads = new ThreadClass[2];
                for (int i = 0; i < threads.Length; ++i)
                {
                    threads[i] = new ThreadAnonymousInnerClassHelper(this, numDocs, docs, reader, exception, i);
                }
                foreach (ThreadClass thread in threads)
                {
                    thread.Start();
                }
                foreach (ThreadClass thread in threads)
                {
                    thread.Join();
                }
                reader.Dispose();
                writer.Dispose();
                dir.Dispose();
                Assert.IsNull(exception.Value, "One thread threw an exception");
            }
        }
コード例 #20
0
 // [Test] // LUCENENET NOTE: For now, we are overriding this test in every subclass to pull it into the right context for the subclass
 public virtual void TestMerge()
 {
     RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
     int numDocs = AtLeast(100);
     int numDeletes = Random().Next(numDocs);
     HashSet<int?> deletes = new HashSet<int?>();
     while (deletes.Count < numDeletes)
     {
         deletes.Add(Random().Next(numDocs));
     }
     foreach (Options options in ValidOptions())
     {
         RandomDocument[] docs = new RandomDocument[numDocs];
         for (int i = 0; i < numDocs; ++i)
         {
             docs[i] = docFactory.NewDocument(TestUtil.NextInt(Random(), 1, 3), AtLeast(10), options);
         }
         Directory dir = NewDirectory();
         RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, ClassEnvRule.Similarity, ClassEnvRule.TimeZone);
         for (int i = 0; i < numDocs; ++i)
         {
             writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
             if (Rarely())
             {
                 writer.Commit();
             }
         }
         foreach (int delete in deletes)
         {
             writer.DeleteDocuments(new Term("id", "" + delete));
         }
         // merge with deletes
         writer.ForceMerge(1);
         IndexReader reader = writer.Reader;
         for (int i = 0; i < numDocs; ++i)
         {
             if (!deletes.Contains(i))
             {
                 int docID = DocID(reader, "" + i);
                 AssertEquals(docs[i], reader.GetTermVectors(docID));
             }
         }
         reader.Dispose();
         writer.Dispose();
         dir.Dispose();
     }
 }
コード例 #21
0
 // [Test] // LUCENENET NOTE: For now, we are overriding this test in every subclass to pull it into the right context for the subclass
 public virtual void TestRandom()
 {
     RandomDocumentFactory docFactory = new RandomDocumentFactory(this, 5, 20);
     int numDocs = AtLeast(100);
     RandomDocument[] docs = new RandomDocument[numDocs];
     for (int i = 0; i < numDocs; ++i)
     {
         docs[i] = docFactory.NewDocument(TestUtil.NextInt(Random(), 1, 3), TestUtil.NextInt(Random(), 10, 50), RandomOptions());
     }
     Directory dir = NewDirectory();
     RandomIndexWriter writer = new RandomIndexWriter(Random(), dir, ClassEnvRule.Similarity, ClassEnvRule.TimeZone);
     for (int i = 0; i < numDocs; ++i)
     {
         writer.AddDocument(AddId(docs[i].ToDocument(), "" + i));
     }
     IndexReader reader = writer.Reader;
     for (int i = 0; i < numDocs; ++i)
     {
         int docID = DocID(reader, "" + i);
         AssertEquals(docs[i], reader.GetTermVectors(docID));
     }
     reader.Dispose();
     writer.Dispose();
     dir.Dispose();
 }
コード例 #22
0
        protected internal virtual void AssertEquals(RandomDocument doc, Fields fields)
        {
            // compare field names
            Assert.AreEqual(doc == null, fields == null);
            Assert.AreEqual(doc.FieldNames.Length, fields.Size);
            HashSet<string> fields1 = new HashSet<string>();
            HashSet<string> fields2 = new HashSet<string>();
            for (int i = 0; i < doc.FieldNames.Length; ++i)
            {
                fields1.Add(doc.FieldNames[i]);
            }
            foreach (string field in fields)
            {
                fields2.Add(field);
            }
            Assert.IsTrue(fields1.SetEquals(fields2));

            for (int i = 0; i < doc.FieldNames.Length; ++i)
            {
                AssertEquals(doc.TokenStreams[i], doc.FieldTypes[i], fields.Terms(doc.FieldNames[i]));
            }
        }
コード例 #23
0
        public void RandomChaptersTest(int numChapters, int minNumParagraphsPerChapter, int maxNumParagraphsPerChapter,
                                       int minNumSentencesPerParagraph, int maxNumSentencesPerParagraph,
                                       string pChapterHeadings)
        {
            RandomNumber   rn            = new RandomNumber();
            RandomDocument rd            = new RandomDocument();
            int            numParagraphs = 0;
            string         chap          = string.Empty;

            string[] chapterHeadings   = null;
            int      chapHeadingInx    = -1;
            int      chapHeadingMaxInx = -1;

            try
            {
                _msg.Length = 0;
                _msg.Append("RandomChaptersTest started ...\r\n");
                Program._messageLog.WriteLine(_msg.ToString());

                if (pChapterHeadings.Trim().Length > 0)
                {
                    chapterHeadings   = pChapterHeadings.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                    chapHeadingMaxInx = chapterHeadings.Length - 1;
                }

                chapHeadingInx = -1;
                for (int ch = 0; ch < numChapters; ch++)
                {
                    numParagraphs = rn.GenerateRandomInt(minNumParagraphsPerChapter, maxNumParagraphsPerChapter);
                    if (chapterHeadings == null)
                    {
                        _str.Length = 0;
                        _str.Append("Chapter ");
                        _str.Append((ch + 1).ToString());
                        _str.Append(Environment.NewLine);
                        _str.Append(Environment.NewLine);
                        chap = rd.GenerateChapter(numParagraphs, minNumSentencesPerParagraph, maxNumSentencesPerParagraph);
                        _str.Append(chap);
                        chap = _str.ToString();
                    }
                    else
                    {
                        chapHeadingInx++;
                        if (chapHeadingInx > chapHeadingMaxInx)
                        {
                            //More chapters are being generated than there are defined chapter headings
                            _str.Length = 0;
                            _str.Append("Chapter ");
                            _str.Append((ch + 1).ToString());
                            chap = rd.GenerateChapter(_str.ToString(), numParagraphs, minNumSentencesPerParagraph, maxNumSentencesPerParagraph);
                        }
                        else
                        {
                            chap = rd.GenerateChapter(chapterHeadings[chapHeadingInx], numParagraphs, minNumSentencesPerParagraph, maxNumSentencesPerParagraph);
                        }
                    }
                    Program._messageLog.WriteLine(chap);
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                _msg.Length = 0;
                _msg.Append("\r\n... RandomChaptersTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }
コード例 #24
0
        /// <summary>
        /// Routine to create random documents.
        /// </summary>
        /// <param name="numRows">Number of data rows to generate containing random sentences.</param>
        /// <param name="minNumParagraphs">Minimum number of paragraphs in a data row.</param>
        /// <param name="maxNumParagraphs">Maximum number of paragraphs in a data row.</param>
        /// <param name="minNumSentencesPerParagraph">Minimum number of sentences per paragraph.</param>
        /// <param name="maxNumSentencesPerParagraph">Maximum number of sentences per paragraph.</param>
        /// <param name="includeDocumentTitle">If true, a line containing a random phrase that serves as document title is included at the beginning of each random document. If false, no title is generated.</param>
        /// <returns>ADO.NET DataTable containing the random documents.</returns>
        public DataTable CreateRandomDocumentDataTable(int numRows, string minNumParagraphs, string maxNumParagraphs, string minNumSentencesPerParagraph, string maxNumSentencesPerParagraph, bool includeDocumentTitle)
        {
            DataTable      dt           = new DataTable();
            RandomNumber   rn           = new RandomNumber();
            PFRandomWord   randWord     = new PFRandomWord();
            RandomDocument randDocument = new RandomDocument();
            StringBuilder  title        = new StringBuilder();

            try
            {
                int minimumNumberOfParagraphs            = Convert.ToInt32(minNumParagraphs);
                int maximumNumberOfParagraphs            = Convert.ToInt32(maxNumParagraphs);
                int minimumNumberOfSentencesPerParagraph = Convert.ToInt32(minNumSentencesPerParagraph);
                int maximumNumberOfSentencesPerParagraph = Convert.ToInt32(maxNumSentencesPerParagraph);

                DataColumn dc = new DataColumn("RandomValue");
                dc.DataType = Type.GetType("System.String");
                dt.Columns.Add(dc);

                for (int i = 0; i < numRows; i++)
                {
                    DataRow dr = dt.NewRow();

                    title.Length = 0;
                    if (includeDocumentTitle)
                    {
                        int numWordsInTitle = rn.GenerateRandomInt((int)3, (int)7);
                        for (int t = 0; t < numWordsInTitle; t++)
                        {
                            if (t > 0)
                            {
                                title.Append(" ");
                            }
                            string temp = randWord.GetWord();
                            temp = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(temp.ToLower());
                            title.Append(temp);
                        }
                    }

                    int    numParagraphs = rn.GenerateRandomInt(minimumNumberOfParagraphs, maximumNumberOfParagraphs);
                    string str           = randDocument.GenerateDocument(numParagraphs, minimumNumberOfSentencesPerParagraph, maximumNumberOfSentencesPerParagraph, title.ToString());


                    dr[0] = str;
                    dt.Rows.Add(dr);
                }
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append("Error in CreateRandomDocumentDataTable routine.\r\n");
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                throw new System.Exception(_msg.ToString());
            }
            finally
            {
                ;
            }

            return(dt);
        }