统计单词Java

功能0:输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位。

功能1:输出文件中所有不重复的单词,按照出现次数由多到少排列,出现次数同样多的,以字典序排列。

功能2: 指定文件目录,对目录下每一个文件执行统计的操作。 

功能3:指定文件目录,是会递归遍历目录下的所有子目录的文件进行统计单词的功能。

功能4:输出出现次数最多的前 n 个单词,  例如, 提示统计统计前多少名:输入10。 就是输出最常出现单词的前 10 名。 当没有指明数量的时候,我们默认列出所有单词的频率。

用简单的文件读取,将字节转换为字母,并通过ASCII码将读取出来的大写字母变为小写字母,最后再排序

最后运用HashMap类,键存放单词,只存放出现的次数。

源代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class TongJiDanCi {
    public static void main(String[] args)throws IOException//扔掉很重要
    {
         File file = new File("D:/c1.txt");
         System.out.println("0.统计英文字母个数及其百分比    1.统计英文单词个数");
         Scanner sc = new Scanner(System.in);
         int value=sc.nextInt();
         while(true) {
         switch(value) {
                  case 0:txtString(file);
                  case 1:txtString2(file);
                  default:break;
         }
         }
    }
    
    /*
     * 统计字母
     */
    public static void txtString(File file) throws IOException{
        try {
            //IO操作读取文件内容
            FileReader fr = new FileReader(file);
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(fr);//构造一个BufferedReader类来读取文件        
            HashMap<String, Integer> hm = new HashMap<String, Integer>();//构建了一个新的HashMap对象,强制指定这个HashMap必须是以String为key, 以Integer为值。 
            String line =null;
            Integer count = 0;//每个字母的个数
            Integer total = 0;//统计字母总数,作百分比用
            while ((line=br.readLine())!=null) {
                char[] ch = line.toCharArray();//将字符串对象中的字符转换为一个字符数组。
                total = total + ch.length;
                for (int i = 0; i < ch.length; i++) {
                    ch[i] = Character.toLowerCase(ch[i]);//将大写字符转换为小写,小写字母不变
                    count = hm.get(ch[i]+"");//ch[i]+""的作用是加一个空格后,括号内转化为字符串
                    if (count == null) {
                        count =1;//只出现一次
                    }else {
                        count++;
                    }
                    hm.put(ch[i]+"", count);
                }
            }
            for (String str : hm.keySet()) {//设变量str获取键
                System.out.println(str+"个数:"+hm.get(str)+"        "+hm.get(str)*100.0/total+"%");
            }
            System.out.println("总字母个数:"+total);
            
            //排序输出
             List<Map.Entry<String,Integer>> list_Data = new ArrayList<Map.Entry<String,Integer>>(hm.entrySet());  
                Collections.sort(list_Data, new Comparator<Map.Entry<String,Integer>>(){   
                      public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2)  
                      {  
                      //o1 to o2升序   o2 to o1降序
                      return o2.getValue().compareTo(o1.getValue());
                      }  
                  });  
                System.out.println("排序输出:"); 
                System.out.println(hm); 
            

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    
    /*
     * 统计单词
     */
     public static void txtString2(File file)  throws IOException{
         FileReader fr = new FileReader(file);
         BufferedReader br = new BufferedReader(fr);//构造一个BufferedReader类来读取文件    
         StringBuffer sb = new StringBuffer();
         String line =null;
         while ((line=br.readLine())!=null){
            sb.append(line);//将读取出的字符追加到stringbuffer中
        }     
        fr.close();
       // 关闭读入流
             String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
             String[] words = str.split("[^(a-zA-Z)]+");  // 非单词的字符来分割,得到所有单词
             Map<String ,Integer> map = new HashMap<String, Integer>() ;
             for(String word :words){
                 if(map.get(word)==null){  // 若不存在说明是第一次,则加入到map,出现次数为1
                     map.put(word,1);
                 }else{
                     map.put(word,map.get(word)+1);  // 若存在,次数累加1
                 }
             }
             // 排序
            List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
             Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
                 public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
                  int i=left.getValue()-right.getValue();
                     if(i==0) {
                      return (right.getKey().compareTo(left.getKey()));
                     }
                     return (left.getValue().compareTo(right.getValue()));
                 }
             };
           
             // 集合默认升序
             Collections.sort(list,comparator);
             int n=list.size();
             System.out.println("一共有"+n+"种单词");
             System.out.println("请输入你要排序前几个单词:");
             Scanner scanner=new Scanner(System.in);
             n=scanner.nextInt();
             for(int i=0;i<n;i++){// 由高到低输出
                 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue());
             }
     }
     
    
}
原文地址:https://www.cnblogs.com/sengzhao666/p/10860480.html