词频统计 第三周

package search;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class UpdateWordSearch
 {
    /**
     * 输入文件 保存分隔后的单词集合 保存统计后的单词集合
     */
    String article;// 保存文章的内容
    String[] rWords;
    String[] words;
    int[] wordFreqs;// 保存单词对应的词频
    String filename;// 文件名
    // 统计总数
    int total = 0;

    // 构造函数:输入文章的内容
    public UpdateWordSearch() throws IOException
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入文件名:");
        filename = sc.nextLine();
        File file = new File(filename);
        if (!file.exists())
            {
            System.out.println("文件不存在!");
            return;
            }
        BufferedReader bf = new BufferedReader(new FileReader(file));
        StringBuffer article = new StringBuffer(); // 动态字符串数组
        String temp = bf.readLine();
        while (temp != null)
        {
            article.append(temp + " "); // 往动态字符串数组里添加数据
            temp = bf.readLine();
            if (temp == null)
            {
                break;
            }
        }
        this.article = article.toString();
    }

    // 分词并统计相应词汇
    public void sWord()
    {
        // 分词的时候,因为标点符号不参与,所以所有的符号全部替换为空格
        final char SPACE = ‘ ‘;
        article = article.replace(‘\"‘, SPACE).replace(‘,‘, SPACE)
                .replace(‘.‘, SPACE).replace(‘\‘‘, SPACE);
        article = article.replace(‘(‘, SPACE).replace(‘)‘, SPACE)
                .replace(‘-‘, SPACE);
        rWords = article.split("\\s+");// 凡是空格隔开的都算单词,上面替换了‘,所以I‘ve被分成两个单词
    }

    public List<String> sort()
    {
        // 将所有出现的字符串放入唯一的list中,不用map,是因为map寻找效率太低了
        List<String> list = new ArrayList<String>();
        for (String word : rWords)
            {
            list.add(word);
            }
        Collections.sort(list);
        return list;
    }

    // 词汇排序
    public List countWordFreq()
    {
        // 统计词频信息
        Map<String, Integer> wordsInfo = new TreeMap<String, Integer>();
        String word = ""; // 词频名字
        int count = 0; // 词频数量
        // 统计单词总数
        int total = 0;
        List<String> wordList = sort();
        word = wordList.get(0);
        for (int i = 0; i <= wordList.size(); i++)
            {
            if (i == wordList.size())
            {
                wordsInfo.put(word, count);
                total++;
                break;
            }
            if (wordList.get(i).equals(word))
            {
                count++;
            }
             else
            {
                wordsInfo.put(word, count);
                total++;
                word = wordList.get(i);
                count = 1;
            }
        }
        // 词频信息排序
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
                wordsInfo.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Entry<String, Integer> o1,
                    Entry<String, Integer> o2)
                   {
                // TODO Auto-generated method stub
                return o2.getValue().compareTo(o1.getValue());

                   }
        });
        this.total = total;
        return list;
    }

    public void run()
    {

        // 拆分文本
        sWord();
        // 统计词频
        List<Map.Entry<String, Integer>> list = countWordFreq();
        // 打印词频总数
        System.out.println("词频总数:");
        System.out.println("total:" + this.total);
        System.out.println("词频统计信息:");
        // 打印统计词频
        int m = 0;
        for (Map.Entry<String, Integer> mapping : list)

        {
            if (m < 10)
                {
                System.out.println(mapping.getKey() + " : "
                        + mapping.getValue());
                m++;
                }
            else
                break;

        }
    }

    // 测试类的功能
    public static void main(String[] args) throws IOException
    {
        UpdateWordSearch w = new UpdateWordSearch();
        w.run();
    }
}
时间: 2024-08-24 10:38:53

词频统计 第三周的相关文章

词频统计(第二周)

新功能需求: 1.小文件输入. 在控制台下输入命令. 2.支持命令行输入英文作品的文件名. 部分核心代码: 结构体定义: 定义一个结构体来存放读取的单词与其统计的次数. typedef struct addup { char word[50]; int count; }R; 读取文本: char temp[50];        R fin[10000]={"\0",0}; char file[10]; fflush(stdin); gets(file); fp=freopen(fil

每周进度及工作量统计——第三周

9月26 类别 开始时间 结束时间 中断时间 净时间 查找资料 13:30 17:30 10m 200m 安装程序 19:00 21:00 10m 100m 查看单元测试资料 22:00 23:00 0m 60m 9月27日 类别 开始时间 结束时间 中断时间 共用时 安装vs cppunit及熟悉环境 9:00 11:20 10m 130m 编写程序 14:30 15:00 0m 30m 写博客 18:00 18:40 0m 40m 9月28日 类别 开始时间 结束时间 中断时间 共用时 结对

201671010421 麻存滔 词频统计软件项目报告

一.需求分析 1.程序可读入任意英文文本文件,该文件中英文词数大于等于1个,程序需要很壮健,能读取容纳英文原版<哈利波特>10万词以上的文章. 2.指定单词词频统计功能:用户可输入从该文本中想要查找词频的一个或任意多个英文单词,运行程序的统计功能可显示对应单词在文本中出现的次数和柱状图. 3.高频词统计功能:用户从键盘输入高频词输出的个数k,运行程序统计功能,可按文本中词频数降序显示前k个单词的词频及单词. 4.统计该文本所有单词数量及词频数,并能将单词及词频数按字典顺序输出到文件result

第二周作业-词频统计

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

第一周 词频统计

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

第二周-词频统计更新

词频统计功能新增: 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 = Conso

软件工程第二周作业----词频统计

实验要求: 统计文件中出现过的单词数目,并按数目和字典顺序排序,将结果输出到指定文件中.需要统计单词的文件名从命令行输入. 实验分工: 代码编写&测试:张文杰 博客编写:朱昱青 实验思路: 1.在主函数中打开输入和输出文件,获得文件的指针,然后以指针为参数调用count()函数进行词频统计. 2.在count()函数中,利用while循环,不断从文件中分离出可能是单词的字符串(也就是被分隔符隔开的连续字母和数字),然后进一步判断该字符串是否是一个单词.如果是,再查看这个单词是否出现过,如果出现过

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

由于不会用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.进行词频统计 3.对结果进行排序并输出 https:https://git.coding.net/yanzouzhe/ywcptj.git SSH:[email protected]:yanzouzhe/ywcptj.git 功能实现 1.小文本输入读取文件 public class Article { /** * @param args */ String content;// 保存文章的内容 String[