java处理文本计算词频工具类

来源:互联网 发布:手机腾讯视频网络错误1 编辑:程序博客网 时间:2024/06/11 13:19

主要代码:

1、工具方法类

public class WordFreqUtil {


    public static List<Map.Entry<String,Integer>> wordFreqCount(String filePath,int sortType) throws IOException {
        Map<String, Integer> map = new TreeMap<String,Integer>();  //存储单词计数信息,key值为单词,value为单词数
        BufferedReader reader=new BufferedReader(new FileReader(filePath));
        String readLine = null;
        while((readLine = reader.readLine()) != null){
            String [] wordArray = filterWord(readLine);
            for(String word : wordArray) {
                word = word.trim();
                if (word != null && !word.equalsIgnoreCase("")) {
                    if(map.get(word) != null){
                        map.put(word, map.get(word) + 1);
                    }else{
                        map.put(word ,1);
                    }
                }
            }
        }
        return sortWordFreq(map,sortType);
    }


    private static List<Map.Entry<String, Integer>> sortWordFreq(Map<String, Integer> map, final int sortType) {
        List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(sortType==1){
                    return o1.getValue() - o2.getValue();  //升序
                }else{
                    return o2.getValue() - o1.getValue();  //降序
                }
            }
        });
        return list;
    }


    public static String[] filterWord(String line){
        line = line.toLowerCase();
        return line.split("[^a-zA-Z]");
    }
}

2、单词统计类

public class WordCount {
    private int count;
    public WordCount(int count){
        this.count=count;
    }
}

0 0