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)的更多相关文章

  1. Golang,用map写个单词统计器

    Golang中也有实用的泛型编程模板.如map.据Go官方团队称,其实现为Hash表,而非类似cpp或Java的红黑树.所以理论上速度更能快上几个等级(Hash与红黑树的效率对比可以看我的文章C++中 ...

  2. 软工之词频统计器及基于sketch在大数据下的词频统计设计

    目录 摘要 算法关键 红黑树 稳定排序 代码框架 .h文件: .cpp文件 频率统计器的实现 接口设计与实现 接口设计 核心功能词频统计器流程 效果 单元测试 性能分析 性能分析图 问题发现 解决方案 ...

  3. 给 VS 2010 选一个好用的代码行数统计器(转)

    给 VS 2010 选一个好用的代码行数统计器 分类: Tricks2011-02-25 05:40 3891人阅读 评论(0) 收藏 举报 2010c 推荐一个VS插件,支持2005/2008/20 ...

  4. Guava缓存器源码分析——缓存统计器

    Guava缓存器统计器实现: 全局统计器——         1.CacheBuilder的静态成员变量Supplier<StatsCounter> CACHE_STATS_COUNTER ...

  5. IntelliJ IDEA 2017版 编译器使用学习笔记(九)(图文详尽版);IDE使用的有趣的插件;IDE代码统计器;Mybatis插件

    一.代码统计器,按照名字搜索即可,在file===setting------plugin 使用右键项目:点击自动统计 二.json转实体类 三.自动找寻bug插件 四.Remind me工具 五.检测 ...

  6. java实现 比较两个文本相似度-- java 中文版 simHash 实现 ,

    比较两个文本的相似度 这里采用 simHash 算法 ; 分词是 基于 http://hanlp.linrunsoft.com/ 的开源 中文分词包 来实现分词 ; 实现效果图: 直接上源码: htt ...

  7. 算法笔记_086:蓝桥杯练习 9-2 文本加密(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 先编写函数EncryptChar,按照下述规则将给定的字符c转化(加密)为新的字符:"A"转化"B" ...

  8. 使用Swing组件编写一个支持中文文本编辑程序ChineseTextEdit.java

      import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class C ...

  9. 通过http路径获取文本内容(Java)

    public static String readFileByUrl(String urlStr) { String res = null; try { URL url = new URL(urlSt ...

随机推荐

  1. ES6中新增的数组知识

    JSON数组格式转换 JSON的数组格式就是为了前端快速的把JSON转换成数组的一种格式,我们先来看一下JSON的数组格式怎么写. let  json = {     '0': 'xzblogs', ...

  2. 4-Five-Youth

      ①People are always talking about 'the problem of youth'. If there is one--which I take leave to do ...

  3. Twenty score

    1.上图中有两个人对读书的看法有较大的不同. There are two people in the cartoon who treat books in completely different w ...

  4. 使用Numpy实现卷积神经网络(CNN)

    import numpy as np import sys def conv_(img, conv_filter): filter_size = conv_filter.shape[1] result ...

  5. 5、iptables之nat

    iptables: 显式扩展.网络防火墙 显式扩展:multiport, iprange, string, time, connlimit, limit, state state:无关是哪种协议 /p ...

  6. 使用axios实现上传视频进度条

    这是最终的效果图 先介绍一下axios的使用:中文的axios官方介绍 首先定义一个uploadTest方法,写在vue文件的methods里 该方法有三个参数,分别是参数,和两个回调函数,参数就是我 ...

  7. 抗性基因数据库CARD介绍

    随着抗生素药物的发现及使用,越来越多的耐药菌株由此产生.而耐药菌株的发展则会增加疾病治疗的难度和成本,因此耐药微生物的研究则显得尤为重要.目前,通过对耐药基因的鉴定挖掘能够一定程度上帮助我们揭开耐药机 ...

  8. _attribute_creature

    生物属性控制表 comment 备注 Entry 生物ID,对就creature_template中entry Level 不等于0时改变等级为该值 Health 不等于0时改变生命值为该值 Atta ...

  9. javaSE习题 第二章 基本数据类型和数组

    问答: 1.什么叫标识符,标识符的规则是什么? 用来标志类名,变量名,方法名,类型名,数组名,文件名的有效字符序列称为标识符. 规则:1.由字母,数字,下划线,美元组成.2.标识符第一个字符不能是数字 ...

  10. [osg][opengl]透视投影的参数Perspective

    gluPerspective这个函数指定了观察的视景体(frustum为锥台的意思,通常译为视景体)在世界坐标系中的具体大小,一般而言,其中的参数aspect应该与窗口的宽高比大小相同.比如说,asp ...