第二周-词频统计更新

词频统计功能新增:

HTTPS:https://git.coding.net/li_yuhuan/WordFrequency.git

SSH:[email protected]:li_yuhuan/WordFrequency.git

代码:

        static void Main(string[] args)
        {
            string str = "";
            int length = args.Length;

            switch (length)
            {
                case 0:
                    {
                        string line = Console.ReadLine();
                        Frequency(line);
                        break;
                    }

                case 1:
                    {
                        str = m_workPath + args[0] + ".txt";

                        if (File.Exists(str))
                        {
                            LoadFile(str);
                            DictionarySort(m_wordList);
                        }

                        break;
                    }

                case 2:
                    {
                        if ("-s" == args[0])
                        {
                            str = args[1];

                            if (File.Exists(str))
                            {
                                LoadFile(str);
                                DictionarySort(m_wordList);
                            }
                        }
                        else if ("dir" == args[0])
                        {
                            if (Directory.Exists(args[1]))
                            {
                                m_top = 10;
                                m_pathList.AddRange(Directory.GetFiles(args[1], "*.txt"));

                                int index;

                                foreach (string path in m_pathList)
                                {
                                    index = path.LastIndexOf("\\");

                                    if (index > 0)
                                    {
                                        string name = path.Substring(index + 1, path.Length - index - 1);
                                        Console.WriteLine(name);
                                    }

                                    LoadFile(path);
                                    DictionarySort(m_wordList);
                                }
                            }
                        }

                        break;
                    }

                default:
                    {
                        break;
                    }
            }
        }

判断通过控制台传入主函数的参数个数分情况处理;

1.没有参数时,则统计输入的一段文字中的单词总数及频次并排序;

2.有一个参数时判断当前的工作目录下是否存在为该名字的txt文件,存在则统计文件中单词总数及频次并排序;

3.有两个参数:

1)当参数为-s + 文件时,判断文件是否存在,统计单词总数频次并排序;

2)当参数为dir + 路径,判断路径是否存在,分别统计路径中所有txt文件中单词总数,频次,并排序;

--------------------------------------------------------------------------------------------------------------------------

        static private void LoadFile(string filepath)
        {
            string line = string.Empty;

            using (StreamReader reader = new StreamReader(filepath))
            {
                line = reader.ReadLine();

                while (line != null)
                {
                    Frequency(line);
                    line = reader.ReadLine();
                }
            }
        }

按行读取文件并对该行进行统计;

--------------------------------------------------------------------------------------------------------------------------

        static private void Frequency(string line)
        {
            List<string> words = new List<string>();
            string word = string.Empty;
            char[] split = { ‘ ‘, ‘,‘, ‘?‘, ‘?‘, ‘.‘, ‘。‘, ‘-‘, ‘—‘, ‘"‘, ‘:‘, ‘:‘, ‘\r‘, ‘\n‘, ‘(‘, ‘)‘, ‘“‘, ‘”‘ };

            words.AddRange(line.Split(split));

            foreach (string str in words)
            {
                if (str != "" && str != null)
                {
                    word = str.ToLower();

                    if (m_wordList.ContainsKey(word))
                    {
                        m_wordList[word] += 1;
                    }
                    else
                    {
                        m_wordList.Add(word, 1);
                    }
                }
            }
        }

对传入的一行进行分割,存入list,遍历list进行比较统计,数据存入Dictionary;

--------------------------------------------------------------------------------------------------------------------------

        static private void DictionarySort(Dictionary<string, int> dictionary)
        {
            if (dictionary.Count > 0)
            {
                List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(dictionary);

                lst.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
                {
                    return s2.Value.CompareTo(s1.Value);
                });

                dictionary.Clear();

                Console.WriteLine("total  " + lst.Count + "  words\n");

                int k = 0;

                foreach (KeyValuePair<string, int> kvp in lst)
                {
                    if (k < m_top)
                    {
                        Console.WriteLine(kvp.Key + ":" + kvp.Value);
                        k++;
                    }
                }

                Console.WriteLine("----------------------------\n");
            }
        }
    }

把dictionary中的键值对存入list,利用list进行排序;

--------------------------------------------------------------------------------------------------------------------------

运行示例:

功能1:

--------------------------------------------------------------------------------------------------------------------------

功能2:

