单词统计包括其续
package 五月九号; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; 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 Main { public Main() { // TODO 自动生成的构造函数存根 } public static List<String> l = new ArrayList<String>(); public static void main(String[] args) throws IOException { // TODO 自动生成的方法存根 // 无用词 l.add("the"); l.add("a"); l.add("is"); l.add("are"); l.add("she"); l.add("he"); l.add("and"); duanyu("D://piao.txt"); System.out.println(); int n = 0; while (n != 6) { Scanner shuru = new Scanner(System.in); System.out.println("1.字母"); System.out.println("2.单词"); System.out.println("3.单词前多少个"); System.out.println("4.字母前多个"); System.out.println("5.增加无用单词"); System.out.println("6.退出"); System.out.println(); String str = "D://piao.txt"; n = shuru.nextInt(); switch (n) { case 1: zimu(str); break; case 2: try { danci(str); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } break; case 3: int x = 0; x = shuru.nextInt(); try { danci(str, x); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } break; case 4: x = shuru.nextInt(); zimu(str, x); break; case 5: String b = shuru.next(); AddString(b); break; case 6: System.out.println("退出成功"); break; } } } public static void duanyu(String s) throws IOException { BufferedReader br = new BufferedReader(new FileReader(s)); StringBuffer sb = new StringBuffer(); String text = null; while ((text = br.readLine()) != null) { sb.append(text);// 将读取出的字符追加到stringbuffer中 } br.close(); // 关闭读入流 String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写 System.out.println(str); String a="";//前 String b="";//后 char c=' '; for(int i=0;i<str.length();i++) { //System.out.println(str.charAt(i)); if(str.charAt(i)!=c) { a=str.charAt(i)+""; } else { } } } public static void danci(String s, int x) throws IOException { { BufferedReader br = new BufferedReader(new FileReader(s)); 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); int i1 = 0; for (int i = 0; i < list.size(); i++) {// 由高到低输出 if (returnString(list.get(list.size() - i - 1).getKey()).equals("")) { } else { i1++; if (i1 <= x) { System.out.println(list.get(list.size() - i - 1).getKey() + ":" + list.get(list.size() - i - 1).getValue()); } } } } } public static void danci(String s) throws IOException { { BufferedReader br = new BufferedReader(new FileReader(s)); 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++) {// 由高到低输出 if (returnString(list.get(list.size() - i - 1).getKey()).equals("")) { } else { System.out.println( list.get(list.size() - i - 1).getKey() + ":" + list.get(list.size() - i - 1).getValue()); } } } } public static void zimu(String str) { double[] list = new double[26]; int[] c = new int[26]; for (int k = 0; k < 26; k++) { list[k] = 0; } for (int k = 0; k < 26; k++) { c[k] = k + 65; } String line = ""; int a = 0; int b = 0; int s = 0; try { FileReader fr = new FileReader(str); Scanner in = new Scanner(fr); while (in.hasNext()) { line = in.nextLine(); // System.out.println(line); for (int i = 0; i < line.length(); i++) { a = line.charAt(i); if (a >= 65 && a <= 90) { b = a - 65; } if (a >= 97 && a <= 122) { b = a - 97; } // System.out.println(b); list[b] += 1; s++; } } for (int t = 0; t < 26; t++) { list[t] = list[t] / s; // System.out.println(list[t]); } int t, k; double d; int e; for (t = 0; t < 25; t++) { for (k = 0; k < 25 - t; k++) { if (list[k] < list[k + 1]) { d = list[k + 1]; list[k + 1] = list[k]; list[k] = d; e = c[k + 1]; c[k + 1] = c[k]; c[k] = e; } } } for (t = 0; t < 26; t++) { System.out.print((char) (c[t]) + " "); System.out.println(String.format("%.2f", list[t] * 100) + "%"); } } catch (Exception e) { } } public static void zimu(String str, int x) { double[] list = new double[26]; int[] c = new int[26]; for (int k = 0; k < 26; k++) { list[k] = 0; } for (int k = 0; k < 26; k++) { c[k] = k + 65; } String line = ""; int a = 0; int b = 0; int s = 0; try { FileReader fr = new FileReader(str); Scanner in = new Scanner(fr); while (in.hasNext()) { line = in.nextLine(); // System.out.println(line); for (int i = 0; i < line.length(); i++) { a = line.charAt(i); if (a >= 65 && a <= 90) { b = a - 65; } if (a >= 97 && a <= 122) { b = a - 97; } // System.out.println(b); list[b] += 1; s++; } } for (int t = 0; t < 26; t++) { list[t] = list[t] / s; // System.out.println(list[t]); } int t, k; double d; int e; for (t = 0; t < 25; t++) { for (k = 0; k < 25 - t; k++) { if (list[k] < list[k + 1]) { d = list[k + 1]; list[k + 1] = list[k]; list[k] = d; e = c[k + 1]; c[k + 1] = c[k]; c[k] = e; } } } for (t = 0; t < 26; t++) { if (t < x) { System.out.print((char) (c[t]) + " "); System.out.println(String.format("%.2f", list[t] * 100) + "%"); } } } catch (Exception e) { } } // 增加无用单词 public static void AddString(String b) { l.add(b); } // 无用词表去除 public static String returnString(String s) { boolean flag = true; for (int i = 0; i < l.size(); i++) { if (s.equals(l.get(i))) { flag = false; } } if (flag) { return s; } else { return ""; } } }
还是以前那个问题就是写一个菜单加点限制。