1. 创建一个类,实现统计文本文件中各类字符和字符串的个数的功能,要求实现:

a) 按字符统计,输出各个字符的数量

b) 按单词统计,输出各个单词的数量

2. 在b)的基础上实现一个类keywordIdentifier,读入一个Java程序源文件,输出各个关键字的个数(注释中出现的关键字不计入关键字个数)

思考:统计1:如果文本文件只包含26个英文字母并且用空格分离,那么只需要使用数组就可以对字符计数,用空格分离得到字符串可以对字符串计数(是否区分大小写问题)。如果文本文件是英文,但是包含各种标点及其他符号,则使用集合key-value的形式来对字符计数,按字符依次读取,然后将26个字母以外的所有字符作为空格处理,再使用空格将其分离,最后对字符串统计计数。

关键字统计:需要所有关键字集合,对每行判断是否为注释,再对非注释进行空格分离,对关键字计数(采用key-value形式)

1.统计字符和单词 StringCounter.java

  1. import java.util.Map;
  2. import java.util.TreeMap;
  3.  
  4. public class StringCounter {
  5.  
  6. private Map<String, Integer> charCount = new TreeMap<String, Integer>();
  7.  
  8. private Map<String, Integer> wordCount = new TreeMap<String, Integer>();
  9.  
  10. public void charCounter(String s) {
  11. Integer freq;
  12. for(int i = 0; i < s.length(); i++) {
  13. freq = charCount.get(s.substring(i, i+1));
  14. if(freq == null) {
  15. freq = new Integer(1);
  16. }
  17. else {
  18. freq = new Integer(freq.intValue() + 1);
  19. }
  20. charCount.put(s.substring(i, i+1), freq);
  21. }
  22. }
  23.  
  24. public void wordCounter(String s) {
  25. //将除过a-zA-Z0-9的其他符号转为空格,再将多个空格转为一个空格
  26. String s1 = s.replaceAll("\\W", " ");
  27. String s2 = s1.replaceAll(" +", " ");
  28. String[] s3 = s2.split(" ");
  29. Integer freq;
  30. for(String str : s3) {
  31. freq = wordCount.get(str);
  32. if(freq == null) {
  33. freq = new Integer(1);
  34. }
  35. else {
  36. freq = new Integer(freq.intValue() + 1);
  37. }
  38. wordCount.put(str, freq);
  39. }
  40. }
  41.  
  42. public void output() {
  43. System.out.println(charCount);
  44. System.out.println(wordCount);
  45. }
  46. }

2.测试 使用的文本文件为随意的英文文本

  1. import java.io.IOException;
  2. import java.io.RandomAccessFile;
  3.  
  4. public class CountTest {
  5.  
  6. public static void main(String[] args) throws IOException {
  7. StringCounter sc = new StringCounter();
  8.  
  9. long filePoint = 0;
  10. String s;
  11. RandomAccessFile file = new RandomAccessFile("string.txt", "r");
  12. long fileLength = file.length();
  13. while(filePoint < fileLength) {
  14. s = file.readLine();
  15. //处理及计数
  16. sc.charCounter(s);
  17. sc.wordCounter(s);
  18. filePoint = file.getFilePointer();
  19. }
  20. file.close();
  21.  
  22. sc.output();
  23. }
  24.  
  25. }

