本文整理汇总了C#中Trie.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.Contains方法的具体用法?C# Trie.Contains怎么用?C# Trie.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Trie
的用法示例。
在下文中一共展示了Trie.Contains方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TrieTests_Contains
public void TrieTests_Contains()
{
Trie<char> trie = new Trie<char>();
trie.Insert("Hello");
Assert.IsTrue(trie.Contains("Hello"));
Assert.IsFalse(trie.Contains("Hell"));
Assert.IsFalse(trie.Contains("Hellop"));
}
示例2: TrieTests_Contains2
public void TrieTests_Contains2()
{
Trie<int> trie = new Trie<int>();
int[] array = new int[] { 1, 2, 3 };
trie.Insert(array);
trie.Insert(new int[] { });
Assert.IsTrue(trie.Contains(new int[] { }));
Assert.IsTrue(trie.Contains(array));
Assert.IsFalse(trie.Contains(new int[] { 1, 2 }));
}
示例3: 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);
}
}
}
}
//.........这里部分代码省略.........
示例4: IsLosingWord
private static bool IsLosingWord(Trie<string> wordList, string word)
{
return word.Length >= 4 && wordList.Contains(word);
}
示例5: Contains_KeyExists
public void Contains_KeyExists()
{
Trie<string, char, int> trie = new Trie<string, char, int>();
trie.Add("key", 5);
Assert.IsTrue(trie.Contains(trie.First()));
}
示例6: Contains_KeyDoesNotExist
public void Contains_KeyDoesNotExist()
{
Trie<string, char, int> trie = new Trie<string, char, int>();
trie.Add("key", 5);
Assert.IsFalse(trie.Contains(new KeyValuePair<string, int>("key2", 1)));
}