功能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()); } } }
