文本统计器(Java)
1. 创建一个类,实现统计文本文件中各类字符和字符串的个数的功能,要求实现:
a) 按字符统计,输出各个字符的数量
b) 按单词统计,输出各个单词的数量
2. 在b)的基础上实现一个类keywordIdentifier,读入一个Java程序源文件,输出各个关键字的个数(注释中出现的关键字不计入关键字个数)
思考:统计1:如果文本文件只包含26个英文字母并且用空格分离,那么只需要使用数组就可以对字符计数,用空格分离得到字符串可以对字符串计数(是否区分大小写问题)。如果文本文件是英文,但是包含各种标点及其他符号,则使用集合key-value的形式来对字符计数,按字符依次读取,然后将26个字母以外的所有字符作为空格处理,再使用空格将其分离,最后对字符串统计计数。
关键字统计:需要所有关键字集合,对每行判断是否为注释,再对非注释进行空格分离,对关键字计数(采用key-value形式)
1.统计字符和单词 StringCounter.java
import java.util.Map;
import java.util.TreeMap; public class StringCounter { private Map<String, Integer> charCount = new TreeMap<String, Integer>(); private Map<String, Integer> wordCount = new TreeMap<String, Integer>(); public void charCounter(String s) {
Integer freq;
for(int i = 0; i < s.length(); i++) {
freq = charCount.get(s.substring(i, i+1));
if(freq == null) {
freq = new Integer(1);
}
else {
freq = new Integer(freq.intValue() + 1);
}
charCount.put(s.substring(i, i+1), freq);
}
} public void wordCounter(String s) {
//将除过a-zA-Z0-9的其他符号转为空格,再将多个空格转为一个空格
String s1 = s.replaceAll("\\W", " ");
String s2 = s1.replaceAll(" +", " ");
String[] s3 = s2.split(" ");
Integer freq;
for(String str : s3) {
freq = wordCount.get(str);
if(freq == null) {
freq = new Integer(1);
}
else {
freq = new Integer(freq.intValue() + 1);
}
wordCount.put(str, freq);
}
} public void output() {
System.out.println(charCount);
System.out.println(wordCount);
}
}
2.测试 使用的文本文件为随意的英文文本
import java.io.IOException;
import java.io.RandomAccessFile; public class CountTest { public static void main(String[] args) throws IOException {
StringCounter sc = new StringCounter(); long filePoint = 0;
String s;
RandomAccessFile file = new RandomAccessFile("string.txt", "r");
long fileLength = file.length();
while(filePoint < fileLength) {
s = file.readLine();
//处理及计数
sc.charCounter(s);
sc.wordCounter(s);
filePoint = file.getFilePointer();
}
file.close(); sc.output();
} }
3.关键词统计
先将所有关键字放入map中,以关键字为键,次数0为值,再读文本统计个数
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.TreeMap; public class keywordIdentifier { private Map<String, Integer> keyWordCount = new TreeMap<String, Integer>(); public void keyWord() throws IOException {
long filePoint = 0;
String s;
RandomAccessFile file = new RandomAccessFile("keyword.txt", "r");
long fileLength = file.length();
while(filePoint < fileLength) {
s = file.readLine(); String[] s1 = s.split(" ");
Integer freq = new Integer(0);
for(String word : s1) {
keyWordCount.put(word, freq);
} filePoint = file.getFilePointer();
}
file.close();
} public void keyWordCounter(String s){
int pos = s.indexOf("//");
if(pos == -1) {
pos = s.length();
}
String sub = s.substring(0, pos);
String sub1 = sub.replaceAll("\\W", " ");
String str = sub1.replaceAll(" +", " ");
String[] s1 = str.split(" ");
Integer freq;
for(String word : s1) {
freq = keyWordCount.get(word);
if(freq != null) {
freq = new Integer(freq.intValue() + 1);
keyWordCount.put(word, freq);
}
}
} public void output() {
System.out.println(keyWordCount);
} }
4.测试关键词统计
import java.io.*;
public class CountTest {
public static void main(String[] args) throws IOException {
keywordIdentifier sc = new keywordIdentifier();
sc.keyWord();
long filePoint = 0;
String s;
RandomAccessFile file = new RandomAccessFile("CountTest.java", "r");
long fileLength = file.length();
while(filePoint < fileLength) {
s = file.readLine();
sc.keyWordCounter(s);
filePoint = file.getFilePointer();
}
file.close();
sc.output();
}
}
输出:
{abstract=0, assert=0, boolean=0, break=0, byte=0, case=0, catch=0, char=0, class="1", const=0, continue=0, default=0, do=0, double=0, else=0, enum=0, extends=0, final=0, finally=0, float=0, for=0, goto=0, if=0, implements=0, import=1, instanceof=0, int=0, interface=0, long=2, native=0, new=2, package=1, private=0, protected=0, public=2, return=0, short=0, static=1, strictfp=0, super=0, switch=0, synchronized=0, this=0, throw=0, throws=1, transient=0, try=0, void=1, volatile=0, while=1}
文本统计器(Java)的更多相关文章
- Golang,用map写个单词统计器
Golang中也有实用的泛型编程模板.如map.据Go官方团队称,其实现为Hash表,而非类似cpp或Java的红黑树.所以理论上速度更能快上几个等级(Hash与红黑树的效率对比可以看我的文章C++中 ...
- 软工之词频统计器及基于sketch在大数据下的词频统计设计
目录 摘要 算法关键 红黑树 稳定排序 代码框架 .h文件: .cpp文件 频率统计器的实现 接口设计与实现 接口设计 核心功能词频统计器流程 效果 单元测试 性能分析 性能分析图 问题发现 解决方案 ...
- 给 VS 2010 选一个好用的代码行数统计器(转)
给 VS 2010 选一个好用的代码行数统计器 分类: Tricks2011-02-25 05:40 3891人阅读 评论(0) 收藏 举报 2010c 推荐一个VS插件,支持2005/2008/20 ...
- Guava缓存器源码分析——缓存统计器
Guava缓存器统计器实现: 全局统计器—— 1.CacheBuilder的静态成员变量Supplier<StatsCounter> CACHE_STATS_COUNTER ...
- IntelliJ IDEA 2017版 编译器使用学习笔记(九)(图文详尽版);IDE使用的有趣的插件;IDE代码统计器;Mybatis插件
一.代码统计器,按照名字搜索即可,在file===setting------plugin 使用右键项目:点击自动统计 二.json转实体类 三.自动找寻bug插件 四.Remind me工具 五.检测 ...
- java实现 比较两个文本相似度-- java 中文版 simHash 实现 ,
比较两个文本的相似度 这里采用 simHash 算法 ; 分词是 基于 http://hanlp.linrunsoft.com/ 的开源 中文分词包 来实现分词 ; 实现效果图: 直接上源码: htt ...
- 算法笔记_086:蓝桥杯练习 9-2 文本加密(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符:"A"转化"B" ...
- 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class C ...
- 通过http路径获取文本内容(Java)
public static String readFileByUrl(String urlStr) { String res = null; try { URL url = new URL(urlSt ...
随机推荐
- C# 禁止任务管理器关闭
http://www.cnblogs.com/luomingui/archive/2011/06/25/2090130.html 测试了好像没用的.不知道什么原因
- 题解——洛谷P2294 [HNOI2005]狡猾的商人(差分约束)
裸的差分约束 dfs判断负环,如果有负环就false,否则就是true 注意有多组数据,数组要清空 #include <cstdio> #include <algorithm> ...
- 深度学习课程笔记(五)Ensemble
深度学习课程笔记(五)Ensemble 2017.10.06 材料来自: 首先提到的是 Bagging 的方法: 我们可以利用这里的 Bagging 的方法,结合多个强分类器,来提升总的结果.例如: ...
- (转载)Navicat Premium 12.1.16.0安装与激活
声明:本文所提供的所有软件均来自于互联网,仅供个人研究和学习使用,请勿用于商业用途,下载后请于24小时内删除,请支持正版! 本文介绍Navicat Premium 12的安装.激活与基本使用.已于20 ...
- 通过sql语句修改表的结构
1.修改表的列名 oracle: ALTER TABLE 表名 RENAME COLUMN 列名 TO 新列名sqlserver:exec sp_rename '[表名].[列名]','[表名].[新 ...
- Selenium 库
自动化测试工具,支持多种浏览器.爬虫中主要用来解决JavaScript渲染问题. 用法 基本使用 from selenium import webdriver #浏览器驱动对象 from seleni ...
- 为DataGridView增加鼠标滚轮功能
#region 鼠标滚动 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "Wi ...
- 如何查找CpG Islands, CpG shores等 --转载
转载自: http://blog.sciencenet.cn/blog-306699-1033567.html 在homo species物种,随着研究的深入,注释信息不断丰富 针对CpG Islan ...
- Intellij idea注册码失效
从网上下载idea需要输入激活码,晚上用的激活码大多是同一个,但是上次使用的时候突然弹窗告诉我注册码失效了,在网上找到一个新的方法 在注册界面有几个选项,我们常用的是Activation Code,现 ...
- 解决输入git branch 进入编辑状态,mac下出现END,无法返回
core.pager指定 Git 运行诸如log.diff等所使用的分页器,你能设置成用more或者任何你喜欢的分页器(默认用的是less), 当然你也可以什么都不用,设置空字符串: git conf ...