-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f84c83b
commit 7bad925
Showing
1 changed file
with
49 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string> | ||
{ | ||
{ "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<string, string> 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(); | ||
Console.WriteLine($"Output: {input}"); | ||
Console.WriteLine($"Elapsed: {sw.ElapsedMicroseconds():000} us"); | ||
} |