文本统计器(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 ...
随机推荐
- 论文笔记:Attention Is All You Need
Attention Is All You Need 2018-04-17 10:35:25 Paper:http://papers.nips.cc/paper/7181-attention-is-a ...
- uint8_t / uint16_t / uint32_t /uint64_t数据类型详解
uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型? 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...
- 【Ruby】【变量】
知识点[Ruby 中$开头的全局变量.内部变量.隐藏变量介绍] Ruby 中充满了一系列的隐藏变量,我们可以从这些预定义的全局变量中获取一些有意思的信息. 全局进程变量 $$ 表示当前运行的 ruby ...
- appium 计算器demo
需要修改的是 platformVersion deviceName demo: #coding=utf- from appium import webdriver import time desire ...
- python函数名称
一.第一类对象, 函数名的使用 函数名就是变量名, 函数名存储的是函数的内存地址 变量的命名规范: 由数字, 字母, 下划线组成 不能是数字开头, 更不能是纯数字 不能用关键字 不要太长 要有意义 不 ...
- sqlserver 中通配符%和_的使用
--以a开头的数据 SELECT * FROM BCUSTOMER_MZN WHERE CST_NAME LIKE 'A%' --以Z结尾的数据 SELECT * FROM BCUSTOMER_MZN ...
- format格式化函数
注意:列表索引设置参数,‘0’是必须的.
- [原][osg][oe]分析一块倾斜摄影瓦片的数据
RangeMode PIXEL_SIZE_ON_SCREEN 首先我们看看原始数据的构成: 第12层:(第一层) 第23层:(最后一层) pagelod下面有N多的pagelod一层包裹一层 通过os ...
- C#6.0 语法
属性表达式 属性值初始化 public string name {get;set;} = "张三"; 函数表达式 NULL检查运算符 var aa = Created?.Date; ...
- 如何备份MySQL数据库
在MySQL中进行数据备份的方法有两种: 1. mysqlhotcopy 这个命令会在拷贝文件之前会把表锁住,并把数据同步到数据文件中,以避免拷贝到不完整的数据文件,是最安全快捷的备份方法. 命令的使 ...