⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 counts.java

📁 一个用Java成功实现的词发分析工具
💻 JAVA
字号:
package hartech.kids.fileKits;

import java.util.Hashtable;
import hartech.*;
import java.io.File;

/**
 *
 * <p>Title: 文学研究助手、程序分析
 *    -- 统计指定目录下(包括其子目录)所有指定类型文件中各种词的出现频率并排序
 *    -- 统计以上所有文件的 文件数、目录数、总行数、注释行数、代码行数、总单词数</p>
 *
 *
 * <p>Description:
 *
 * 使用 JDirectory 类遍历给定目录下所有文件(包括子目录),可过滤文件(如只读java、cpp、txt文件)
 * 使用 JLineReader 类遍历该所有文件中的每一行,每次返回其中一行
 * 使用 pickWords() 方法读取出该行中各个单词(由纯字母组成)
 *
 * 把取出的每个单词加入哈希表,该单词为 key、其出现频率为值,每加一次其对应值(频率)加一
 * 同时记录频率最高的频率值 maxFre
 *
 * 全部加完后建一个字符串数组,数组中元素个数为 maxFre+1
 * 把所有哈希表中单词和其频率写入该字符串:
 *      string[单词频率] + = " -> " + 该单词字符串;
 * 由此实现排序(如 string[maxFre]单词频率最高)
 *
 * 单词中字母忽略大小写
 *
 * linesCount 记录总行数
 * wordsCount 记录总单词数
 *
 * </p>
 *
 * <p>Website: www.hartech.cn </p>
 * <p>Page: http://www.hartech.cn/blog/blogview.asp?logID=86 </p>
 * <p>Date: 2006-09-03 </p>
 */
public class Counts {

  static File file;
  static int maxFre = 1;
  static int linesCount = 0;
  static int commentLinesCount = 0;
  static int wordsCount = 0;
  static int filesCount = 0;
  static int dirsCount = 0;

  // 设置 startFre
  // 只记录出现频率大于 startFre 的单词
  static int startFre = 0;

  // 依次打印出频率从高到低的单词,同频率的同一行
  // 打印总行数、单词数
  public static String print(String[] in) {
    String out = file.getAbsoluteFile() + "\r\n\r\n" +
        "Files:\t\t" + filesCount + "\r\n" +
        "Directorys:\t\t" + dirsCount + "\r\n" +
        "Words:\t\t" + wordsCount + "\r\n" +
        "Lines:\t\t" + linesCount + "\r\n" +
        "    Comment Lines:\t" + commentLinesCount + "\r\n" +
        "    Codes Lines:\t\t" + (linesCount - commentLinesCount) + "\r\n\r\n";
    for (int i = in.length - 1; i > 0; i--) {
      if (!in[i].equals("")) {
        out += " [" + i + "]    " + in[i] + "\r\n";
      }
    }
    //System.out.print(out);
    return out;
  }

  // 根据给定哈希表把里面所有单词放入一字符串数组,索引越高的放入频率越高的单词,实现排序
  public static String[] sort(Hashtable hash) {
    String[] out = new String[maxFre + 1];
    for (int i = 0; i < out.length; i++) {
      out[i] = "";
    }
    // get all keys(words) inside hash table
    String[] word = JString.pickWords(hash.keySet().toString());
    int n = 0;
    for (int i = 0; i < word.length; i++) {
      n = ( (Integer) hash.get(word[i])).intValue();
      if (n > startFre) {
        out[n] += " -> " + word[i];
      }
    }
    return out;
  }

  // 把给定目录下所有文件(包括子目录下)内所有单词加入哈希表,返回该哈希表
  public static Hashtable makeHashTable(String dir, String filter) {
    return makeHashTable(new File(dir), filter);
  }

  public static Hashtable makeHashTable(File dir, String filter) {
    JLineReader lineReader = new JLineReader(dir, filter);

    // 重置所有变量
    file = dir;
    maxFre = 1;
    linesCount = 0;
    commentLinesCount = 0;
    wordsCount = 0;
    filesCount = lineReader.filesCount();
    dirsCount = lineReader.dirsCount();

    Hashtable hash = new Hashtable();
    String line = lineReader.nextLine();
    String[] words = null;
    String word = null;

    Integer fre = 0;
    while (line != null) {
      linesCount++;
      line = line.trim();
      if (line.startsWith("//") || line.startsWith("*") ||
          line.startsWith("/*") || line.startsWith("'")) {
        commentLinesCount++;
      }
      words = JString.pickWords(line);
      for (int i = 0; i < words.length; i++) {
        wordsCount++;
        word = words[i].toLowerCase();
        // 该单词第一次出现
        if ( (fre = (Integer) hash.get(word)) == null) {
          fre = 1;
        }
        else {
          fre++;
          if (fre > maxFre) {
            maxFre = fre.intValue();
          }
        }
        hash.put(word, fre);
      }
      line = lineReader.nextLine();
    }
    return hash;
  }

  public static void main(String[] args) {
    // test:
    System.out.println(print(sort(makeHashTable("E:/1.txt", "java,txt"))));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -