• 字母频率


    package 字母频率;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.io.StreamTokenizer;
    import java.util.HashMap;
    import java.util.Scanner; 
    
    public class main {
        static void a() {
        try {
            char shu[] = new char[1000000];
            char zimu[] = new char[52];
            int j=0;
            int count[]=new int[52];
            String pathname="C:\\\\\\\\Users\\\\\\\\xxoo\\\\\\\\Desktop\\\\\\\\飘.txt";
            File filename=new File(pathname);
            InputStreamReader reader=new InputStreamReader(new FileInputStream(filename));
            BufferedReader br=new BufferedReader(reader);
            String line[]=new String[1000];;    
            for(int i=0;i<line.length;i++){
            line[i]=br.readLine(); 
    //        System.out.println(line[i]);
            }
            
            br.close();
            int k=0;
             while(line[k]!=null) {
                 for(int i=0;i<line[k].length();i++) {                                
                      shu[j]=line[k].charAt(i);
                      j++;                     
                 }
             k++;
            }    
            for(int i=0;i<shu.length;i++) {
                 switch(shu[i]) {
                 case 'a':zimu[0]='a';count[0]++;break;
                 case 'b':zimu[1]='b';count[1]++;break;
                 case 'c':zimu[2]='c';count[2]++;break;
                 case 'd':zimu[3]='d';count[3]++;break;
                 case 'e':zimu[4]='e';count[4]++;break;
                 case 'f':zimu[5]='f';count[5]++;break;
                 case 'g':zimu[6]='g';count[6]++;break;
                 case 'h':zimu[7]='h';count[7]++;break;
                 case 'i':zimu[8]='i';count[8]++;break;
                 case 'j':zimu[9]='j';count[9]++;break;
                 case 'k':zimu[10]='k';count[10]++;break;
                 case 'l':zimu[11]='l';count[11]++;break;
                 case 'm':zimu[12]='m';count[12]++;break;
                 case 'n':zimu[13]='n';count[13]++;break;
                 case 'o':zimu[14]='o';count[14]++;break;
                 case 'p':zimu[15]='p';count[15]++;break;
                 case 'q':zimu[16]='q';count[16]++;break;
                 case 'r':zimu[17]='r';count[17]++;break;
                 case 's':zimu[18]='s';count[18]++;break;
                 case 't':zimu[19]='t';count[19]++;break;
                 case 'u':zimu[20]='u';count[20]++;break;
                 case 'v':zimu[21]='v';count[21]++;break;
                 case 'w':zimu[22]='w';count[22]++;break;
                 case 'x':zimu[23]='x';count[23]++;break;
                 case 'y':zimu[24]='y';count[24]++;break;
                 case 'z':zimu[25]='z';count[25]++;break;
                 case 'A':zimu[26]='A';count[26]++;break;
                 case 'B':zimu[27]='B';count[27]++;break;
                 case 'C':zimu[28]='C';count[28]++;break;
                 case 'D':zimu[29]='D';count[29]++;break;
                 case 'E':zimu[30]='E';count[30]++;break;
                 case 'F':zimu[31]='F';count[31]++;break;
                 case 'G':zimu[32]='G';count[32]++;break;
                 case 'H':zimu[33]='H';count[33]++;break;
                 case 'I':zimu[34]='I';count[34]++;break;
                 case 'J':zimu[35]='G';count[35]++;break;
                 case 'K':zimu[36]='K';count[36]++;break;
                 case 'L':zimu[37]='L';count[37]++;break;
                 case 'M':zimu[38]='M';count[38]++;break;
                 case 'N':zimu[39]='N';count[39]++;break;
                 case 'O':zimu[40]='O';count[40]++;break;
                 case 'P':zimu[41]='P';count[41]++;break;
                 case 'Q':zimu[42]='Q';count[42]++;break;
                 case 'R':zimu[43]='R';count[43]++;break;
                 case 'S':zimu[44]='S';count[44]++;break;
                 case 'T':zimu[45]='T';count[45]++;break;
                 case 'U':zimu[46]='U';count[46]++;break;
                 case 'V':zimu[47]='V';count[47]++;break;
                 case 'W':zimu[48]='W';count[48]++;break;
                 case 'X':zimu[49]='X';count[49]++;break;
                 case 'Y':zimu[50]='Y';count[50]++;break;
                 case 'Z':zimu[51]='Z';count[51]++;
                 }
            }    
            int ci=0;
            int sum=0;
            System.out.println("短文中各字母出现情况统计如下:");
            for(int i=0;i<52;i++)
            {
                if(count[i]!=0) {
                    ci++;
                    sum+=count[i];
                    }
            } 
    
           ci=0;
            for(int i=0;i<52;i++)
            {
                if(count[i]!=0) {
                    ci++;
                    double a=(double) ((Math.round(count[i]*100)/100.0)/sum)*100;
                    double b= (double) (Math.round(a*100)/100.0);
                    System.out.println(ci+".字母"+zimu[i]+"的出现次数是:"+count[i]+"	"+b+"%");
                    }
            }
            System.out.println("字母共计:"+sum+"个");
        }catch (Exception e) {
            e.printStackTrace();
        }
        }
    
        
        static void b() throws IOException 
        {
            HashMap<String, Integer>mp = new HashMap<>();
            mp.put("jack", 1);
            mp.put("Tom", 2);
            mp.put("rose", 3);
            mp.put("jack",4);
            try (BufferedReader in = new BufferedReader(new FileReader("C:\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\xxoo\\\\\\\\\\\\\\\\Desktop\\\\\\\\\\\\\\\\飘.txt"))
                    ){
                    String line;
                    int c = 0;
                    while(null != (line = in.readLine())) {
                        
                        String[] words = line.split("\W");//单词的边界                
                        for(String w : words) {
                            if(w.trim().length() == 0) continue;
    //                        if(w.trim().length() == 1) {
    //                            //长度为1
    //                            char cc = w.charAt(0);
    //                            if(!Character.isLetterOrDigit(c)) {
    //                                continue;
    //                            }
    //                        }
                            w = w.toLowerCase();//不分大小写的统计                    
                            if(!mp.containsKey(w)) {
                                mp.put(w, 1);
                            }else {
                                int n = mp.get(w);
                                mp.put(w, n+1);
                            }                    
                        }
                        
                    }
                    
                    for(String key : mp.keySet()) {
                        int v = mp.get(key);
                        System.out.println(key+"	"+v);                }
                    
                } catch (Exception e) {
                    // TODO: handle exception
                }
        }
    
        static void b(int a) throws IOException 
        {
            
            HashMap<String, Integer>mp = new HashMap<>();
            mp.put("jack", 1);
            mp.put("Tom", 2);
            mp.put("rose", 3);
            mp.put("jack",4);
            try (BufferedReader in = new BufferedReader(new FileReader("C:\\\\\\\\\\\\\\\\Users\\\\\\\\\\\\\\\\xxoo\\\\\\\\\\\\\\\\Desktop\\\\\\\\\\\\\\\\飘.txt"))
                    ){
                    String line;
                    int c = 0;
                    while(null != (line = in.readLine())) {
                        
                        String[] words = line.split("\W");//单词的边界                
                        for(String w : words) {
                            if(w.trim().length() == 0) continue;
    //                        if(w.trim().length() == 1) {
    //                            //长度为1
    //                            char cc = w.charAt(0);
    //                            if(!Character.isLetterOrDigit(c)) {
    //                                continue;
    //                            }
    //                        }
                            w = w.toLowerCase();//不分大小写的统计                    
                            if(!mp.containsKey(w)) {
                                mp.put(w, 1);
                            }else {
                                int n = mp.get(w);
                                mp.put(w, n+1);
                            }                    
                        }
                        
                    }
                    
                    for(String key : mp.keySet()) {
                        int v = mp.get(key);
                        System.out.println(key);
                        System.out.println(v);
                    }
                    
                } catch (Exception e) {
                    // TODO: handle exception
                }
        }
        
        public static void main(String[] args) throws IOException {
            int a=1;
            while(a!=0)
            {
                System.out.println("请选择:1 输出单词频率,2 输出不重复单词,3 输出前n个数量多的单词,4 去掉常用词,0 退出");
                Scanner sc = new Scanner(System.in); 
                a = sc.nextInt(); 
                switch(a)
                {
                case 1:a();System.out.println("================================================");break;
                case 2:b();System.out.println("================================================");break;
                case 3:     
                    System.out.println("输出前几个数量多的单词");
                    Scanner sc1 = new Scanner(System.in); 
                    int n= sc1.nextInt(); 
                    b(n);
                    System.out.println("================================================");break;
                case 4:System.out.println("================================================");break;
                case 0:break;
                }
            }
        }
        
        
        
    }
  • 相关阅读:
    Windows下_findnext()异常问题
    Windows 10正式版官方原版镜像!(备忘)
    qt中出现error: C2059: 语法错误:“namespace”未定义等大量错误的问题
    linux shell的简单思维导图
    高仿花生壳客户端程序(qt)
    公告
    修改注册表自定义键盘快捷键
    斜率优化
    元旦老人与丛林
    CF 1466G Song of the Sirens
  • 原文地址:https://www.cnblogs.com/sonofdemon/p/10864830.html
Copyright © 2020-2023  润新知