本文整理汇总了C#中Trie.GetWordWeight方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.GetWordWeight方法的具体用法?C# Trie.GetWordWeight怎么用?C# Trie.GetWordWeight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Trie
的用法示例。
在下文中一共展示了Trie.GetWordWeight方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}