词频junit测试
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测试的更多相关文章
- 复利计算器(软件工程)及Junit测试———郭志豪
计算:1.本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按复利计算公式来计算就是:1,000,000×(1+3%)^30 客户提出: 2.如果按照单利计算 ...
- Junit测试框架 Tips
关于Junit测试框架使用的几点总结: 1.Junit中的测试注解: @Test →每个测试方法前都需要添加该注解,这样才能使你的测试方法交给Junit去执行. @Before →在每个测试方法执行前 ...
- junit测试,使用classpath和file 加载文件的区别
用junit测试发现一个问题,怎么加载配置文件?一直都出现这样的错误 ERROR: org.springframework.test.context.TestContextManager - Caug ...
- Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法
一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期修改后(不论是增加新功能,修改bug),都可以做到重新测试的工作.以减少我们在发布的时候出现更过甚至 ...
- 单元测试实战 - Junit测试
一.对加法函数进行测试 1.实例化被测单元(方法):类名 实例名=new 类名([参数]) 2.调用被测单元,对比预期值和输出值(实际值): 在没有junit测试工具的情况下,我们要进行如下的测试代码 ...
- Android Junit测试框架
对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定 ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- Struts2+Spring+Mybatis+Junit 测试
Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis package com.action.kioskmoni ...
- Junit测试打印详细的log日志,可以看到sql
Junit测试打印详细的log日志,可以看到sql 在log4j.xml的日志配置文件中,把日志级别从info级别调整到debug级别: <?xml version="1.0" ...
随机推荐
- MySQL主主复制3
一.创建并授权用户 在每一台(主)服务器上创建一个用户,并为之授权,使它们可以互相访问彼此的数据库 在Server-1上: 创建一个充许Server-2来访问的用户server2,密码为:server ...
- ez_setup.py(安装python下setuptools用)
#!python"""Bootstrap setuptools installation If you want to use setuptools in your pa ...
- SU Demos-05Sorting Traces-02Demos
运行结果: 不足之处,欢迎批评指正.
- 如何用distinct消除重复记录的同时又能选取多个字段值?
http://www.cnblogs.com/warioland/archive/2012/05/30/2526128.html
- JSP获取客户端的IP地址的方法
//声明以下方法 <%! public String getRemortIP(HttpServletRequest request) { if (request.getHeader(" ...
- HDU4609 3-idiots(母函数 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men wh ...
- sqlSQL2008如何创建定时作业
SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...
- 如何清除swap里的文件
vi打开一个文件的时候突然断网,再次连接上去vi打开的时候提示在swap里面已经存在一个.要删除这个文件怎么办呢? 如下: 关了swapoff -a后 再ls -al查看 把文件所在目录里*.swp结 ...
- Set集合对象比较两个元素的方法
Set集合对象比较两个元素的方法并不是根据“equals()”方法的返回值来比较.而是用“hashCode()”方法来进行判断.只要两个元素的“hashCode()”方法的返回值相同,就认为两个元素相 ...
- [SDOI2011]染色 题解
题目大意: 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段) 思路: 树剖之后 ...