Пример #1
0
        public void NullSourceString(IStringSearchAlgorithm algorithm)
        {
            string toFind   = null;
            string toSearch = "this string does not contain the missing string data";

            ISearchMatch[] matches = algorithm.Search(toFind, toSearch).ToArray();
        }
Пример #2
0
        public void NullTargetString(IStringSearchAlgorithm algorithm)
        {
            string toFind   = "missing data";
            string toSearch = null;

            ISearchMatch[] matches = algorithm.Search(toFind, toSearch).ToArray();
        }
Пример #3
0
        static void Main(string[] args)
        {
            // set language to english
            // (because in czech "ch" is one letter and "abch".IndexOf("bc") returns -1)
            System.Threading.Thread.CurrentThread.CurrentCulture=System.Globalization.CultureInfo.CreateSpecificCulture("en");

            // algorithms
            IStringSearchAlgorithm[] algorithms=new IStringSearchAlgorithm[2];
            algorithms[0]=new AhoCorasickStringMatch();
            algorithms[1] = new HashingStringMatch();

            double[] results=new double[3];
            for(int j=0; j<50; j++)
            {
                // generate random keywords and random text
                string[] keywords=GetRandomKeywords(80);
                string text=GetRandomText(2000);

                // insert keyword into text
                text=text.Insert(rnd.Next(text.Length),keywords[rnd.Next(keywords.Length)]);

                // for each algorithm...
                for(int algIndex=0; algIndex<algorithms.Length; algIndex++)
                {
                    IStringSearchAlgorithm alg=algorithms[algIndex];
                    Console.WriteLine(alg.ToString());
                    alg.Keywords=keywords;

                    // search for keywords and measure performance
                    HiPerfTimer tmr=new HiPerfTimer();
                    tmr.Start();

                    StringSearchResult[] r=alg.FindAll(text);

                    tmr.Stop();

                    Console.WriteLine(tmr.Duration);
                    Console.WriteLine("Found: "+r.Length);
                    alg.TotalDuration += tmr.Duration;

                }

                Console.WriteLine("===========================");
                Console.WriteLine();
            }

            Console.WriteLine("\n===SUMMARY===");

            for(int j=0; j<algorithms.Length; j++)
            {
                IStringSearchAlgorithm alg=algorithms[j];
                Console.WriteLine(alg.ToString()+"="+alg.TotalDuration);
            }

            Console.WriteLine();
        }
Пример #4
0
        public void EmptyEmpty(IStringSearchAlgorithm algorithm)
        {
            string toFind   = string.Empty;
            string toSearch = string.Empty;

            ISearchMatch[] matches = algorithm.Search(toFind, toSearch).ToArray();

            Assert.IsNotNull(matches, "The matches array should not be null.");
            Assert.AreEqual(0, matches.Length, "The matches array should have not items.");
        }
Пример #5
0
        public void EmptySourceString(IStringSearchAlgorithm algorithm)
        {
            string toFind   = string.Empty;
            string toSearch = "this string does not contain the missing string data";

            ISearchMatch[] matches = algorithm.Search(toFind, toSearch).ToArray();

            Assert.IsNotNull(matches, "The matches array should not be null.");
            Assert.AreEqual(0, matches.Length, "The matches array should have not items.");
        }
Пример #6
0
        public void Example(IStringSearchAlgorithm algorithm)
        {
            string toFind   = "he";
            string toSearch = "The brown cat ran through the kitchen";

            foreach (ISearchMatch match in algorithm.Search(toFind, toSearch))
            {
                Console.WriteLine("Match found at: {0}", match.Start);
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            // set language to english
            // (because in czech "ch" is one letter and "abch".IndexOf("bc") returns -1)
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en");

            // algorithms
            IStringSearchAlgorithm[] algorithms = new IStringSearchAlgorithm[2];
            algorithms[0] = new AhoCorasickStringMatch();
            algorithms[1] = new HashingStringMatch();

            double[] results = new double[3];
            for (int j = 0; j < 50; j++)
            {
                // generate random keywords and random text
                string[] keywords = GetRandomKeywords(80);
                string   text     = GetRandomText(2000);

                // insert keyword into text
                text = text.Insert(rnd.Next(text.Length), keywords[rnd.Next(keywords.Length)]);

                // for each algorithm...
                for (int algIndex = 0; algIndex < algorithms.Length; algIndex++)
                {
                    IStringSearchAlgorithm alg = algorithms[algIndex];
                    Console.WriteLine(alg.ToString());
                    alg.Keywords = keywords;

                    // search for keywords and measure performance
                    HiPerfTimer tmr = new HiPerfTimer();
                    tmr.Start();

                    StringSearchResult[] r = alg.FindAll(text);

                    tmr.Stop();

                    Console.WriteLine(tmr.Duration);
                    Console.WriteLine("Found: " + r.Length);
                    alg.TotalDuration += tmr.Duration;
                }

                Console.WriteLine("===========================");
                Console.WriteLine();
            }

            Console.WriteLine("\n===SUMMARY===");

            for (int j = 0; j < algorithms.Length; j++)
            {
                IStringSearchAlgorithm alg = algorithms[j];
                Console.WriteLine(alg.ToString() + "=" + alg.TotalDuration);
            }

            Console.WriteLine();
        }
Пример #8
0
        public void ExactSingleCharMatch(IStringSearchAlgorithm algorithm)
        {
            string toFind   = "f";
            string toSearch = "f";

            ISearchMatch[] matches = algorithm.Search(toFind, toSearch).ToArray();

            Assert.IsNotNull(matches, "The matches array should not be null.");
            Assert.AreEqual(1, matches.Length, "The matches array should have not items.");
            Assert.AreEqual(0, matches[0].Start, "The start of the string match should be 0");
            Assert.AreEqual(toFind.Length, matches[0].Length, "The length of the string match should equal the string found");
        }
        private static StringBuilder PerformSearchAndReplace(IStringSearchAlgorithm algorithm, string input, string find, string replace)
        {
            StringBuilder result        = new StringBuilder();
            int           previousStart = 0;

            foreach (var match in algorithm.Search(find, input))
            {
                result.Append(input.Substring(previousStart, match.Start - previousStart));
                result.Append(replace);
                previousStart = match.Start + match.Length;
            }

            result.Append(input.Substring(previousStart));
            return(result);
        }
Пример #10
0
        public void MultipleMatchesLeadingMiddleTrailing(IStringSearchAlgorithm algorithm)
        {
            string toFind   = "found";
            string toSearch = "leadingfound and foundtrailing";

            ISearchMatch[] matches = algorithm.Search(toFind, toSearch).ToArray();

            Assert.IsNotNull(matches, "The matches array should not be null.");
            Assert.AreEqual(2, matches.Length, "The matches array should have not items.");

            Assert.AreEqual(7, matches[0].Start, "The start of the string match should be 0");
            Assert.AreEqual(toFind.Length, matches[0].Length, "The length of the string match should equal the string found");

            Assert.AreEqual(17, matches[1].Start, "The start of the string match should be 10");
            Assert.AreEqual(toFind.Length, matches[1].Length, "The length of the string match should equal the string found");
        }
 public KeywordSearchManager()
 {
     _searchAlgorithm = new StringSearch();
 }
 public AlgorithmSelector(string name, IStringSearchAlgorithm algorithm)
 {
     Name = name;
     Algorithm = algorithm;
 }