From 7bad9251e23a148dcefc115e6292538e3689e3b1 Mon Sep 17 00:00:00 2001 From: Tristan Hyams Date: Thu, 23 Nov 2023 20:54:54 -0600 Subject: [PATCH] Update Program.cs --- examples/Mnemonic/Program.cs | 68 ++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/examples/Mnemonic/Program.cs b/examples/Mnemonic/Program.cs index ceea55f..4e9c6d0 100644 --- a/examples/Mnemonic/Program.cs +++ b/examples/Mnemonic/Program.cs @@ -1,32 +1,62 @@ using Mnemonic.AhoCorasick; using System; +using System.Collections.Generic; using System.Diagnostics; using static Mnemonic.Utilities.Extensions.StopwatchExtensions; Console.WriteLine("Mnemonic AhoCorasick StringReplace"); var ac = new AhoCorasickStringReplace(); +var patterns = new Dictionary +{ + { "apple is red", "apple is yellow" }, + { "apple is teal", "apple is blue" }, + { "apple is puple", "apple is violet" }, + { "apple is yellowish", "apple is brown" }, + { "apple is bolt", "apple is belt" }, + { "apple is orange", "apple is blue" }, + { "apple is rod", "apple is red" }, + { "apple is green", "apple is blue" }, + { "pear is green", "pear is blue" }, + { "cheese is green", "cheese has mold" } +}; -ac.AddPattern("apple is red", "apple is yellow"); -ac.AddPattern("apple is teal", "apple is blue"); -ac.AddPattern("apple is puple", "apple is violet"); -ac.AddPattern("apple is yellowish", "apple is brown"); -ac.AddPattern("apple is bolt", "apple is belt"); -ac.AddPattern("apple is orange", "apple is blue"); -ac.AddPattern("apple is rod", "apple is red"); -ac.AddPattern("apple is green", "apple is blue"); -ac.AddPattern("pear is green", "pear is blue"); -ac.AddPattern("cheese is green", "cheese has mold"); - +ac.AddPatterns(patterns); ac.BuildFailureLinks(); -var sw = Stopwatch.StartNew(); -var input = "My apple is red green."; -var output = ac.Replace(input); -sw.Stop(); +AhoStringReplace(); +StringReplace(patterns); + +Console.ReadKey(); + +void AhoStringReplace() +{ + var input = "My apple is red green."; + var sw = Stopwatch.StartNew(); + var output = ac.Replace(input); + sw.Stop(); + + Console.WriteLine($"AhoStringReplace"); + Console.WriteLine($"Input: {input}"); + Console.WriteLine($"Output: {output}"); + Console.WriteLine($"Elapsed: {sw.ElapsedMicroseconds():000} us"); +} + +void StringReplace(Dictionary patterns) +{ + Console.WriteLine($"StringReplace"); + + var input = "My apple is red green."; + Console.WriteLine($"Input: {input}"); + + var sw = Stopwatch.StartNew(); -Console.WriteLine($"Input: {input}"); -Console.WriteLine($"Output: {output}"); -Console.WriteLine($"Elapsed: {sw.ElapsedMicroseconds():000} us"); + foreach (var kvp in patterns) + { + input = input.Replace(kvp.Key, kvp.Value); + } + sw.Stop(); -Console.ReadKey(); \ No newline at end of file + Console.WriteLine($"Output: {input}"); + Console.WriteLine($"Elapsed: {sw.ElapsedMicroseconds():000} us"); +} \ No newline at end of file