本文整理汇总了C#中Trie.TryFindCount方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.TryFindCount方法的具体用法?C# Trie.TryFindCount怎么用?C# Trie.TryFindCount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Trie
的用法示例。
在下文中一共展示了Trie.TryFindCount方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
internal static void Main()
{
string[] separators = { " ", ",", ".", "!", "?", "\"", "/", "[", "]", "{", "}", "(", ")", ":", ";", "|", "<", ">", "*", "\t", "-", "_" };
var keywords = new Trie(new TrieNode('#'));
var wordsReader = new StreamReader(@"..\..\keywords.txt");
var textReader = new StreamReader(@"..\..\text.txt");
var writer = new StreamWriter(@"../../output1.txt");
var result = new Dictionary<string, int>();
string all;
var time = new Stopwatch();
// first variant builds trie from the keywords and checks each word from the text if it is present in the Trie
time.Start();
using (wordsReader)
{
for (var w = wordsReader.ReadLine(); w != null; w = wordsReader.ReadLine())
{
keywords.Add(w.ToLower());
}
}
using (textReader)
{
all = textReader.ReadToEnd().ToLower();
}
StringBuilder word = new StringBuilder();
for (int i = 0; i < all.Length; i++)
{
if (char.IsLetter(all[i]))
{
word.Append(all[i]);
}
else
{
if (keywords.Contains(word.ToString()))
{
if (result.ContainsKey(word.ToString()))
{
result[word.ToString()]++;
}
else
{
result[word.ToString()] = 1;
}
}
word.Clear();
}
}
using (writer)
{
writer.Write(string.Join(Environment.NewLine, result));
}
time.Stop();
Console.WriteLine("Elapsed time in seconds: {0:0.0000}", time.ElapsedMilliseconds / 1000f);
// Second variant bilds a Trie with the text and checks each keyword in it - 3 times slower due to too large trie
time.Reset();
var text = new Trie(new TrieNode('#'));
time.Start();
int wordCount = 0;
textReader = new StreamReader(@"..\..\text.txt");
using (textReader)
{
all = textReader.ReadToEnd().ToLower();
for (int i = 0; i < all.Length; i++)
{
if (char.IsLetter(all[i]))
{
word.Append(all[i]);
}
else
{
text.Add(word.ToString());
word.Clear();
}
}
}
word.Clear();
writer = new StreamWriter(@"../../output2.txt");
wordsReader = new StreamReader(@"..\..\keywords.txt");
using (wordsReader)
{
using (writer)
{
for (var w = wordsReader.ReadLine(); w != null; w = wordsReader.ReadLine())
{
if (text.TryFindCount(w.ToLower(), out wordCount))
{
writer.WriteLine("[{0}, {1}]", w, wordCount);
}
}
}
}
//.........这里部分代码省略.........