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();
}
}

下图是词频统计所做的junit测试:

词频junit测试的更多相关文章

  1. 复利计算器(软件工程)及Junit测试———郭志豪

    计算:1.本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按复利计算公式来计算就是:1,000,000×(1+3%)^30 客户提出: 2.如果按照单利计算 ...

  2. Junit测试框架 Tips

    关于Junit测试框架使用的几点总结: 1.Junit中的测试注解: @Test →每个测试方法前都需要添加该注解,这样才能使你的测试方法交给Junit去执行. @Before →在每个测试方法执行前 ...

  3. junit测试,使用classpath和file 加载文件的区别

    用junit测试发现一个问题,怎么加载配置文件?一直都出现这样的错误 ERROR: org.springframework.test.context.TestContextManager - Caug ...

  4. Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法

    一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期修改后(不论是增加新功能,修改bug),都可以做到重新测试的工作.以减少我们在发布的时候出现更过甚至 ...

  5. 单元测试实战 - Junit测试

    一.对加法函数进行测试 1.实例化被测单元(方法):类名 实例名=new 类名([参数]) 2.调用被测单元,对比预期值和输出值(实际值): 在没有junit测试工具的情况下,我们要进行如下的测试代码 ...

  6. Android Junit测试框架

    对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定 ...

  7. 在Eclipse中生成接口的JUnit测试类

    在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...

  8. Struts2+Spring+Mybatis+Junit 测试

    Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis  package com.action.kioskmoni ...

  9. Junit测试打印详细的log日志,可以看到sql

    Junit测试打印详细的log日志,可以看到sql 在log4j.xml的日志配置文件中,把日志级别从info级别调整到debug级别: <?xml version="1.0" ...

随机推荐

  1. MySQL导入sql脚本 导出数据库

    导出数据库 不能停止服务 cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) mysqldump -u用户名 -p 数据库名 > 导出的文件名 ...

  2. hdu2639 01背包第K优解

    #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...

  3. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  4. 转:Docker学习---挂载本地目录

    原文: http://my.oschina.net/piorcn/blog/324202 docker可以支持把一个宿主机上的目录挂载到镜像里 docker run -it -v /home/dock ...

  5. GridView中超链接设置

    <%# Eval("id") %>Bind方式    <%# Bind("id","~/info.aspx?id={0}" ...

  6. iOS数据持久化文件读写之偏好设置

    很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能.每个应用都有个NSUserDefaults实例,通过它来存取偏好设置.比如, ...

  7. 完数[HDU1406]

    完数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. Java 集合系列18之 Iterator和Enumeration比较

    概要 这一章,我们对Iterator和Enumeration进行比较学习.内容包括:第1部分 Iterator和Enumeration区别第2部分 Iterator和Enumeration实例 转载请 ...

  9. HDU 3333 & 离线+线段树

    题意: 统计一段区间内不同数字之和.如1 1 2 3 1 统计2---5即1+2+3. SOL: 很少打过离线的题目...这种可离线可在线的题不管怎么样一般都是强行在线... 考虑这题,此前做过一个类 ...

  10. ACM Longest Repeated Sequence

    Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...