统计文本文件26个字母的出现频率:
package text; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.NumberFormat; public class word3 { private static String formattedDecimalToPercentage(double decimal) { //获取格式化对象 NumberFormat nt = NumberFormat.getPercentInstance(); //设置百分数精确度2即保留两位小数 nt.setMinimumFractionDigits(2); return nt.format(decimal); } public static void main(String []args) { String a1; char a='A'; int a2[]=new int[27]; char b1[]=new char[26]; char b2[]=new char[26]; for(int i=0;i<26;i++) { b1[i]=a; b2[i]=(char)(a+32); a++; } try { BufferedReader in = new BufferedReader(new FileReader("E:\\eclipse\\p.txt")); String str; while ((str = in.readLine()) != null) { char[] d=str.toCharArray(); for(int i=0;i<d.length-1;i++) { for(int j=0;j<26;j++) { if(b1[j]==d[i]||b2[j]==d[i]) { a2[j]++; } } } } a2[26]=0; for(int i=0;i<26;i++) { a2[26]=a2[i]+a2[26]; } for(int i=0;i<26;i++) { System.out.print(b2[i]); System.out.print("出现的次数为:"); System.out.println(a2[i]); double d=(double)((double)a2[i]/(double)a2[26]); String result2=formattedDecimalToPercentage(d); System.out.println("频率 = "+result2); } } catch (IOException e) { } } }
运行结果·:
统计文本文档中每个单词出现的次数:
package text; import java.io.BufferedReader; import java.io.FileReader; import java.util.*; public class word1 { public static void main(String [] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("E:\\eclipse\\p.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++){// 由高到低输出 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } } }
运行结果: