单词统计续
package bao; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.*; public class wcontinue { public static void main(String [] args) throws IOException { wordcontinue xm=new wordcontinue(); wordcontinue.meau(); } public static void meau() throws IOException { System.out.println("*1.查找字母*"); System.out.println("*2.查找单词 *"); System.out.println("*3.按单词数查找*"); Scanner sc=new Scanner (System.in); int n=sc.nextInt();//输入数字 switch(n) { case 1:zimu(new File("E:\\eclipse\\p.txt"));break; case 2:danci(new File("E:\\eclipse\\p.txt"));break; case 3:danci2(new File("E:\\eclipse\\p.txt"));break; } sc.close(); } public static void z(File file){ double sum=0.0; BufferedReader bfr = null; //定义字符读取(缓冲)流 try{ bfr = new BufferedReader(new FileReader(file)); String value = null; String newValue = ""; while((value = bfr.readLine())!=null){ //开始读取文件中的字符 newValue = newValue+value; //存入newValue变量中 } char[] ch = newValue.toCharArray();//把newValue变成字符数组 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(Collections.reverseOrder()); for(int x = 0;x<ch.length;x++){ char c = ch[x]; if((c>=65&&c<=90)||(c>=97&&c<=122)) { sum++; if(tm.containsKey(c)){ //如果TreeMap(tm)中有该键,则取出该键中的值,也就是出现的次数 int conut = tm.get(c); tm.put(c,conut+1); //存入把新值存入tm集合中,如果键相同的话, 新键会替换老键,值也随着变化了 } else{ tm.put(c, 1); //如果没有出现该键就说明是第一次出现,然后就存入1次 } } } //下面的是取出TreeMap(tm)中的键和值 Set<Map.Entry<Character, Integer>> set = tm.entrySet(); Iterator<Map.Entry<Character, Integer>> iter = set.iterator(); while(iter.hasNext()){ Map.Entry<Character, Integer> map = iter.next(); char k = map.getKey(); int v = map.getValue(); // double baifenbi=0.0; // baifenbi=v/sum; // System.out.println(baifenbi); System.out.println(k+"("+v+") "); } meau(); } catch(IOException e){ System.out.println("文件读取错误"); } finally{ try{ if(bfr!=null) bfr.close(); } catch(IOException e){ System.out.println("文件关闭错误"); } } } public static void dFile file) throws IOException { BufferedReader br = null; br = new BufferedReader(new FileReader(file)); 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); float sum=0; float a=0; for(int i=0;i<list.size();i++){// 由高到低输出 if(list.get(list.size()-i-1).getKey().equals("the")||list.get(list.size()-i-1).getKey().equals("and")) { } else { a=list.get(list.size()-i-1).getValue(); sum=sum+a; //求总字母数 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); } } System.out.println("单词总数为:"+sum); meau(); } public static void d2(File file) throws IOException { BufferedReader br = null; br = new BufferedReader(new FileReader(file)); 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); BufferedReader br1 = new BufferedReader(new FileReader("E:\\eclipse\\stopwords.txt")); StringBuffer sb1 = new StringBuffer(); String text1 =null; while ((text1=br1.readLine())!= null){ sb1.append(text1);// 将读取出的字符追加到stringbuffer中 } br1.close(); // 关闭读入流 String str1 = sb1.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写 String[] words1 = str1.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词 Map<String ,Integer> map1 = new HashMap<String, Integer>() ; for(String word :words1){ if(map1.get(word)==null){ // 若不存在说明是第一次,则加入到map,出现次数为1 map1.put(word,1); }else{ map1.put(word,map1.get(word)+1); // 若存在,次数累加1 } } // 排序 List<Map.Entry<String ,Integer>> list1 = new ArrayList<Map.Entry<String,Integer>>(map1.entrySet()); Comparator<Map.Entry<String,Integer>> comparator1 = 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(list1,comparator1); float sum=0; float a=0; System.out.printf("请输入单词数"); Scanner sc=new Scanner (System.in); int num=sc.nextInt();//输入数字 for(int i=0;i<num;i++){// 由高到低输出 int b=0; for(int v=0;v<list1.size();v++) { if(list1.get(list1.size()-v-1).getKey().equals(list.get(list.size()-i-1).getKey())) { System.out.println(list.get(list.size()-i-1).getKey()+"单词为无用词汇"); b=1; break; } } if(b==1) { }else { a=list.get(list.size()-i-1).getValue(); sum=sum+a; //求总字母数 System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue()); FileOutputStream fileOutputStream = null; File file1 = new File("E:\\eclipse\\output.txt"); if(!file.exists()){ file.createNewFile(); } fileOutputStream = new FileOutputStream(file1); fileOutputStream.write(list.get(list.size()-i-1).getKey().getBytes("gbk")); fileOutputStream.flush(); fileOutputStream.close(); } } System.out.println("单词总数为:"+sum); meau(); } }
学习于网络