• 课堂测试-统计单词个数和字母出现频率


    testtwo.class

    package classthird;
    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.text.DecimalFormat;
    import java.util.HashMap;
    import java.util.Map;
    
    
    
    
    public class testtwo {
        public Map<String,Integer> map1=new HashMap<String,Integer>();
        public static void main(String arg[]) {
            test("D:\Test\a.txt");
            
            
            
        }
        public static void test(String path) {
             try {
                    char shu[] = new char[1000];//存储单个字母内容
                    char zimu[] = new char[26];//存储字母‘a-z’
                    String h1[]=new String[1000];    //存储原文内容
                    String word3[]=new String[1000];    //存储单词
                    String word4[]=new String[1000];    //存储单词
                    String word5[]=new String[1000];    //存储单词
                    int countword[]=new int[1000];
                    int countwordtwo=0;
                    double count[]=new double[26];
                    int j=0;
                    String pathname=path;
                    File filename=new File(pathname);
                    InputStreamReader reader=new InputStreamReader(new FileInputStream(filename));
                    BufferedReader br=new BufferedReader(reader);
                    String line[]=new String[100];;    
                    for(int i=0;i<line.length;i++)
                    {
                        line[i]=br.readLine();    
                    }
                    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[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]='g';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[24]++;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]++;
                         }
                    }    
                    int ci=0;
                    double sum=0;
                    double a1;
                    double max=0;
                    DecimalFormat df = new DecimalFormat( "0.00");    
                    System.out.println("短文中各字母出现情况统计如下:");
                    for(int i=0;i<26;i++) {
                        if(count[i]!=0) {
                            sum+=count[i];
                        }
                    }///求字母总数
                    for (int x = 0; x < 26 - 1; x++) {
                        for (int y = x + 1; y < 26; y++) {
                            if (count[x] > count[y]) {
                                double temp = count[x];
                                count[x] = count[y];
                                count[y] = temp;
                                char temp1=zimu[x];
                                zimu[x] = zimu[y];
                                zimu[y] = temp1;
                            }
                        }
                    }
                    for(int i=0;i<26;i++)
                    {            
                        if(count[i]!=0) 
                        {
                            ci++;
                            a1=count[i]/sum*100;
                            System.out.println(ci+".字母"+zimu[i]+"的出现频率是:"+df.format(a1)+"%");   
                        }
                    }
                    
                    System.out.println("字母共计:"+sum+"个");
                    for(int i=0;i<shu.length;i++) 
                    {
                        h1[i] = String.valueOf(shu[i]);
                    }
                    int msg1=0;
                    int count1=0; //判断单词个数
                    String word2 ="";
                    for(int i=0;i<shu.length;i++) 
                    {
                        
                        if((shu[i]>='a'&&shu[i]<='z')||(shu[i]>='A'&&shu[i]<='Z')) 
                        {    
                            word2+=h1[i];
                        }else
                        {
                            msg1=1;  
                        }
                        if(word3[i]==" ") 
                        {                //考虑标点加空格的情况
                            msg1=0;        //msg1初始化
                            word3[i]="";
                            continue;    //跳出循环
                        }
                        if(msg1==1) 
                        {                    //如果中间出现非字母
                            word3[i]=word2;    //word2存入数组
                            word2="";       //word2初始化
                            msg1=0;            //msg1初始化
                            
                        }
                        if(word3[i]==null) 
                        {     //判断为空,防止空指针
                            msg1=0;                //msg1初始化
                            word3[i]="空";
                            continue;            //跳出循环
                        }
                        if(word3[i]=="") 
                        {     //判断为空,防止空指针
                            msg1=0;                //msg1初始化
                            word3[i]="空";
                            continue;            //跳出循环
                        }
                        if(word3[i].length()==1) 
                        {                                //若长度为一,是单字母不为单词
                            msg1=0;                        //msg1初始化
                            word3[i]="";
                            continue;                    //跳出循环
                        }
                        //System.out.println(word3[i]);
                        word4[count1]=word3[i];
                        count1++;
                        if(word3[i]==""&&word3[i-1]=="") 
                        {
                            break;
                        }
                    }
                    
                    for(int i=0;i<word4.length;i++)
                    {
                        if(word4[i]!=null) {
                            //System.out.println(word4[i]);
                        }else {
                            break;
                        }
                    }
                    int thought_a=0;
                    for(int i=0;i<word4.length;i++)
                    {
                        word5[thought_a]=word4[i];
                    }
                   
                }catch (Exception e) 
                {
                    e.printStackTrace();
                }
        }
        public void daoru(String path) throws IOException
        {
            
            File a=new File(path);
            FileInputStream b = new FileInputStream(a);
            InputStreamReader c=new InputStreamReader(b,"UTF-8");
            String string2=new String();
            while(c.ready())
            {
                char string1=(char) c.read();
                if(!isWord(string1))
                {
                    if(map1.containsKey(string2))
                    {
                        Integer num1=map1.get(string2)+1;
                        map1.put(string2,num1);
                    }
                    else
                    {
                        Integer num1=1;
                        map1.put(string2,num1);
                    }
                    string2="";
                }
                else
                {
                    string2+=string1;
                }
            }
            if(!string2.isEmpty())
            {
                if(map1.containsKey(string2))
                {
                    Integer num1=map1.get(string2)+1;
                    map1.put(string2,num1);
                }
                else
                {
                    Integer num1=1;
                    map1.put(string2,num1);
                }
                string2="";
            }
            c.close();
            b.close();
        }
        public void testthird(String path) {
            String sz[];
            Integer num[];
            final int MAXNUM=10; //统计的单词出现最多的前n个的个数
            sz=new String[MAXNUM+1];
            num=new Integer[MAXNUM+1];
            testtwo tt=new testtwo();
            int account =1;
            //Vector<String> ve1=new Vector<String>();
            try {
                tt.daoru(path);
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            System.out.println("英文单词的出现情况如下:");
            int g_run=0;
    
            for(g_run=0;g_run<MAXNUM+1;g_run++)
            {
                account=1;
                for(Map.Entry<String,Integer> it : tt.map1.entrySet())
                {
                    if(account==1)
                    {
                        sz[g_run]=it.getKey();
                        num[g_run]=it.getValue();
                        account=2;
                    }
                    if(account==0)
                    {
                        account=1;
                        continue;
                    }
                    if(num[g_run]<it.getValue())
                    {
                        sz[g_run]=it.getKey();
                        num[g_run]=it.getValue();
                    }
                    //System.out.println("英文单词: "+it.getKey()+" 该英文单词出现次数: "+it.getValue());
                }
                tt.map1.remove(sz[g_run]);
            }
            int g_count=1;
            String tx1=new String();
            for(int i=0;i<g_run;i++)
            {
                if(sz[i]==null)
                    continue;
                if(sz[i].equals(""))
                    continue;
                tx1+="出现次数第"+(g_count)+"多的单词为:"+sz[i]+"			出现次数: "+num[i]+"
    ";
                System.out.println("出现次数第"+(g_count)+"多的单词为:"+sz[i]+"			出现次数: "+num[i]);
                g_count++;
            }
            try {
                tt.daochu(tx1);
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        public void daochu(String txt) throws IOException
        {
            File fi=new File("tongji.txt");
            FileOutputStream fop=new FileOutputStream(fi);
            OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8");
            ops.append(txt);
            ops.close();
            fop.close();
        }
        
        public boolean isWord(char a)
        {
            if(a<='z'&&a>='a'||a<='Z'&&a>='A')
                return true;
            return false;
        }
        public void testthird1(String name) {
            // TODO Auto-generated method stub
            
        }
        
    }

    testthird.class

    package classthird;
    
    import java.io.File;
    import java.io.IOException;
    
    import classthird.testtwo;
    public class TestThird {
         public static void main(String[] args) throws IOException {
             
                 testtwo tt=new testtwo();
                File file = new File("D:\Test");
    
                String strArray[] = file.list();
                for (String s : strArray) {
                    System.out.println("文件名:" + s);
                }
                System.out.println("-----------------------------");
                File fileArray[] = file.listFiles();
                for (File f : fileArray) {
                    //System.out.println("文件名:" + f.getName());
                    String thought_a;
                    thought_a=f.getCanonicalPath();
                    tt.test(thought_a);
                    tt.testthird(thought_a);
                }
            }
    }

  • 相关阅读:
    出乎意料的else语句
    2015年全国谷歌卫星地图离线数据存储方案
    Convert Sorted List to Binary Search Tree
    程序猿学习资料分享---爱分享的程序猿(新浪微博)
    myeclipse中断点调试
    J2EE之13个规范标准概念
    ora-01036: 非法的变量名/编号
    CCNA Cloud CLDFND 210-451 QUIZ: Server Virtualization
    已知二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
    UML类图中的几种关系总结
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10809274.html
Copyright © 2020-2023  润新知