3.关键词统计

  先将所有关键字放入map中,以关键字为键,次数0为值,再读文本统计个数

  1. import java.io.IOException;
  2. import java.io.RandomAccessFile;
  3. import java.util.Map;
  4. import java.util.TreeMap;
  5.  
  6. public class keywordIdentifier {
  7.  
  8. private Map<String, Integer> keyWordCount = new TreeMap<String, Integer>();
  9.  
  10. public void keyWord() throws IOException {
  11. long filePoint = 0;
  12. String s;
  13. RandomAccessFile file = new RandomAccessFile("keyword.txt", "r");
  14. long fileLength = file.length();
  15. while(filePoint < fileLength) {
  16. s = file.readLine();
  17.  
  18. String[] s1 = s.split(" ");
  19. Integer freq = new Integer(0);
  20. for(String word : s1) {
  21. keyWordCount.put(word, freq);
  22. }
  23.  
  24. filePoint = file.getFilePointer();
  25. }
  26. file.close();
  27. }
  28.  
  29. public void keyWordCounter(String s){
  30. int pos = s.indexOf("//");
  31. if(pos == -1) {
  32. pos = s.length();
  33. }
  34. String sub = s.substring(0, pos);
         String sub1 = sub.replaceAll("\\W", " ");
  35. String str = sub1.replaceAll(" +", " ");
  36. String[] s1 = str.split(" ");
  37. Integer freq;
  38. for(String word : s1) {
  39. freq = keyWordCount.get(word);
  40. if(freq != null) {
  41. freq = new Integer(freq.intValue() + 1);
  42. keyWordCount.put(word, freq);
  43. }
  44. }
  45. }
  46.  
  47. public void output() {
  48. System.out.println(keyWordCount);
  49. }
  50.  
  51. }

4.测试关键词统计

  1. import java.io.*;
  2.  
  3. public class CountTest {
  4.  
  5. public static void main(String[] args) throws IOException {
  6. keywordIdentifier sc = new keywordIdentifier();
  7. sc.keyWord();
  8. long filePoint = 0;
  9. String s;
  10. RandomAccessFile file = new RandomAccessFile("CountTest.java", "r");
  11. long fileLength = file.length();
  12. while(filePoint < fileLength) {
  13. s = file.readLine();
  14. sc.keyWordCounter(s);
  15. filePoint = file.getFilePointer();
  16. }
  17. file.close();
  18.  
  19. sc.output();
  20. }
  21.  
  22. }

输出:

{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. 论文笔记: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 ...

  2. uint8_t / uint16_t / uint32_t /uint64_t数据类型详解

    uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型? 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个新的数据类型 ...

  3. 【Ruby】【变量】

    知识点[Ruby 中$开头的全局变量.内部变量.隐藏变量介绍] Ruby 中充满了一系列的隐藏变量,我们可以从这些预定义的全局变量中获取一些有意思的信息. 全局进程变量 $$ 表示当前运行的 ruby ...

  4. appium 计算器demo

    需要修改的是 platformVersion deviceName demo: #coding=utf- from appium import webdriver import time desire ...

  5. python函数名称

    一.第一类对象, 函数名的使用 函数名就是变量名, 函数名存储的是函数的内存地址 变量的命名规范: 由数字, 字母, 下划线组成 不能是数字开头, 更不能是纯数字 不能用关键字 不要太长 要有意义 不 ...

  6. sqlserver 中通配符%和_的使用

    --以a开头的数据 SELECT * FROM BCUSTOMER_MZN WHERE CST_NAME LIKE 'A%' --以Z结尾的数据 SELECT * FROM BCUSTOMER_MZN ...

  7. format格式化函数

    注意:列表索引设置参数,‘0’是必须的.

  8. [原][osg][oe]分析一块倾斜摄影瓦片的数据

    RangeMode PIXEL_SIZE_ON_SCREEN 首先我们看看原始数据的构成: 第12层:(第一层) 第23层:(最后一层) pagelod下面有N多的pagelod一层包裹一层 通过os ...

  9. C#6.0 语法

    属性表达式 属性值初始化 public string name {get;set;} = "张三"; 函数表达式 NULL检查运算符 var aa = Created?.Date; ...

  10. 如何备份MySQL数据库

    在MySQL中进行数据备份的方法有两种: 1. mysqlhotcopy 这个命令会在拷贝文件之前会把表锁住,并把数据同步到数据文件中,以避免拷贝到不完整的数据文件,是最安全快捷的备份方法. 命令的使 ...