예제 #1
0
        public async Task CoordTest()
        {
            using (Index index = new Index())
            {
                index.WorkPath = "CoordTest\\";
                int N = 1000;
                index.AddDataSource(new TestDataSource(N));
                index.Stemmers.Clear();
                await index.CreateAsync();

                Assert.False(index.IsCreating);
                Assert.True(index.CanSearch);

                string[] words = { "and", "tupman", "everybody", "old" };

                // coordinate test
                foreach (string word in words)
                {
                    List <int> pos = new List <int>();
                    foreach (Match m in Regex.Matches(TestText1.ToLower(), @"\b" + word + @"\b"))
                    {
                        pos.Add(m.Index);
                    }
                    Index.SearchResult res = index.Search(word);
                    Assert.Equal(2 * N, res.foundPages.Count);
                    Assert.Equal(pos.Count, res.foundPages[0].pos.Count);
                    foreach (var p in res.foundPages)
                    {
                        Assert.True(pos.SequenceEqual(p.pos));
                    }
                }
            }
            Directory.Delete("CoordTest\\", true);
        }
예제 #2
0
        public void BuilderTest()
        {
            Vocab voc = new Vocab();

            voc.Add("and", 1);
            voc.Add("end", 3);
            voc.Add("old", 2);
            voc.Add("the", 6);
            voc.Add("them", 5);
            voc.Add("then", 4);
            voc.Range = ('a', 'z');
            voc.Name  = "en";
            Index.Builder bldr = new Index.Builder("BuilderTest").AddVoc(voc);
            bldr.AddDoc("A", "");

            string[]     words = { "and", "tupman", "everybody", "old" };
            List <int>[] pos   = new List <int> [words.Length];
            for (int q = 0; q < pos.Length; q++)
            {
                pos[q] = new List <int>();
            }

            foreach (Match match in Regex.Matches(TestText1.ToLower(), @"\b\w+\b"))
            {
                for (int q = 0; q < pos.Length; q++)
                {
                    if (match.Value.Equals(words[q]))
                    {
                        pos[q].Add(match.Index);
                    }
                }
                bldr.AddWord(match.Value, (ulong)match.Index);
            }
            bldr.EndPage("1");

            using (Index index = bldr.Build())
            {
                for (int q = 0; q < pos.Length; q++)
                {
                    Index.SearchResult res = index.Search(words[q]);
                    Assert.NotNull(res);
                    Assert.Equal(1, res.foundPages.Count);
                    Assert.True(pos[q].SequenceEqual(res.foundPages[0].pos));
                }
            }

            Directory.Delete("BuilderTest", true);
        }
예제 #3
0
        public async Task PageTextTest()
        {
            int N = 10;
            SamePageDataSource ds = new SamePageDataSource(N);

            using (Index index = new Index("PageTextTest"))
            {
                index.AddDataSource(ds);
                await index.CreateAsync();

                Index.SearchResult res = index.Search("\"old lady\"");
                Assert.Contains(Index.SearchResult.BEGIN_MATCHED_SYMBOL + "old" + Index.SearchResult.END_MATCHED_SYMBOL, res.foundPages[0].text);
                res = index.Search("Test");
                Assert.Contains(Index.SearchResult.BEGIN_MATCHED_SYMBOL + "Test" + Index.SearchResult.END_MATCHED_SYMBOL, res.foundDocs.First().headers["Name"]);
            }
            Directory.Delete("PageTextTest", true);
        }
예제 #4
0
        public async Task RequestSyntaxTest()
        {
            using (Index index = new Index())
            {
                index.WorkPath = "RequestSyntaxTest\\";
                //index.Stemmers.Clear();

                int Npages = 100;
                index.AddDataSource(new TestDataSource(Npages));
                await index.CreateAsync();

                Assert.False(index.IsCreating);
                Assert.True(index.CanSearch);

                Index.SearchResult res = index.Search("and (tupman|old)" /*, new Index.SearchOptions(){ dist = 20}*/);

                Assert.Equal(2, res.foundDocs.Count);

                Assert.Equal(Npages, res.foundDocs.First().pages.Count);
                Assert.Equal(Npages, res.foundDocs.Last().pages.Count);

                res = index.Search("and (tupman|old) {Name=Dump}" /*, new Index.SearchOptions(){ dist = 20}*/);

                Assert.Equal(1, res.foundDocs.Count);

                Assert.Equal(Npages, res.foundDocs.First().pages.Count);

                Assert.Equal(res.foundPages[0].pos.Count, res.foundPages[1].pos.Count);
                for (int q = 0; q < 2 * Npages; q++)
                {
                    Assert.Equal(42, res.foundPages[q].pos.Count);
                }
                Assert.True(Enumerable.SequenceEqual(res.foundPages[0].pos, res.foundPages[1].pos));

                res = index.Search("lady old", new Index.SearchOptions()
                {
                    dist = 40
                });
                foreach (int pos in res.foundPages[0].pos)
                {
                    Console.WriteLine("" + pos + ":" + TestText1.Substring(pos, 10));
                }

                Assert.Equal(10, res.foundPages[0].pos.Count);
                res = index.Search("\"lady\" old", new Index.SearchOptions()
                {
                    dist = 40
                });
                Assert.Equal(8, res.foundPages[0].pos.Count);
                res = index.Search("\"old lady\"", new Index.SearchOptions()
                {
                    dist = 40
                });
                Assert.Equal(8, res.foundPages[0].pos.Count);
                res = index.Search("\"lady old\"", new Index.SearchOptions()
                {
                    dist = 40
                });
                Assert.Equal(0, res.foundPages.Count);

                Assert.Equal(12, index.Search("lady (old | young)", new Index.SearchOptions()
                {
                    dist = 40
                }).foundPages[0].pos.Count);
                Assert.Equal(3, index.Search("\"old ladies were\"", new Index.SearchOptions()
                {
                    dist = 40
                }).foundPages[0].pos.Count);
                res = index.Search("\"old lady were\"", new Index.SearchOptions()
                {
                    dist = 40
                });
                Assert.Equal(0, res.foundPages.Count);
                res = index.Search("\"old (lady|ladies) (who|were|looked)\"", new Index.SearchOptions()
                {
                    dist = 40
                });
                //foreach (int pos in res.foundPages[0].pos)
                // Console.WriteLine(""+pos+":"+TestText1.Substring(pos,10));
                Assert.Equal(9, res.foundPages[0].pos.Count);

                res = index.Search("?an?", new Index.SearchOptions()
                {
                    dist = 40
                });
                Assert.Equal(Regex.Matches(TestText1.ToLower(), @"\w*an\w*").Count, res.foundPages[0].pos.Count);
            }
            Directory.Delete("RequestSyntaxTest\\", true);
        }