统计文章中字母出现频率:abcdsedgfcvfjghvhgb
class test1{ String zimu;//出现的字母 int cishu;//字母出现的次数 public test1(String zimu,int cishu) { this.zimu = zimu; this.cishu = cishu; } public String getZimu() { return zimu; } public void setZimu(String zimu) { this.zimu = zimu; } public int getCishu() { return cishu; } public void setCishu(int cishu) { this.cishu = cishu; } } public class ZimuCollect { public static void collect() throws IOException { try { //IO操作读取文件内容 FileReader fr = new FileReader("file.txt"); BufferedReader br = new BufferedReader(fr); HashMap<String, Integer> map = new HashMap<String, Integer>(); String string =null; Integer count = 0;//每个字母的次数 Integer total = 0;//总共多少个字母 while ((string=br.readLine())!=null) { char[] ch = string.toCharArray();//将获取的string分成字符数组 total = total + ch.length; for (int i = 0; i < ch.length; i++) { ch[i] = Character.toLowerCase(ch[i]);//将所有的字母变成小写的 count = map.get(ch[i]+""); if (count == null) {//字母没有出现重复; count = 1; }else {//字母出现重复,count+1; count++; } map.put(ch[i]+"", count); } } List<test1> result = new ArrayList<>(); test1 e = null; for (String str : map.keySet()) { e = new test1(str,map.get(str)); result.add(e); } result.sort((test1 e1,test1 e2)->{ return e2.getCishu()-e1.getCishu();}); for(entity ee : result) { System.out.println("字母"+ee.getZimu()+"在文章中出现"+ee.getCishu()+"次,其频率为"+String.format("%.2f",ee.getCishu()*1.0/total)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) throws IOException { try{ ZimuCollect zimucollect = new ZimuCollect(); ZimuCollect.collect(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
思路:
读取文件,将读取到的放在字符数组里,先将其全部变为小写,用map的统计方法统计其出现次数,最后遍历即可。
统计文章中单词出现的频率:
package Test; 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.List; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; class test2{ String danci;//出现的单词 int cishu;//单词对应出现的次数 public test2(String zimu,int cishu) { this.danci = zimu; this.cishu = cishu; } public String getDanci() { return danci; } public int getCishu() { return cishu; } } public class DanciCollect { public static boolean judgeNouse(String str) throws IOException { boolean flag = true; FileReader fr = new FileReader("judge.txt"); BufferedReader bf = new BufferedReader(fr); String str1; // 按行读取字符串 while ((str1 = bf.readLine()) != null) { if(str.equals(str1)) { flag = false; } } bf.close(); fr.close(); return flag; } public static String toLowerCase(String str) { char []StringArr = str.toCharArray(); for (int i = 0; i < StringArr.length; i++) { StringArr[i] = Character.toLowerCase(StringArr[i]); } StringBuffer sb = new StringBuffer(); for(int i = 0;i < StringArr.length;i++) { sb.append(StringArr[i]); } String str1 = sb.toString(); return str1; } public static void collect1() throws IOException { try { File file1 = new File("piao.txt");//定义一个file对象,用来初始化FileReader FileReader reader1 = new FileReader(file1);//定义一个fileReader对象,用来初始化BufferedReader BufferedReader bReader1 = new BufferedReader(reader1);//new一个BufferedReader对象,将文件内容读取到缓存 StringBuilder sb1 = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中 String s1 = ""; while ((s1 =bReader1.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格 sb1.append(s1);//将读取的字符串添加换行符后累加p存放在缓存中 } bReader1.close(); String text = sb1.toString(); int i=0; String[] array = {".",",","?","!",":","‘","’","“","”","—",";","-"}; for (int j = 0; j < array.length; j++) { text = text.replace(array[j]," "); //将text中的array数组中包含的特殊字符用空格代替 } String[] textArray = text.split(" "); //根据空格将text分割并存放在textArray中 Map<String, Integer> map = new TreeMap<String, Integer>(); Integer count = 0;//每个字母的次数 Integer total = 0;//总共多少个字母 while(i < textArray.length) { String str = toLowerCase(textArray[i]); if(!judgeNouse(str)) { total = total + 1; count = map.get(str+""); if (count == null) {//单词没有出现重复; count = 1; }else {//单词出现重复,count+1; count++; } map.put(str+"", count); i++; } else { i++; } } List<test2> result = new ArrayList<>(); test2 e = null; for (String str : map.keySet()) { e = new test2(str,map.get(str)); result.add(e); } result.sort((test2 e1,test2 e2)->{ return e2.getCishu()-e1.getCishu();}); System.out.println("文章共计"+total+"个单词"); for(int ii = 0 ; ii < result.size();ii++) { System.out.println(result.get(ii).getDanci()+"在文章中出现"+result.get(ii).getCishu()+"次,其频率为"+String.format("%.2f",result.get(ii).getCishu()*1.0/total)); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void main(String args[]) throws IOException { try { DanciCollect dancicollect = new DanciCollect(); DanciCollect.collect1(); }catch (FileNotFoundException e) { e.printStackTrace(); } } }
思路:
读取文件中的内容,用append(s)方法将每次读取的内容追加到缓存,将缓存内容放在字符串里,将特殊符号放到一个数组里,然后将这些特殊符号用空格代替,用split(" ")方法将其分成一个个单词存进字符串数组,然后遍历统计单词频率即可。
若去掉无用词,在存进map之前用一个函数判断,不是无用词即存进map里,然后用map来统计单词频率,转换成List数组,用sort函数排序,输出频率最高的前n个单词
这次的代码不是我自己写的,是我借鉴的别人的,因为我自己的那种统计字母与统计单词是完全分开的,不能一个方法来写这两个题,于是从别人那里我学会了使用map方法……