C#进行简单的词频统计

这段代码展示了一个C#程序,用于统计输入文本中各单词出现的频率。程序首先读取文本文件,然后通过正则表达式分割单词,过滤掉长度小于等于3的单词和中文字符。接下来,程序使用字典数据结构存储单词及其对应的频率,并对字典按值降序排序。最后,程序遍历排序后的字典,打印出频率大于等于5的单词及其频率。
摘要由CSDN通过智能技术生成

代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 泛型
{
    class Program
    {


  


        static Dictionary<string, int> CountWords(string text)
        {
            // 1.创建从单词到频率的新映射
            Dictionary<string, int> frequencies;
            frequencies = new Dictionary<string, int>();

            // 2.将文本分解成单词

            string[] words = Regex.Split(text, @"\W+");
            Regex r = new Regex(@"[\u4e00-\u9fa5]");  //匹配是否有中文

            foreach (var word in words)
            {
                if (word == ""||word.Length<=3||r.IsMatch(word))
                    continue;
                //3. 添加或更新映射
                if (frequencies.ContainsKey(word))
                {

                    frequencies[word]++;
                }
                else {
                    frequencies[word] = 1;
                }
            }
            return frequencies;
        }
        static void Main(string[] args)
        {
            // 读取要统计的文件
            string text = ReadTxtContent(@"c:\users\administrator\documents\visual studio 2015\Projects\泛型\泛型\1.txt");
            //过虐可能认识的单词
          //  string filter = ReadTxtContent(@"c:\users\administrator\documents\visual studio 2015\Projects\泛型\泛型\proj.txt");
            Dictionary<string, int> frequencies = CountWords(text);
           // Dictionary<string, int> filterWords = CountWords(filter);
            var dicSort = from objDic in frequencies orderby objDic.Value descending select objDic;  //降序 如果想按升序(顺序)排列,只需要把变量 dicSort 右边的 descending 去掉即可。 如果要按 Key 排序,只需要把变量 dicSort 右边的 objDic.Value 改为 objDic.Key 即可。
         
            foreach (var entry in dicSort)
            {
                
                // 4. 打印映射中的每个键值对
                string word = entry.Key;
                int frequency = entry.Value;
                if (frequency < 5 )//||filterWords.ContainsKey(word))  本来想进行过滤的 但这样写系统进行一个一个匹配  太费时间,需要算法优化。
                {
                    continue;
                }
                Console.WriteLine("{0}:{1}",word,frequency);
            }
            Console.ReadKey();
        }

        static string text = "";
        static string content = "";
        public static string ReadTxtContent(string Path)
        {
            StreamReader sr = new StreamReader(Path, Encoding.UTF8);
            while ((content = sr.ReadLine()) != null)
            {
                text += content;
            }
            return text.ToString();
        }
    }
}

在这里插入图片描述

下载地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值