词频统计------实训一
词频统计
老五在寝室吹牛他熟读过《鲁滨逊漂流记》,在女生面前吹牛热爱《呼啸山庄》《简爱》和《飘》,在你面前说通读了《战争与和平》。但是,他的四级至今没过。你们几个私下商量,这几本大作的单词量怎么可能低于四级,大家听说你学习《构建之法》,一致推举你写个程序名字叫wf,统计英文作品的单词量并给出每个单词出现的次数,准备用于打脸老五。
1.结对成员:16012009徐小东,16012008李雪 码云地址:https://gitee.com/changchundehui/09-08/blob/master/Wf.java
2.编程过程照片:

3设计思路:这个题的要求是利用java语言统计英文作品的单词量并给出每个单词出现的次数,按照这个要求给出如下设计。
整个词频统计设计分三个部分,第一部分是读取文件并把所有单词截出来。第二部分统计词频。第三部分把词频排序并输出。
实现功能:1.第一部分:是主程序逻辑入口,这个程序有三种模式,1.单行语言处理,2.单个文件处理,3.批量处理,0.退出程序\n请键入1 2 3选择您需要的模式,模式2指定具体路径为d盘根目录。设计了三种模式:输入1执行功能一,输入2执行功能二,输入3执行功能三。
public class Wf {
public static void main(String[] args) {
//主程序逻辑入口
while (true) {
System.out.println("本程序有三种模式:1.单行语言处理;2.单个文件处理;3.批量处理;0.退出程序\n请键入1 2 3选择您需要的模式,模式2指定具体路径为d盘根目录");
Scanner readerScanner = new Scanner(System.in);
int flag = readerScanner.nextInt();
if (flag == 0) {
break;
设计了三种模式:输入1执行功能一,输入2执行功能二,输入3执行功能三。
功能一:读取一行单词
else if (flag == 1) {
try {
System.out.println("当前为当行语言处理模式,请输入您要评测的语句");
BufferedReader bf =new BufferedReader(new InputStreamReader(System.in)); //读取命令行中一行
String s=bf.readLine();
LineCode(s);
} catch (IOException ex) {
System.out.println("请按单行输入句子");
}
功能二:命令行输入英文作品文件名称
else if (flag == 2) {
System.out.println("当前为单个文件处理模式,请输入您要输入的文件名,格式:aaa.txt");
String s = readerScanner.next();
try {
TxtCode(s);
} catch (Exception ex) {
System.out.println("请输入正确的文件名称,确定后文件存在以及文件是否放在d:根目录下");
}
功能三.批量统计
else if(flag==3){
System.out.println("当前为批量文件处理模式,请输入文件具体路径,格式:d:/ljr");
String path=readerScanner.next();
File file =new File(path);
if (file.isDirectory()) {
File[] filelist =file.listFiles();
for(File file1:filelist){
try {
String s=file1.getPath();//地址回溯
System.out.println(s);
FileCode(s);
} catch (Exception ex) {
System.out.println("请输入正确的路径,若程序无法结束请重新运行程序");
}
第二部分:统计单个文件,统计单行
//统计单个文件
public static void TxtCode(String txtname) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("D:/word.txt" ));
List<String> lists = new ArrayList<String>(); //存储过滤后单词的列表
String readLine = null;
while ((readLine = br.readLine()) != null) {
String[] wordsArr1 = readLine.split("[^a-zA-Z]"); //过滤出只含有字母的
for (String word : wordsArr1) {
if (word.length() != 0) { //去除长度为0的行
lists.add(word);
}
}
}
br.close();
StatisticalCode(lists);
}
//统计单行
public static void LineCode(String args) {
List<String> lists = new ArrayList<String>(); //存储过滤后单词的列表
String[] wordsArr1 = args.split("[^a-zA-Z]"); //过滤出只含有字母的
for (String word : wordsArr1) {
if (word.length() != 0) { //去除长度为0的行
lists.add(word);
}
}
StatisticalCode(lists);
}
public static void FileCode(String args) throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(args));
List<String> lists = new ArrayList<String>(); //存储过滤后单词的列表
String readLine = null;
while ((readLine = br.readLine()) != null) {
String[] wordsArr1 = readLine.split("[^a-zA-Z]"); //过滤出只含有字母的
for (String word : wordsArr1) {
if (word.length() != 0) { //去除长度为0的行
lists.add(word);
}
}
}
br.close();
StatisticalCode(lists);
}
public static void StatisticalCode(List<String> lists) {
//统计排序
Map<String, Integer> wordsCount = new TreeMap<String, Integer>(); //存储单词计数信息,key值为单词,value为单词数
//单词的词频统计
for (String li : lists) {
if (wordsCount.get(li) != null) {
wordsCount.put(li, wordsCount.get(li) + 1);
} else {
wordsCount.put(li, 1);
}
}
// System.out.println("wordcount.Wordcount.main()");
SortMap(wordsCount); //按值进行排序
}
第三部分:按value 大小进行排序
public static void SortMap(Map<String, Integer> oldmap) {
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldmap.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue(); //降序
}
});
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getKey() + ": " + list.get(i).getValue());
}
}
4.功能运行结果
功能一:
功能二:

功能三:
5.小结感受:通过这次的结对编程,让我体会到了一个团队的合作力量是非常大的,虽然在编程的过程中也遇到过困难,但在两个人的探讨下问题迎刃而解。在编程的过程中遇到不懂得问题会查阅一些资料,在互帮互助的情况下两个人又知道了很多知识,在学习java的过程中,有一些不懂得地方,在这次结对编程过程中,通过两人的交流,也都弄懂了。所以我认为结对编程能够带来1+1>2的效果。
6.评价:在这次的结对编程中,我的小伙伴真的很用心,有一些我不太懂的问题能给我耐心讲解。有一些需要完成的任务做的也很认真。在两人的合作下成功的完成了这次的编程。我们俩打字都比较慢,所以编程的时间比较久,希望能够提高打字的速度。期待下一次的合作。
浙公网安备 33010602011771号