本文整理汇总了C#中Trie.AddWord方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.AddWord方法的具体用法?C# Trie.AddWord怎么用?C# Trie.AddWord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Trie
的用法示例。
在下文中一共展示了Trie.AddWord方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddWord_RemoveWord01
public void AddWord_RemoveWord01()
{
var trie = new Trie();
trie.AddWord("");
trie.AddWord("");
Assert.AreEqual(1, trie.GetWords().Count);
Assert.AreEqual(2, trie.RemoveWord(""));
Assert.AreEqual(0, trie.GetWords().Count);
trie.AddWord("");
Assert.AreEqual(1, trie.GetWords().Count);
Assert.AreEqual(1, trie.RemoveWord(""));
Assert.AreEqual(0, trie.GetWords().Count);
}
示例2: Main
static void Main(string[] args)
{
var dictionary = new[] { "algorithm 10",
"selection sort 20",
"linked list 20",
"selection 5",
"viagra -100",
"selection sort fast 10"
};
var words = "The selection sort algorithm could be applied to linked lists. viagra";
var trie = new Trie();
foreach (var s in dictionary)
{
var input = s.Split(' ')
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
var result = input[0];
for (int i = 1; i < input.Length - 1; i++)
{
result += (' ' + input[i]);
}
trie.AddWord(result.ToLower(), double.Parse(input[input.Length - 1]));
}
var score = 0.0;
var spaceCounters = 0;
var currentWord = string.Empty;
words = words.ToLower();
for (var i = 0; i < words.Length; i++)
{
if (!char.IsLetter(words[i]))
{
continue;
}
if (currentWord.Length == 0)
{
currentWord = words[i].ToString();
i++;
}
while (i < words.Length && char.IsLetter(words[i]))
{
currentWord += words[i];
i++;
}
spaceCounters++;
if (!trie.HasWord(currentWord))
{
currentWord = string.Empty;
continue;
}
var currentScore = 0.0;
while (trie.HasWord(currentWord))
{
currentWord += ' ';
var j = i;
while (trie.GetWordWeight(currentWord) == 0)
{
j++;
while (j < words.Length && char.IsLetter(words[j]))
{
currentWord += words[j];
j++;
}
currentWord += ' ';
if (!trie.HasWord(currentWord))
{
break;
}
spaceCounters++;
i = j;
}
var weight = trie.GetWordWeight(currentWord);
currentScore = weight == 0 ? currentScore : weight;
}
score += currentScore;
currentWord = string.Empty;
}
Console.WriteLine(score / spaceCounters);
}
示例3: Main
public static void Main()
{
string[] words = new string[]{"app","apple","appl", "brown", "brony", "michael",
"michtell", "mike", "brow","manny", "mann", "scott", "xie", "stokton"};
Trie dict = new Trie();
foreach(string s in words)
dict.AddWord(s);
List<string> l1 = dict.GetWords("s");
foreach(string s in l1)
Console.WriteLine(s);
Console.WriteLine(dict.CheckWord("xi"));
Console.WriteLine(dict.CountPrefix("ap"));
}
示例4: BuildSampleTrie
private ITrie BuildSampleTrie()
{
ITrie trie = new Trie();
string[] strings =
{
"123", "1", "23", "1",
"this", "test", "the", "TEMP", "TOKEN", "TAKE", "THUMP"
};
foreach (string s in strings)
{
trie.AddWord(s);
}
return trie;
}