教你用java统计目录下所有文档的词频

本文是统计目录下所有文档的词频top10,非单个文档,包含中文和英文。

直接上代码:

  1 package com.huawei.wordcount;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileReader;
  6 import java.io.IOException;
  7 import java.util.ArrayList;
  8 import java.util.Collections;
  9 import java.util.Comparator;
 10 import java.util.HashMap;
 11 import java.util.List;
 12 import java.util.Map;
 13 import java.util.Map.Entry;
 14 
 15 public class WordCount {
 16 
 17     public static void main(String args[]) throws Exception {
 18         String filebashpath = "/Users/gwl/Documents/wordcount/aa/";
 19         HashMap<String, Integer> map = new HashMap<String, Integer>();
 20         printTen(filebashpath, map);
 21     }
 22 
 23     public static void printTen(String filepath, HashMap<String, Integer> map) {
 24 
 25         ArrayList<File> files = getListFiles(filepath);
 26         BufferedReader br = null;
 27         String line = null;
 28 
 29         try {
 30             for (File file : files) {
 31                 //对于swp、swo等这些不正常退出产生的文件进行排除
 32                 if (file.toString().contains(".sw")) {
 33                     continue;
 34                 }
 35                 br = new BufferedReader(new FileReader(file));
 36                 while ((line = br.readLine()) != null) {
 37                     line.toLowerCase();
 38                     //匹配分隔符,包括标点符号和一些特殊字符。
 39                     String reg1 = "\\,|\\。|\\,|\\;|\\ |\\#|\\$|\\^|\\&|\\*|\\?|\\.|\\!|\\:|\\(|\\)|\\+|\\=|\\[|\\]|\\;";
 40                     //输出的单词或语句必须是中文、大小写字母、数字、"-"、"_"组成。
 41                     String reg2 = "^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$";
 42                     String str[] = line.split(reg1);
 43                     for (String s : str) {
 44                         if (s.matches(reg2)) {
 45                             if (!map.containsKey(s)) {
 46                                 map.put(s, 1);
 47                             } else {
 48                                 map.put(s, map.get(s) + 1);
 49                             }
 50                         }
 51                     }
 52                 }
 53             }
 54 
 55         } catch (IOException ioException) {
 56             ioException.printStackTrace();
 57         } finally {
 58             if (br != null) {
 59                 try {
 60                     br.close();
 61                 } catch (IOException e) {
 62                     e.printStackTrace();
 63                 }
 64             }
 65         }
 66 
 67         List<Entry<String, Integer>> list = 
 68                 new ArrayList<Entry<String, Integer>>(map.entrySet());
 69 
 70         Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
 71             // 降序排序
 72             public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
 73                 return o2.getValue().compareTo(o1.getValue());
 74             }
 75         });
 76 
 77         int i = 10;
 78         for (Entry<String, Integer> e : list) {
 79             if (i > 0) {
 80                 System.out.println(e.getKey() + ":" + e.getValue());
 81                 i--;
 82             }
 83         }
 84     }
 85 
 86     public static ArrayList<File> getListFiles(Object obj) {
 87         File directory = null;
 88         
 89         if (obj instanceof File) {
 90             directory = (File) obj;
 91         } else {
 92             directory = new File(obj.toString());
 93         }
 94         
 95         ArrayList<File> files = new ArrayList<File>();
 96         if (directory.isFile()) {
 97             files.add(directory);
 98             return files;
 99         } else if (directory.isDirectory()) {
100             File[] fileArr = directory.listFiles();
101             for (int i = 0; i < fileArr.length; i++) {
102                 File fileOne = fileArr[i];
103                 files.addAll(getListFiles(fileOne));
104             }
105         }
106         
107         return files;
108     }
109 }

注:分隔符正则匹配需要根据各自的情况进行定义。

当然数据量比较大的时候需要采用大数据计算,比如mapreduce,那样的话会简单很多。

posted on 2017-08-17 16:33  running_wolf  阅读(1335)  评论(0编辑  收藏  举报

导航