--------------------------------------------------------------------------------------------------------------------------

功能3:

--------------------------------------------------------------------------------------------------------------------------

功能4:(未完成)

时间: 2024-10-11 09:49:04

第二周-词频统计更新的相关文章

第二周 词频统计

原需求 1.读取文件,文件内包可含英文字符,及常见标点,空格级换行符. 2.统计英文单词在本文件的出现次数 3.将统计结果排序 4.显示排序结果 新需求: 1.小文件输入. 为表明程序能跑 2.支持命令行输入英文作品的文件名 3. 支持命令行输入存储有英文作品文件的目录名,批量统计 4. 从控制台读入英文单篇作品,重定向输出 1. #include <stdio.h>#include <string.h>#include <stdlib.h>int main(){ FI

2nd 词频统计更新

词频统计更新 实现功能:从控制台输入文件路径,并统计单词总数及不重复的单词数,并输出所有单词词频,同时排序. 头文件 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 定义宏 #define WORD_LENGTH 250 定义结构体及全局变量 typedef struct Node { char word[WORD_LENGTH]; int time; struct Node *next;

第一周 词频统计

这是我的第一篇博客,说起来有些惭愧,作为一个程序猿竟然至今没写过一篇技术博客.在这里,先向读到这篇博客的读者致歉,原谅我粗糙的表达能力. 在读研究生之前,“程序员”对我来说,只是三个字的组合,我并不了解程序员的世界,也不知道一个程序员的基本素养(这个词是从亮哥那听来的,但是是从杨老师那了解的).在这里,我要向我的导师--杨贵福老师表示深深的感谢,他教会了我许多作为一个程序员应有的工作的态度以及责任. 接下来谈谈我第一次上我导师的课的感受.我现在是研二,两年来我从没听过我的导师上的课.上周五是我第

第二周-频统计效能分析

根据作业要求对个人项目词频统计进行效能分析 工具:vs2015自带的效能分析工具: 1.第一次分析结果 string.split()方法和dictionary.contain()方法占比例较高; 由于水平问题暂时未想到解决方案,会继续探索,完善个人项目;

补交第一周词频统计 四则运算的地址

由于不会用codind.net .现在补上 四则运算HTTPS SSH :https://git.coding.net/brilliant/1hao.git [email protected]:brilliant/1hao.git 词频统计HTTPS SSH :https://git.coding.net/brilliant/cptj11.git https://git.coding.net/brilliant/cptj11.git

词频统计更新

代码有两个分支,1.选择输入文本路径或,2.选择直接输入文章. public static void main(String[] args) { HashMap<String,Integer> map=new HashMap<String,Integer>();//用于统计各个单词的个数,排序 //过滤字符串中的所有标点符号 String regex=" ?.!:,\"\"'';\n"; BufferedReader br; try { //

第三周 词频统计

HTTP:https:https://git.coding.net/liqiao085/wf--week2.gitssh://[email protected]:liqiao085/wf--week2.git 功能1: void f1()//完成自己输入文章统计功能 {        int sum = 0;    gets(str);  //  printf("%s\n",str);     int len = strlen(str);//文章长度      for(int i = 

第二周作业-词频统计

本周作业是词频统计,编程具体要求如下: https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/922 对实现功能进行简要的介绍: 功能一:小文件输入,为表明程序能跑.需要在控制台下输入命令,得到文件中不重复的总单词数.并对单词出现的次数进行排序输出. 功能二:支持命令行输入英文作品的文件名,亲自录入,输出显示不重复单词总数,并对出现频率最高的前10的单词进行输出 功能三:支持命令行输入存储有英文作品文件的目录名,批量统计词频. 功能四:

第二周结对编程作业——词频统计

本周作业是结对编写一个词频统计的程序,我们组是我(欧阳思琪)和贺晋飞同学共同完成这项任务.在仔细阅读了要求之后,我们组对程序编程进行了讨论.由于语言可以不必局限于要求中的C.C++,我们便考虑JAVA或python,两者各有优缺点,JAVA写起来比较繁重,而基于以往用python处理NLP相关项目的经验觉得python较为简单,但觉得在简单要求下,使用JAVA的运行速度明显更快,所以我们选择使用JAVA来完成本次作业. 分工:欧阳思琪 代码编写与博客编写 贺晋飞   代码审查与代码测试 实际:由