public static void Main() { var generator = new TextGenerator(WordTypes.Name); var names = generator.GenerateText(1000); var trie = new Trie(); var words = new HashSet<string>(); names.Split(' ').ToList().ForEach( x => { words.Add(x); trie.AddWord(x); }); var result = new StringBuilder(); foreach (var word in words.OrderBy(x => x)) { int occurenceCount; trie.TryFindWord(word, out occurenceCount); result.AppendFormat("{0} -> {1} times", word, occurenceCount).AppendLine(); } Console.WriteLine(result); }
static void Main(string[] args) { long count = 0; trie = new Trie(); dict = new Dictionary<string, bool>(); foreach (var word in Random.Words(10, 40)) { trie.Add(word); dict[word] = true; count++; } Console.Clear(); // 123 test bench("123", 0); // test an early find on list bench("car", 30); // test the last one on list bench("caterpillar", 60); Console.WriteLine(); Console.WriteLine(count); Console.Read(); }
private static Trie<int> ReadFile(string fileName) { string text = ""; try { Console.Write("Reading file"); using (StreamReader sr = new StreamReader(fileName)) { text = sr.ReadToEnd(); } } catch (IOException e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } Console.WriteLine(" Done"); Console.Write("Extracting words"); words = new List<string>(text.ToLower().Split(new char[] { '"', '\'', '\r', '\n', '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries)); Trie<int> result = new Trie<int>(); Console.WriteLine(" Done. {0} words extracted", words.Count()); for (int i = 0; i < words.Count; i++) { if (i % (words.Count / 100) == 0) { Console.Write("\rBuilding trie {0} percent complete", (i * 100) / words.Count); } result.Add(words[i], i); } Console.WriteLine(); return result; }
private static void BuildUp(string fileName, Trie<int> trie) { IEnumerable<WordAndLine> allWordsInFile = GetWordsFromFile(fileName); foreach (WordAndLine wordAndLine in allWordsInFile) { trie.Add(wordAndLine.Word, wordAndLine.Line); } }
public static void Main() { Trie<int> trie = new Trie<int>(); BuildUp(@"../../Lorem Ipsum.txt", trie); LookUp("lorem", trie); LookUp("ipsum", trie); LookUp("amet", trie); }
public static void Main() { var trie = new Trie(new TrieNode(' ', new Dictionary<char, TrieNode>(), false, 0)); var words = GenerateRandomWords(1000000); var uniqueWords = new HashSet<string>(words); AddWordsToTrie(words, trie); GetCountOfAllUniqueWords(uniqueWords, trie); }
public dynamic Get(int count) { while (words.Count < count) words.AddRange(Trie.Random.Words(5, 10)); DateTime sTime; var trie = new Trie.Trie(); var dict = new Dictionary<string, bool>(); var testWords = new List<string> {"123", "car", "caterpillar" }; var trieData = new List<List<int>>(); var dictData = new List<List<int>>(); int x = 0; int jumps = 5000; while (x < count) { foreach (var word in words.Take(jumps).Skip(x)) { trie.Add(word); dict[word] = true; } int dictZum = 0; int trieZum = 0; foreach (var word in testWords) { for (int J = 0; J < TESTS; J++) { sTime = DateTime.Now; for (int i = 0; i < LOOPS; i++) trie.Contains(word); trieZum += sTime.Diff(); sTime = DateTime.Now; for (int i = 0; i < LOOPS; i++) dict.ContainsKey(word); dictZum += sTime.Diff(); } } x += jumps; trieData.Add(new List<int> { x, trieZum / (TESTS * testWords.Count) }); dictData.Add(new List<int> { x, dictZum / (TESTS * testWords.Count) }); } dynamic chart = new ExpandoObject(); chart.Label = "TrieBenchmark"; chart.TrieData = trieData; chart.DictData = dictData; return Json(chart); }
private static void LookUp(string searchString, Trie<int> trie) { Console.WriteLine("----------------------------------------"); Console.WriteLine("Look-up for string '{0}'", searchString); var result = trie.Retrieve(searchString).ToArray(); string matchesText = string.Join(",", result); int matchesCount = result.Count(); if (matchesCount == 0) { Console.WriteLine("No matches found."); } else { Console.WriteLine(" {0} matches found. \tLines: {1}", matchesCount, matchesText); } }
private static void Test() { var a = new Trie(); a.AddWord("hello"); a.AddWord("hello"); a.AddWord("hi"); a.AddWord("helloo"); int oc = 0; Console.WriteLine(a.TryFindWord("hel", out oc) + " -> " + oc + " times"); Console.WriteLine(a.TryFindWord("hi", out oc) + " -> " + oc + " times"); Console.WriteLine(a.TryFindWord("hii", out oc) + " -> " + oc + " times"); Console.WriteLine(a.TryFindWord("hello", out oc) + " -> " + oc + " times"); Console.WriteLine(a.TryFindWord("helloo", out oc) + " -> " + oc + " times"); }
static void Main(string[] args) { Trie newTrie = new Trie(); FileStream fs = new FileStream(@".\names.txt",FileMode.Open); StreamReader sr = new StreamReader(fs); int ch, sum = 0, totalcount = 0; string temp = ""; bool inString = false; while((ch = sr.Read())!= -1) { if(ch >= 65 && ch <= 90) { temp += Convert.ToChar(ch); } else if(34 == ch) { inString = !inString; if(false == inString && temp != "") { newTrie.AddString(temp); temp = ""; totalcount++; } } } for(int i = 0; i < 26; i++) { while(newTrie.Head.child[i].count != 0) { sum += TraversalTrie(newTrie.Head.child[i], 0, ""); //System.Console.WriteLine(); } } System.Console.WriteLine(sum); System.Console.WriteLine(GlobalCount); }
public static void Main() { var words = LoadWords(VocabularyPath).ToArray(); Console.WriteLine("Words count: {0}", words.Length); const int prefixLength = 2; var data = Enumerable .Range('A', 'Z' - 'A' + 1) .Select(i => (char)i) .ToArray(); var prefixes = GetAllMatches(data, prefixLength).ToArray(); Console.WriteLine("Search prefixes count: {0}", prefixes.Length); Console.WriteLine(); var stopWatch = Stopwatch.StartNew(); var matchesCount = 0; foreach (var prefix in prefixes) { var resultArray = words .Where(w => w.StartsWith(prefix)) .ToArray(); matchesCount += resultArray.Length; } 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>(); foreach (var item in words) { trie.Insert(item, true); } 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).ToArray(); matchesCount += resultTrie.Length; } Console.WriteLine("Found {0} matches", matchesCount); stopWatch.Stop(); Console.WriteLine("Trie find prefixes time: {0} ms", stopWatch.ElapsedMilliseconds); }
public void changeToContent(Trie.Trie trie) { }
public static void readFileFromFile(String path) { if (tree == null) { tree = new Trie.Trie(path); } Scanner scanner = new Scanner(path); String info; int count = 0; int man = "男".GetHashCode(); int woman = "女".GetHashCode(); while ((info = scanner.nextLine()) != null) { count++; count %= 9; if (count == 0) { tree.setDescribe(info); tree.insert(); } else { switch (count) { case 1: tree.setQQ(Int32.Parse(info)); break; case 2: tree.setAge(Int32.Parse(info)); break; case 3: int message = info.GetHashCode(); if (message == man) { tree.setSex(true); } else if (message == woman) { tree.setSex(false); } else { tree.setSex(null); } break; case 4: tree.setName(info); break; case 5: tree.setGroupID(Int32.Parse(info)); break; case 6: tree.setGroupName(info); break; case 7: tree.setPrivilege(info); break; case 8: tree.setCreateTime(info); break; } } } }
public static void Main() { Trie trie = new Trie(); trie.Go(); }
public dynamic Get(int count) { while (words.Count < count) { words.AddRange(Trie.Random.Words(5, 10)); } DateTime sTime; var trie = new Trie.Trie(); var dict = new Dictionary <string, bool>(); var testWords = new List <string> { "123", "car", "caterpillar" }; var trieData = new List <List <int> >(); var dictData = new List <List <int> >(); int x = 0; int jumps = 5000; while (x < count) { foreach (var word in words.Take(jumps).Skip(x)) { trie.Add(word); dict[word] = true; } int dictZum = 0; int trieZum = 0; foreach (var word in testWords) { for (int J = 0; J < TESTS; J++) { sTime = DateTime.Now; for (int i = 0; i < LOOPS; i++) { trie.Contains(word); } trieZum += sTime.Diff(); sTime = DateTime.Now; for (int i = 0; i < LOOPS; i++) { dict.ContainsKey(word); } dictZum += sTime.Diff(); } } x += jumps; trieData.Add(new List <int> { x, trieZum / (TESTS * testWords.Count) }); dictData.Add(new List <int> { x, dictZum / (TESTS * testWords.Count) }); } dynamic chart = new ExpandoObject(); chart.Label = "TrieBenchmark"; chart.TrieData = trieData; chart.DictData = dictData; return(Json(chart)); }