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


C# Trie.TryFindCount方法代码示例

本文整理汇总了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);
                    }
                }
            }
        }
//.........这里部分代码省略.........
开发者ID:kalinalazarova1,项目名称:TelerikAcademy,代码行数:101,代码来源:TrieTest.cs


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