课程练习之“单词统计续”
package pre.lq; //20173522 李秦 import java.util.Scanner; public class Main { static Tongji word=new Tongji(); static Tongjizm zm=new Tongjizm(); static Tongjitwo two=new Tongjitwo(); public static void main(String []args) throws Exception { Scanner scn=new Scanner(System.in); while(true) { System.out.print("1,统计第一章中字母出现的频率排序"+"\n" +"2,统计第一章中单词出现的频率的排序"+"\n" +"3,统计第一章中单词出现频率前十的排序"+"\n"); int x=scn.nextInt(); switch (x) { case 1: zm.ZiMu(); break; case 2: word.Word(); break; case 3: two.Wordtwo(); default: System.exit(0); } } } }
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class Tongji { public Tongji() { } public void Word( ) throws Exception { BufferedReader br = new BufferedReader(new FileReader("D:"+File.separator+"第一章.txt")); StringBuffer sb = new StringBuffer(); String text =null; while ((text=br.readLine())!= null) { sb.append(text);// 将读取出的字符追加到stringbuffer中 } br.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) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); for(int i=0;i<list.size();i++) {// 由高到低输出 DecimalFormat df=new DecimalFormat("0.00"); String num=df.format((float)list.get(list.size()-i-1).getValue()/list.size()*100); System.out.println(list.get(list.size()-i-1).getKey() +":"+num+"%"); } } }
package pre.lq; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.text.DecimalFormat; 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 Tongjitwo { public Tongjitwo() { } public void Wordtwo( ) throws Exception { Scanner scn=new Scanner(System.in); BufferedReader br = new BufferedReader(new FileReader("D:"+File.separator+"第一章.txt")); StringBuffer sb = new StringBuffer(); String text =null; while ((text=br.readLine())!= null) { sb.append(text);// 将读取出的字符追加到stringbuffer中 } br.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) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); System.out.println("请输入要输出的前n个"+"\n"); int n=scn.nextInt(); for(int i=0;i<n;i++) {// 由高到低输出 DecimalFormat df=new DecimalFormat("0.00"); String num=df.format((float)list.get(list.size()-i-1).getValue()/list.size()*100); System.out.println(list.get(list.size()-i-1).getKey() +":"+num+"%"); } } }
package pre.lq; import java.io.*; import java.text.NumberFormat; import java.util.*; public class Tongjizm { public Tongjizm() { } public void ZiMu( ) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:"+File.separator+"第一章.txt"))); int[] count = new int[26]; char[] c = new char[1]; int len = br.read(c); while(len!=-1) { if(c[0]<='Z'&&c[0]>='A') { int number = c[0]; count[number-65]++; // COUNT[number-65]++; } if(c[0]<='z'&&c[0]>='a') { int number = c[0]; count[number-97]++; } len = br.read(c); } count=Paixu(count); Print(count); System.out.print("\nb.txt文件读取完毕!"); br.close(); } public static int[] Paixu(int[] count) { int temp; int size=count.length; for(int i=0;i<size-1;i++) { for(int j=i+1;j<size;j++) { if(count[i]<count[j]) { temp=count[j]; count[j]=count[i]; count[i]=temp; } } } return count; } public static void Print(int[] count) { NumberFormat numberFormat = NumberFormat.getInstance(); // 设置精确到小数点后2位 numberFormat.setMaximumFractionDigits(2); int sum=0; for(int i=0;i<count.length;i++) { sum=count[i]+sum; } String[] a=new String[count.length]; for(int i=0;i<count.length;i++) { a[i] = numberFormat.format((float) count[i] / (float) sum * 100); } for(int i=0;i<26;i++) { if(count[i]>0) { char lowerCase = (char)(i+97); System.out.println(lowerCase+"("+count[i]+")"+"("+a[i]+"%)"); } } } }