个人作业(统计单词)
输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位:
package test; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class W1 { public static void main(String[] args) throws IOException { try { //IO操作读取文件内容 FileReader fr = new FileReader("d:\\文件\\单词.txt"); BufferedReader br = new BufferedReader(fr); NumberFormat nf = NumberFormat.getInstance(); // 设置精确到小数点后2位 nf.setMaximumFractionDigits(2); 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(); for (int i = 0; i < ch.length; i++) { if (ch[i] > 'A' && ch[i]< 'z') { total++; ch[i] = Character.toLowerCase(ch[i]); count = map.get(ch[i]+""); if (count == null) { count = 1; }else { count++; } map.put(ch[i]+"", count); } } } ArrayList<String> list = new ArrayList<String>(); list.addAll(map.keySet()); //将单词添加到list中 //排序按照出现次数降序排列 for(int i = 0;i < list.size();i++) { for(int j = 0;j < (list.size() - i-1);j++) //list.size() - i-1因为要用到i+1要考虑是否超出范围的问题 { if(map.get(list.get(j)) < map.get(list.get(j+1))) { String t = list.get(j); list.set(j, list.get(j+1)); list.set( j+1, t); } } } for(int i = 0 ; i < list.size();i++) { System.out.println(list.get(i) + ":" + map.get(list.get(i)) +" "+ nf.format((float)(map.get(list.get(i)))*100/total) + "%"); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
截图
1.输出单个文件中的前 N 个最常出现的英语单词。
2.输出文件中所有不重复的单词,按照出现次数由多到少排列,出现次数同样多的,以字典序排列。
3.指定文件目录,对目录下每一个文件执行统计的操作。
4.指定文件目录,是会递归遍历目录下的所有子目录的文件进行统计单词的功能。
package test; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Scanner; public class W2 { public static String StatList(String str) { StringBuffer sb = new StringBuffer(); HashMap<String ,Integer> has = new HashMap<String ,Integer> (); // 打开一个哈希表 String[] slist = str.split("[^a-zA-Z\']+"); for (int i = 0; i < slist.length; i++) { if (!has.containsKey(slist[i])) { has.put(slist[i], 1); } else { has.put(slist[i],has.get(slist[i])+1 ); } } Iterator<String> iterator = has.keySet().iterator(); String a[]=new String[100]; int s[]=new int[100]; int n=15; int judge; Scanner in=new Scanner(System.in); System.out.println("输入前n个最常出现的单词:"); n=in.nextInt(); for(int i=0;i<n;i++) { iterator = has.keySet().iterator(); while(iterator.hasNext()) { String word = (String) iterator.next(); if(s[i]<has.get(word)) { s[i]=has.get(word); a[i]=word; } } judge=woor(a[i]); if(judge==1) { n++; has.remove(a[i]); } else { sb.append("单词:").append(a[i]).append(" 次数").append(has.get(a[i])).append("\r\n"); has.remove(a[i]); } } return sb.toString(); } public static void main(String[] args) { File file = new File("d:\\文件"); // // TODO Auto-generated method stub // ArrayList<String> fil=getFiles("d:\\文件");//目录下的文件 ArrayList<String> fil=getDirectory(file);//目录下的文件和子目录里的文件 for(int i=0;i<fil.size();i++) { String filePath = fil.get(i); //String filePath = "d:\\文件\\单词.txt"; String sz=W3.readTxtFile(filePath); String ltxt=null; System.out.println(ltxt=StatList(sz)); try { W3.daochu(ltxt); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static int woor(String a) { int n=0; String[] biao= {"a","the","an","and"}; for(int i=0;i<biao.length;i++) { if(biao[i].equals(a)) { n=1; } } return n; } public static ArrayList<String> getFiles(String path) { ArrayList<String> files = new ArrayList<String>(); File file = new File(path); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { if (tempList[i].isFile()) { files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { } } return files; } private static ArrayList<String> getDirectory(File file) { ArrayList<String> files = new ArrayList<String>(); ArrayList<String> files1 = new ArrayList<String>(); //获取该目录下的文件列表 File flist[] = file.listFiles(); for (File f : flist) { if (f.isDirectory()) { // 如果f是一个目录 files1=getFiles(f.getAbsolutePath()); files.addAll(files1); getDirectory(f); } else { //如果f是一个文件 files.add(f.getAbsolutePath()); } } return files; } }
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; //import java.io.Reader; //从文本文档中读入 public class W3 { public static String readTxtFile(String filePath){ try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; String lineText=""; while((lineTxt = bufferedReader.readLine()) != null) { lineText+=(lineTxt); } read.close(); return lineText; } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return null; } public static void daochu(String a) throws IOException { File file=new File("d:\\文件\\c.txt"); FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); osw.append(a); osw.close(); fos.close(); } }
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; //import java.io.Reader; //从文本文档中读入 public class W3 { public static String readTxtFile(String filePath){ try { String encoding="GBK"; File file=new File(filePath); if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; String lineText=""; while((lineTxt = bufferedReader.readLine()) != null) { lineText+=(lineTxt); } read.close(); return lineText; } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } return null; } public static void daochu(String a) throws IOException { File file=new File("d:\\文件\\c.txt"); FileOutputStream fos=new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8"); osw.append(a); osw.close(); fos.close(); }
}
截图: