week1词频统计
使用java完成对txt格式的英文短片进行字符提取及统计。
package nenu.softWareProject;
import java.io.*;
import java.util.*;
public class Test2 {
String filename;
//字符计数
public static int count(String filename,String target) throws FileNotFoundException, IOException {
FileReader fr=new FileReader(filename);
BufferedReader br=new BufferedReader(fr);
StringBuilder strb=new StringBuilder();
while(true){
String line=br.readLine();
if(line==null){
break;
}
strb.append(line);
}
String result=strb.toString();
int count=0;
int index=0;
while(true){
index=result.indexOf(target,index+1);
if(index>0){
count++;
}else{
break;
}
}
System.out.println(target+","+count);
br.close();
return count;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("E:\\artical.txt");// 要读的文件路径
InputStreamReader isr = new InputStreamReader(fis);// 字符流
BufferedReader infile = new BufferedReader(isr); // 缓冲
String string;
String file = null;
while ((string = infile.readLine()) != null) {
file += string;
}
String words[];
file = file.toLowerCase();
file = file.replaceAll("[^A-Za-z]", " ");
file = file.replaceAll("\\s+", " ");
words = file.split("\\s+");
String filepath= "E:/artical.txt";
Map<String, Integer> hashMap = new HashMap<String, Integer>();
for (int i = 0; i < words.length; i++) {
String key = words[i];
if (hashMap.get(key) != null) {
int value = ((Integer) hashMap.get(key)).intValue();
value++;
hashMap.put(key, new Integer(value));
} else {
hashMap.put(key, new Integer(1));
}
Test2.count(filepath,key);
}
}
}
代码运行结果:
never,0
give,1
up,1
never,0
lose,1
hope,1
always,2
have,2
faith,1
it,3
allows,1
you,5
to,1
cope,1
trying,1
times,1
will,4
pass,2
as,3
they,1
always,2
do,1
just,1
have,2
patience,1
your,2
dreams,1
will,4
come,1
true,1
so,1
put,1
on,1
a,17
smile,1
you,5
ll,6
live,1
through,1
your,2
pain,1
know,1
it,3
will,4
pass,2
and,1
strength,1
you,5
will,4
gain,1
代码还存在问题,没有排序,输出有重复字符,改进中
week1词频统计的更多相关文章
- 作业3-个人项目<词频统计>
上了一天的课,现在终于可以静下来更新我的博客了. 越来越发现,写博客是一种享受.来看看这次小林老师的“作战任务”. 词频统计 单词: 包含有4个或4个以上的字 ...
- C语言实现词频统计——第二版
原需求 1.读取文件,文件内包可含英文字符,及常见标点,空格级换行符. 2.统计英文单词在本文件的出现次数 3.将统计结果排序 4.显示排序结果 新需求: 1.小文件输入. 为表明程序能跑 2.支持命 ...
- c语言实现词频统计
需求: 1.设计一个词频统计软件,统计给定英文文章的单词频率. 2.文章中包含的标点不计入统计. 3.将统计结果以从大到小的排序方式输出. 设计: 1.因为是跨专业0.0···并不会c++和java, ...
- 软件工程第一次个人项目——词频统计by11061153柴泽华
一.预计工程设计时间 明确要求: 15min: 查阅资料: 1h: 学习C++基础知识与特性: 4-5h: 主函数编写及输入输出部分: 0.5h: 文件的遍历: 1h: 编写两种模式的词频统计函数: ...
- python瓦登尔湖词频统计
#瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...
- Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)
解决问题的方案 Hadoop上的中文分词与词频统计实践 首先来推荐相关材料:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-c ...
- pyspark进行词频统计并返回topN
Part I:词频统计并返回topN 统计的文本数据: what do you do how do you do how do you do how are you from operator imp ...
- 使用storm分别进行计数和词频统计
计数 直接上代码 public class LocalStormSumTopology { public static void main(String[] agrs) { //Topology是通过 ...
- jieba库分词词频统计
代码已发至github上的python文件 词频统计结果如下(词频为1的词组数量已省略): {'是': 5, '风格': 4, '擅长': 4, '的': 4, '兴趣': 4, '宣言': 4, ' ...
随机推荐
- 编写DVWA暴力破解High级别的Python脚本
1. 过程(不查看源代码) 使用burpsuite或者owasp zap抓取数据包,可以看出页面执行过程如下: 首先获取上一次请求的token,然后sleep几秒,最后使用get方法提交验证. 2. ...
- 一道hive面试题(窗口函数)
表student中的数据格式如下: name month degree s1 201801 As1 201802 As1 201803 Cs1 201804 As1 201805 As1 201806 ...
- 关于Win10 环境下Quartus II 15.0器件列表无法下拉的解决方法
不知大家在Windows 10 64位系统环境下使用Quartus II 15.0在新建工程时遇到过这种问题没,在新建工程的过程是选择器件的列表无法下拉,只能看到一个器件型号,如图1所示. 图1 开始 ...
- 自己第一次使用ANTLR遇到的问题
觉得既然是第一次尝试ANTLR嘛,那就来个简单点的,parse Windows 的 hosts 文件吧,结果...... 先上一段 grammar: grammar hosts; hostfile: ...
- 20155220 实验三 敏捷开发与XP实践 实验报告
20155220 实验三 敏捷开发与XP实践 实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim ...
- 微信小程序点击事件
<---------------------------------------------------index文件夹:------------------------------------ ...
- 使用WinIO库实现保护模式下的IO和内存读写
问题已解决: 原因是函数的调用方式与WinIO中不一致,使用的时候漏掉了__stdcall. 函数原定义为: 在实际的GPIO读写中遇到以下问题: SetPortVal可正常写入,但是GetPortV ...
- 我们一起学习WCF 第二篇WCF承载多个接口
前言:现在王大叔养了大批猪,赚了很多钱.但是最近发现养鸡也可以赚很多钱,他就像扩展业务开始养鸡.又过两年他发现市场对狗的需求量很大,他开始养狗.那么他改怎么做呢,不可能去修改猪住的地方把鸭子和狗放里面 ...
- PHP Redis 缓存数据
// 注:只是在此做下记录,有兴趣的可以参考,不做实际教程文档// 配置文件define('CONFIG', [ 'redis-server' => '127.0.0.1', 'redis-po ...
- Qt 链接报错 version `Qt_5' not found
问题: 在Ubuntu 上发布软件包的时候,复制链接库的时候出现Qt 版本找不到的情况: ./libqxcb.so: /usr/lib/x86_64-linux-gnu/libQt5XcbQpa.so ...