java單詞統計


要求1:輸出某個英文文本文件中26字母出現的頻率,由高到低排序,並顯示字母出現的百分比,精確到小數點后兩位。

 

思路:分別設存放字母和字母出現次數的數組,遍歷文件內容,將字母及出現頻率按由高到低的順序輸出

 

源碼:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class word
{
    static String str="";
    static String str1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    static char ch1 []=str1.toCharArray();//存放字母的數組
    public static double num[]=new double[100];//存放字母出現次數的數組
    public static int sum=0;//出現的字母個數
    //讀取文件內容
    public static void read()
    {  
        Scanner scan  =new Scanner(System.in);
        File file = new File("D:\\h\\halibote\\Harry Potter and the Sorcerer's Stone.txt");
        int score = 0;
        StringBuffer result = new StringBuffer();
        try
        {
            FileReader r = new FileReader(file);
            BufferedReader br = new BufferedReader(r);
            int i=0;
            str=br.readLine();
            while(str!=null)
            {
               for(int j=0;j<str.length();j++)
               {
                   for(int k=0;k<str1.length();k++)
                   {
                       if(str.charAt(j)==str1.charAt(k))
                       {
                           sum++;
                           num[k]++;
                       }
                   }
               }
               str=br.readLine();
           }
            br.close();
            for(int p=0;p<str1.length()-1;p++)
            {
                int o=p;
                for(int q=p;q<str1.length();q++)
                {
                    if(num[o]<num[q])
                    {
                        o=q;
                    }
                }
                if(o!=p)
                {
                    char ff=ch1[o];
                    ch1[o]=ch1[p];
                    ch1[p]=ff;
                    double fff=num[o];
                    num[o]=num[p];
                    num[p]=fff;   
                }
            }
            for(int k=0;k<str1.length();k++)
            {
                   num[k]=num[k]/sum*100;
                   System.out.print(ch1[k]);
                   System.out.printf("%.2f",num[k]);
                   System.out.println("%");
            }   
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 
    public static void main(String[] args)
    {
        read();
    }
}
 

要求2:輸出單個文件中的前n個最常出現的單詞

 

思路:

遍歷文件,讀取所有單詞並存入數組

對讀取的單詞進行去重並存入新數組

統計單詞出現次數並將所統計每個單詞的出現次數存入一數組

 

按出現次數由高到低的順序輸出n個單詞及出現次數

 

源碼

import java.io.File;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; 
public class word1

    private static String str="";
    private static Scanner sc=new Scanner(System.in);
    private static BufferedReader cin=null;
    private static String a[]=new String[1000000];//存放從文件中讀取的所有單詞
    private static String c[]=new String[10000000];//存放去重后單詞
    private static int b[]=new int[1000000];//存放單詞出現次數
    private static int length=0;//單詞總個數
    private static int length1=0;//去重后單詞個數
    private static int nn=0;
    private static int j=0;
    static File[] list = new File("D:\\h").listFiles();
   
    //讀取文件內容
    public static void Readfile()
    {
        File file=new File("D:\\h\\halibote\\Harry Potter and the Sorcerer's Stone.txt");                               
        try
        {
            InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");
            cin=new BufferedReader(read);
            str=cin.readLine();
            cun();
            cin.close();
            read.close();
        }
        catch(IOException e) {
            System.out.println("讀取失敗!");
            e.printStackTrace();
        }
    }
   
    //將單詞存到數組a
    public  static void cun() throws IOException
    {
     { 
      while(str!=null)
      {
       int i=0;
       str=str.toLowerCase(); //把大寫改成小寫
       for(i=0;i<str.length();i++)
       {
        if((str.charAt(i)>96&&str.charAt(i)<123))
        {                   
         a[j]=a[j]+str.charAt(i);   
        }
        if(str.charAt(i)==' '||str.charAt(i)==','||str.charAt(i)=='.')
        {
         if(!a[j].equals(""))
         {
          j=j+1;
          a[j]="";
         }
        }  
       }             
       str=cin.readLine();
      }
      length=j;
     }
    }
    //去重
    public static void Statistics()
    {
        for(int k=0;k<length;k++)
        {
            b[k]=0;
        }
        c[0]=a[0];
        int tt=1;
        Boolean rt=true;
        for(int i=1;i<length;i++)
        {
            rt=false;
            for(int j=0;j<tt;j++)
            {
                if(a[i].equals(c[j]))
                {
                    rt=true;
                    break;
                }
            }
            if(!rt)
            {
                c[tt]=a[i];
                tt++;
            }
        }
        length1=tt;
        for(int i=0;i<length1;i++)
        {
            for(int j=0;j<length;j++)
            {
                if(c[i].equals(a[j]))
                {
                    b[i]++;
                }
            }
        }
    }
   
    //排序
    public  static void Sorting()
    {
        int t3=0;
        int t2=0;
        String sr="";
        for(int i=0;i<length1-1;i++)
        {
            t3=i;
            for(int j=i+1;j<length1;j++)
            {
                if(b[t3]<b[j])
                {
                    t3=j;
                }
            }
            if(t3!=i)
            {
               t2=b[i];
               b[i]=b[t3];
               b[t3]=t2;
               sr=c[i];
               c[i]=c[t3];
               c[t3]=sr;
            }
         }
    }
   
    //顯示
    public  static void show()
    {
        for(int k=0;k<nn;k++)
        {
            System.out.print(c[k]+"\t"+b[k]+"   ");
            System.out.printf("%.2f",(double)b[k]/length1*100);
            System.out.print("%");
            System.out.println("");
        }
    }
  
    public static void main(String[] args) throws IOException
    {
           System.out.println("請輸入需要統計的個數:");
           nn=sc.nextInt();
           a[0]="";
           Readfile();                   
           Statistics();
           Sorting();
           show();  
    } 
}
 
功能1:輸出文件中所有不重復的單詞,按照出現次數由多到少排列,出現次數同樣多的,以字典序排列
 
思路:只需將輸出結果改為單詞加出現次數並寫入文件,其他與要求2一致
 
源碼:
import java.io.File;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Scanner;
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; 
public class word2

    private static String str="";
    private static Scanner sc=new Scanner(System.in);
    private static BufferedReader cin=null;
    private static String a[]=new String[1000000];
    private static String c[]=new String[10000000];
    private static int b[]=new int[1000000];
    private static int length=0;
    private static int length1=0;
    private static int nn=0;
    private static int j=0;
    static File[] list = new File("D:\\h").listFiles();
    public static void Readfile()
    {
        File file=new File("D:\\h\\halibote\\Harry Potter and the Sorcerer's Stone.txt");                               
        try
        {
            InputStreamReader read = new InputStreamReader(new FileInputStream(file),"UTF-8");
            cin=new BufferedReader(read);
            str=cin.readLine();
            cun();
            cin.close();
            read.close();
        }
        catch(IOException e) {
            System.out.println("讀取失敗!");
            e.printStackTrace();
        }
    }
   
    //將單詞存到數組a
    public  static void  cun() throws IOException
    {
     {
      while(str!=null)
      {
       int i=0;
       str=str.toLowerCase(); //把大寫改成小寫
       for(i=0;i<str.length();i++)
       {
        if((str.charAt(i)>96&&str.charAt(i)<123))
        {                   
         a[j]=a[j]+str.charAt(i);   
        }
        if(str.charAt(i)==' '||str.charAt(i)==','||str.charAt(i)=='.')
        {
         if(!a[j].equals(""))
         {
          j=j+1;
          a[j]="";
         }
                    }
       }             
       str=cin.readLine();
      }
      length=j;
     }
    }
   
    //去重
    public static void Statistics()
    {
        for(int k=0;k<length;k++)
        {
            b[k]=0;
        }
        c[0]=a[0];
        int tt=1;
        Boolean rt=true;
        for(int i=1;i<length;i++)
        {
            rt=false;
            for(int j=0;j<tt;j++)
            {
                if(a[i].equals(c[j]))
                {
                    rt=true;
                    break;
                }
            }
            if(!rt)
            {
                c[tt]=a[i];
                tt++;
            }
        }     
        length1=tt;
        for(int i=0;i<length1;i++)
        {
            for(int j=0;j<length;j++)
            {
                if(c[i].equals(a[j]))
                {
                    b[i]++;
                }
            }
        }
    }
   
    //排序
    public  static void  Sorting()
    {
        int t3=0;
        int t2=0;
        String sr="";
        for(int i=0;i<length1-1;i++)
        {
            t3=i;
            for(int j=i+1;j<length1;j++)
            {
                if(b[t3]<b[j])
                {
                    t3=j;
                }
            }
           if(t3!=i)
           {
               t2=b[i];
               b[i]=b[t3];
               b[t3]=t2;
               sr=c[i];
               c[i]=c[t3];
               c[t3]=sr;
           }
        }
    }
    //將輸出結果寫入文本文件
    public static void Writefile() throws IOException
    {
        File file=new File("D:\\h\\halibote\\t1.txt");
        if(!file.exists())
            file.createNewFile();
        FileWriter write = new FileWriter(file,true);
        BufferedWriter out=new BufferedWriter(write);
        for(int i=0;i<length1;i++)
        {
            StringBuffer sb=new StringBuffer();
            out.write("這是第"+(i+1)+"個: ");   
            out.write(c[i]+"\t"+b[i]);
            out.write("\r\n");
        }       
        out.close();
    }
   
    //顯示
    public static void show1()
    {
        for(int k=0;k<length1;k++)
        {
                System.out.print(c[k]+"\t \t\t"+b[k]+"\n");       
        }
    }
   
    public static void main(String[] args) throws IOException
    {
           a[0]="";
           Readfile();                   
           Statistics();
           Sorting(); 
           System.out.println("程序中所有不重復的單詞!");
           show1();
           Writefile();
    } 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM