当前位置: 首页>>代码示例>>C#>>正文


C# Trie.HasWord方法代码示例

本文整理汇总了C#中Trie.HasWord方法的典型用法代码示例。如果您正苦于以下问题:C# Trie.HasWord方法的具体用法?C# Trie.HasWord怎么用?C# Trie.HasWord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Trie的用法示例。


在下文中一共展示了Trie.HasWord方法的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);
        }
开发者ID:cwetanow,项目名称:Telerik,代码行数:99,代码来源:Startup.cs


注:本文中的Trie.HasWord方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。