示例#1
0
        static void SearchDemo()
        {
            string needle   = "ardcara";
            string haystack = "abataradabardardcaraadatatabat";

            Console.WriteLine();
            Console.WriteLine("Needle:");
            Console.WriteLine(needle);
            Console.WriteLine();
            Console.WriteLine("Haystack:");
            Console.WriteLine(haystack);
            Console.WriteLine();

            IStringSearcher stringSearcher = new BoyerMooreSearcher();

            Console.WriteLine("BM found position: {0}", stringSearcher.Search(needle, haystack));

            stringSearcher = new BoyerMooreHorspoolSearcher();
            Console.WriteLine("BMH found position: {0}", stringSearcher.Search(needle, haystack));

            stringSearcher = new KnuthMorrisPrattSearcher();
            Console.WriteLine("KMP found position: {0}", stringSearcher.Search(needle, haystack));

            Console.WriteLine();
            Console.WriteLine("Array:");

            int[] arr     = { 1, 4, 7, 9, 12, 14, 15, 17, 19, 20, 23, 25, 27, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39 };
            int   element = 37;

            ISearcher <int> searcher = new BinarySearcher();

            Console.WriteLine("Binary search of element {0}: position {1}", element, searcher.Search(element, arr, arr.Length));

            searcher = new InterpolationSearcher();
            Console.WriteLine("Interpolation search of element {0}: position {1}", element, searcher.Search(element, arr, arr.Length));

            Console.ReadKey();
            Console.Clear();
        }
示例#2
0
        public ReturnObject Post(SearchQuery query)
        {
            try
            {
                List <SearchResult> result = new List <SearchResult>();
                Searcher            searcher;

                if (query.Pattern == null)
                {
                    query.Pattern = "";
                }

                query.Pattern = query.Pattern.Trim();

                if (query.Pattern.Equals(""))
                {
                    throw new ArgumentException("Pattern kosong.");
                }

                if (query.Id == 0)
                {
                    searcher = new KmpSearcher(query.Pattern);
                }
                else if (query.Id == 1)
                {
                    searcher = new BoyerMooreSearcher(query.Pattern);
                }
                else if (query.Id == 2)
                {
                    searcher = new RegexSearcher(query.Pattern);
                }
                else
                {
                    throw new NotImplementedException();
                }

                PrepareNewsList(query.Source);

                foreach (News news in newsList)
                {
                    SearchResult searchResult = new SearchResult()
                    {
                        Url = news.Url, Title = news.Title, ImageUrl = news.ImageUrl, PubDate = news.PubDate
                    };
                    bool found = false;

                    int indexMatchContent = searcher.CheckMatch(news.Content);
                    if (indexMatchContent != -1)
                    {
                        found = true;
                        searchResult.Match = SearchResult.StringToMatch(news.Content, query.Pattern, indexMatchContent, 200);
                    }
                    else
                    {
                        int indexMatchTitle = searcher.CheckMatch(news.Title);
                        if (indexMatchTitle != -1)
                        {
                            found = true;
                            searchResult.Match = SearchResult.StringToMatch(news.Title, query.Pattern, indexMatchTitle, 50) + " (Pada Judul)";
                        }
                    }

                    if (found)
                    {
                        result.Add(searchResult);
                    }
                }
                return(new ReturnObject()
                {
                    status = true, data = result
                });
            }
            catch (Exception e)
            {
                return(new ReturnObject()
                {
                    status = false, data = e.Message
                });
            }
        }