private Trie CreatePrefixTree() { var trie = new Trie(); trie.AddRange(dictionary); return(trie); }
internal static void ApplyObsoleteKanaMap(Trie trie) { trie.AddRange(new Dictionary <string, string>() { { "wi", "ゐ" }, { "we", "ゑ" } }); }
private Trie CreateTrie(IEnumerable <string> dictionaryItems) { var trie = new Trie(); trie.AddRange(dictionaryItems); return(trie); }
public void AddRange() { const int Count = 10; var trie = new Trie<bool>(); trie.AddRange(Enumerable.Range(0, Count).Select(i => new TrieEntry<bool>(i.ToString(), false))); Assert.AreEqual(Count, trie.Count); }
public void AddRange() { const int count = 10; var trie = new Trie <bool>(); trie.AddRange(Enumerable.Range(0, count).Select(i => new StringEntry <bool>(i.ToString(), false))); Assert.AreEqual(count, trie.Count); }
public void TestMinimize(string[] strings, string expected) { var trie = new Trie(); trie.AddRange(strings); var min = DfaMinimizer <char> .Minimize(trie); var result = min.ToString(); Assert.AreEqual(expected, result); }
public static async Task Create(Dictionary <string, string> arguments, Dictionary <string, string> options) { await Task.Delay(1); var cacheFilePath = arguments["cache"]; var projectDirectory = arguments["project"]; // Check file exists if (cacheFilePath == null || !File.Exists(cacheFilePath)) { throw new ComposerException("Cache doesn't exist"); } // Create project directory // TODO: think about logic if directory already exists if (!Directory.Exists(projectDirectory)) { Directory.CreateDirectory(projectDirectory); } // Create Project var project = new Models.Project { Title = "Example Halo Project", Version = "1.0.0", Description = "Simple project for testing the tool", Properties = new ProjectProperties { TagsFolder = "tags", GitEnabled = true } }; using (var stream = File.OpenRead(cacheFilePath)) using (var reader = new EndianReader(stream, Endian.BigEndian)) { EngineDescription engineDescription = null; EngineDatabase engineDatabase = XMLEngineDatabaseLoader.LoadDatabase("data/formats/engines.xml"); var cacheFile = CacheFileLoader.LoadCacheFile(reader, engineDatabase, out engineDescription); var stringIdTrie = new Trie(); if (cacheFile.StringIDs != null) { stringIdTrie.AddRange(cacheFile.StringIDs); } if (cacheFile.TagClasses.Any()) { LoadTags(project, cacheFile, projectDirectory, stringIdTrie); } } File.WriteAllText(Path.Combine(projectDirectory, "project.json"), JsonConvert.SerializeObject(project, Formatting.Indented)); }
private static void TrieWork() { const int prefixLength = 1; var prefixes = GetAllMatches(Enumerable.Range(65, 26).Select(i => (char)i).ToArray(), prefixLength) .ToArray(); var words = GetWords().ToArray(); Console.WriteLine( "Words count: {0}. Prefixes count: {1}.", words.Length, prefixes.Length); var stopWatch = Stopwatch.StartNew(); foreach (var prefix in prefixes) { var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray(); } stopWatch.Stop(); Console.WriteLine("ToArray method: {0}", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); var trie = new Trie <bool>(); trie.AddRange(words.Select(w => new StringEntry <bool>(w, false))); stopWatch.Stop(); Console.WriteLine("Build tree: {0}", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); int prefixesCount = 0; foreach (var prefix in prefixes) { var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray(); prefixesCount = resultTrie.Length; } stopWatch.Stop(); Console.WriteLine("Trie find prefixes: {0}", stopWatch.ElapsedMilliseconds); Console.WriteLine($"Prefixes found: {prefixesCount}"); }
public void CheckTest() { var words = GetWords(); var trie = new Trie <bool>(); trie.AddRange(words.Select(w => new StringEntry <bool>(w, false))); foreach (var prefix in Prefixes) { var result1 = words.Where(w => w.StartsWith(prefix)); var result2 = trie.GetByPrefix(prefix).Select(t => t.Key).OrderBy(w => w); Assert.IsTrue(result1.SequenceEqual(result2)); } }
public static void Main() { var words = LoadWords(VocabularyPath); Console.WriteLine("Words count: {0}", words.Count()); const int PrefixLength = 2; var prefixes = GetAllMatches(Enumerable.Range('A', 'Z' - 'A' + 1). Select(i => ((char)i)).ToArray(), PrefixLength) .ToArray(); Console.WriteLine("Search prefixes count: {0}", prefixes.Count()); Console.WriteLine(); var stopWatch = Stopwatch.StartNew(); int matchesCount = 0; foreach (var prefix in prefixes) { var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray(); matchesCount += resultArray.Count(); } Console.WriteLine("Found {0} matches", matchesCount); stopWatch.Stop(); Console.WriteLine("Regular string matching time: {0} ms", stopWatch.ElapsedMilliseconds); Console.WriteLine(); stopWatch.Restart(); var trie = new Trie <bool>(); trie.AddRange(words.Select(w => new TrieEntry <bool>(w, false))); stopWatch.Stop(); Console.WriteLine("Build trie time: {0} ms", stopWatch.ElapsedMilliseconds); Console.WriteLine(); stopWatch.Restart(); matchesCount = 0; foreach (var prefix in prefixes) { var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray(); matchesCount += resultTrie.Count(); } Console.WriteLine("Found {0} matches", matchesCount); stopWatch.Stop(); Console.WriteLine("Trie find prefixes time: {0} ms", stopWatch.ElapsedMilliseconds); }
public void BenchmarkTest() { const int Count = 1; var words = GetWords(); TestContext.WriteLine( "Words count: {0}. Iterations count: {1}. Prefixes count: {2}.", words.Count(), Count, Prefixes.Length); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i < Count; i++) { foreach (var prefix in Prefixes) { var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray(); } } stopWatch.Stop(); TestContext.WriteLine("ToArray method: {0}", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); var trie = new Trie <bool>(); trie.AddRange(words.Select(w => new StringEntry <bool>(w, false))); stopWatch.Stop(); TestContext.WriteLine("Build tree: {0}", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); for (int i = 0; i < Count; i++) { foreach (var prefix in Prefixes) { var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray(); } } stopWatch.Stop(); TestContext.WriteLine("Trie find prefixes: {0}", stopWatch.ElapsedMilliseconds); }
public void BenchmarkTest() { const int Count = 1; var words = GetWords(); TestContext.WriteLine( "Words count: {0}. Iterations count: {1}. Prefixes count: {2}.", words.Count(), Count, Prefixes.Length); var stopWatch = Stopwatch.StartNew(); for (int i = 0; i < Count; i++) { foreach (var prefix in Prefixes) { var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray(); } } stopWatch.Stop(); TestContext.WriteLine("ToArray method: {0}", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); var trie = new Trie<bool>(); trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false))); stopWatch.Stop(); TestContext.WriteLine("Build tree: {0}", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); for (int i = 0; i < Count; i++) { foreach (var prefix in Prefixes) { var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray(); } } stopWatch.Stop(); TestContext.WriteLine("Trie find prefixes: {0}", stopWatch.ElapsedMilliseconds); }
public static void Main() { var words = LoadWords(VocabularyPath); Console.WriteLine("Words count: {0}", words.Count()); const int PrefixLength = 2; var prefixes = GetAllMatches(Enumerable.Range('A', 'Z' - 'A' + 1). Select(i => ((char)i)).ToArray(), PrefixLength) .ToArray(); Console.WriteLine("Search prefixes count: {0}", prefixes.Count()); Console.WriteLine(); var stopWatch = Stopwatch.StartNew(); int matchesCount = 0; foreach (var prefix in prefixes) { var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray(); matchesCount += resultArray.Count(); } Console.WriteLine("Found {0} matches", matchesCount); stopWatch.Stop(); Console.WriteLine("Regular string matching time: {0} ms", stopWatch.ElapsedMilliseconds); Console.WriteLine(); stopWatch.Restart(); var trie = new Trie<bool>(); trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false))); stopWatch.Stop(); Console.WriteLine("Build trie time: {0} ms", stopWatch.ElapsedMilliseconds); Console.WriteLine(); stopWatch.Restart(); matchesCount = 0; foreach (var prefix in prefixes) { var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray(); matchesCount += resultTrie.Count(); } Console.WriteLine("Found {0} matches", matchesCount); stopWatch.Stop(); Console.WriteLine("Trie find prefixes time: {0} ms", stopWatch.ElapsedMilliseconds); }
public static readonly int N = 2; // We use 2 as 3 doesn't allow '*c#*' wildcards (but '*c#' and 'c#*' work), public static Trie <int> CreateTrie(TagLookup allTags) { // From http://algs4.cs.princeton.edu/52trie/ // 15. Substring matches. // Given a list of (short) strings, your goal is to support queries where the user looks up a string s // and your job is to report back all strings in the list that contain s. // Hint: if you only want prefix matches (where the strings have to start with s), use a TST as described in the text. // To support substring matches, insert the suffixes of each word (e.g., string, tring, ring, ing, ng, g) into the TST. var trieSetupTimer = Stopwatch.StartNew(); var trie = new Trie <int>(); trie.AddRange(allTags.Select(t => new TrieEntry <int>(t.Key, TrieTerminator))); trie.AddRangeAllowDuplicates(allTags.Select(t => new TrieEntry <int>(Reverse(t.Key), TrieReverseTerminator))); trieSetupTimer.Stop(); Logger.LogStartupMessage("\nTook {0} ({1,6:N2} ms) to SETUP the Trie (ONE-OFF cost)", trieSetupTimer.Elapsed, trieSetupTimer.Elapsed.TotalMilliseconds); return(trie); }
public static void Main(string[] args) { const int PrefixLength = 1; var prefixes = GetAllMatches(Enumerable.Range(65, 26).Select(i => ((char)i)).ToArray(), PrefixLength) .ToArray(); var words = GetWords(); Console.WriteLine("Words count: {0}. Prefixes count: {1}.", words.Count(), prefixes.Length); var stopWatch = Stopwatch.StartNew(); foreach (var prefix in prefixes) { var resultArray = words.Where(w => w.StartsWith(prefix)).ToArray(); } stopWatch.Stop(); Console.WriteLine("ToArray method: {0}ms.", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); var trie = new Trie<bool>(); trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false))); stopWatch.Stop(); Console.WriteLine("Build tree: {0}ms.", stopWatch.ElapsedMilliseconds); stopWatch.Restart(); foreach (var prefix in prefixes) { var resultTrie = trie.GetByPrefix(prefix).Select(w => w.Key).ToArray(); } stopWatch.Stop(); Console.WriteLine("Trie find prefixes: {0}ms.", stopWatch.ElapsedMilliseconds); }
public void CheckTest() { var words = GetWords(); var trie = new Trie<bool>(); trie.AddRange(words.Select(w => new TrieEntry<bool>(w, false))); foreach (var prefix in Prefixes) { var result1 = words.Where(w => w.StartsWith(prefix)); var result2 = trie.GetByPrefix(prefix).Select(t => t.Key).OrderBy(w => w); Assert.IsTrue(result1.SequenceEqual(result2)); } }