⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 实现页面的词频统计
💻 TXT
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace wordcount
{
    class Entry
    {
        private string strword;
        public string Word
        {
            get { return strword; }
        }

        private int iFrequence;
        public int Frequence
        { 
            get { return iFrequence; } 
        }
        public Entry(string word)
        {
            this.strword = word;
            this.iFrequence = 1;
        }
        public void IncFreq()
        {
            this.iFrequence++;
        }
    }
    class FileStat
    {
        public static void Run(string fileTEXT,string fileStore)
        { if (!File.Exists(fileTEXT))
          {
            new FileNotFoundException("无法打开文件["+fileTEXT+"]!");
          }
          StreamReader reader = new StreamReader(fileTEXT);
          ArrayList list = new ArrayList();
          string line;
          line = reader.ReadLine();
          while (line!=null)
          {
              if (!line.Equals(""))
              {
                  string [] splits = line.Split(new char[]{' ','\t',',','.','?','!','<','>','\\','/',':'});
                  foreach (string str in splits)
                  {
                      if (!str.Equals(""))
                      {
                          bool found = false;
                          foreach (Entry entry in list)
                          {
                              if (entry.Word .Equals(str,StringComparison.OrdinalIgnoreCase))
                              { 
                                  entry.IncFreq();
                                  found = true;
                                  break;
                              }
                          }
                          if (found == false)
                              list.Add(new Entry(str));
                      }
                  }
              }
              line = reader.ReadLine();
          }
          reader.Close();
          try 
          {
              int count = 0;
              StreamWriter sw = new StreamWriter(fileStore);
              foreach (Entry entry in list)
              {
                  sw.WriteLine("{0}\t{1}",entry.Word,entry.Frequence);
                  count += entry.Frequence ;
              }
              sw.Close();
              Console.WriteLine("Total {0} words.",count);
          }
          catch (Exception e)
          {
              Console.WriteLine("the file could not be wrirren:");
              Console.WriteLine(e.Message);
          }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("input text file name:");
            string fileTEXT = Console.ReadLine();
            Console.WriteLine("input store file name:");
            string fileStore = Console.ReadLine();
            FileStat.Run(fileTEXT, fileStore);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -