public static void FuzzyMatchDemo()
        {
            List <string> words =
                System.IO.File
                .ReadAllLines("google-10000-english/google-10000-english.txt")
                .Where(x =>
                       !(x.Equals("magic", StringComparison.InvariantCultureIgnoreCase)) &&
                       !(x.Equals("light", StringComparison.InvariantCultureIgnoreCase)))
                .ToList();

            // Listing 2.22 A fuzzy match
            Demo.Benchmark("Listing 2.22 A fuzzy match", () =>
            {
                string fuzzyMatch = ConcurrentSpeculation.FuzzyMatch(words, "magic"); //#D

                Console.WriteLine($"FuzzyMatch for 'magic' = {fuzzyMatch}");
            });

            Demo.Benchmark("Listing 2.23 Fast Fuzzy Match using precomputation", () =>
            {
                Func <string, string> fastFuzzyMatch = PartialFuzzyMatch(words); //#D

                string magicFuzzyMatch = fastFuzzyMatch("magic");
                string lightFuzzyMatch = fastFuzzyMatch("light");   //#E

                Console.WriteLine($"FastFuzzyMatch for 'magic' = {magicFuzzyMatch}");
                Console.WriteLine($"FastFuzzyMatch for 'light' = {lightFuzzyMatch}");
            });
        }
示例#2
0
        public static void RunDemo()
        {
            List <string> urls = new List <string> { //#A
                @"http://www.google.com",
                @"http://www.microsoft.com",
                @"http://www.bing.com",
                @"http://www.google.com"
            };

            Demo.Benchmark("Listing 2.17 Web crawler execution", () =>
            {
                var webPageTitles = from url in urls //#B
                                    from pageContent in WebCrawler(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });

            Demo.Benchmark("Listing 2.18 Web crawler execution using memoization", () =>
            {
                var webPageTitles = from url in urls //#B
                                    from pageContent in WebCrawlerMemoized(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });

            Demo.Benchmark("Listing 2.19 Web crawler query using PLINQ", () =>
            {
                var webPageTitles = from url in urls.AsParallel() //#A
                                    from pageContent in WebCrawlerMemoized(url)
                                    select ExtractWebPageTitle(pageContent);

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });

            Demo.Benchmark("Listing 2.20 Thread-safe memoization function", () =>
            {
                var webPageTitles = from url in urls.AsParallel()
                                    from pageContent in WebCrawlerMemoizedThreadSafe(url) //#B
                                    select ExtractWebPageTitle(pageContent);              //#C

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });

            Demo.Benchmark("Listing 2.21 Thread-Safe Memoization function with safe lazy evaluation", () =>
            {
                var webPageTitles = from url in urls.AsParallel()
                                    from pageContent in WebCrawlerMemoizedLazyThreadSafe(url) //#B
                                    select ExtractWebPageTitle(pageContent);                  //#C

                Console.WriteLine($"Crawled {webPageTitles.Count()} page titles");
            });
        }