3.2.1 泛型例子,泛型字典统计文本中的单词数

        static Dictionary<string, int> CountWords(string text)
        {
            Dictionary<string, int> frequencies;
            frequencies = new Dictionary<string, int>();
            string[] words = Regex.Split(text, @"\W+");
            foreach (string word in words)
            {
                if (frequencies.ContainsKey(word))
                    frequencies[word]++;
                else
                    frequencies[word] = 1;
            }
            return frequencies;
        }
        
        static void Main(string[] args)
        {
            string text = @"ay,iyaiyai
                            ay,iyaiyai
                            ay,iyaiyai
                            where's my samural?
                            i've been searching for a man
                            allacross japan
                            just to find,to find my samural
                            someone who is strong
                            but still a little shy
                            yes i need,i need my samural
                            ay,ay,ay
                            i'm your little butterfly
                            green,black and blue
                            make the colours of the sky
                            ay,ay,ay
                            i'm your little butterfly
                            green,black and blue
                            make the colours of the sky
                            i've been searching in the woods
                            and high upon the hills
                            just to find,to find my samural
                            someone who won't regret
                            to keep me his net
                            yes i need,i need my samural";
            Dictionary<string, int> freque = CountWords(text);
            foreach (KeyValuePair<string, int> entry in freque)
            {
                string word = entry.Key;
                int freques = entry.Value;
                Console.WriteLine("{0}:{1}", word, freques);
            }
        }

 

转载于:https://www.cnblogs.com/xiacy/archive/2012/05/01/2477714.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值