统计txt文档中的单词个数

public class Bean {

    private char name;
    private double pinlv;
    public Bean(char name,double pl) {
        this.name=name;
        this.pinlv=pl;
    }
    public char getName() {
        return name;
    }
    public void setName(char name) {
        this.name = name;
    }
    public double getPinlv() {
        return pinlv;
    }
    public void setPinlv(double pinlv) {
        this.pinlv = pinlv;
    }

    
        
    }
import java.awt.List;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.ls.LSInput;
    public class zero {
    static int sum=0;
    private static DecimalFormat decimalFormat;


        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("F:\\eclipse\\统计txt文档中的单词个数\\src\\统计txt文档中的单词个数\\c.txt")));
            int[] count  = new int[26];
            int[] COUNT  = new int[26];
            decimalFormat = new DecimalFormat(".00");
            char[] c = new char[1];
            int len = br.read(c);
            while(len!=-1) {
                
                if(c[0]<='Z'&&c[0]>='A') {
                    int number = c[0];
                    COUNT[number-65]++;
                }
                if(c[0]<='z'&&c[0]>='a') {
                    int number = c[0];
                    count[number-97]++;
                }
                len = br.read(c);
            }
            Sum(count, COUNT);
            Print(count, COUNT);
            
            
            
            System.out.print("\nc.txt文件读取完毕!");
            br.close();
            char[] c1=new char[52];
            double []b=new double[52];
            for(int x=0;x<print_doublelist(count).length;x++) {
                b[x]=print_doublelist(count)[x];
            }
            for(int y=0;y<print_doublelist2(COUNT).length;y++) {
                b[y+print_doublelist(count).length]=print_doublelist2(COUNT)[y];
            }
            for(int i=0;i<52;i++) {
                if(i<26)
                c1[i]=(char)(i+97);
                else
                    c[i]=(char)(i+26);
            }
            
            Map<Character, Double> map = new HashMap<Character, Double>();
            ArrayList<Map.Entry<Character, Double>> list = new ArrayList<>();
            
            for(int i=0;i<52;i++) {
                map.put(c1[i], b[i]);                
            }
            for(Map.Entry<Character, Double> entry : map.entrySet()){
                
                list.add(entry);
                
            }
             list.sort(new Comparator<Map.Entry<Character, Double>>(){
                 @Override
                 public int compare(Map.Entry<Character, Double> o1, Map.Entry<Character, Double> o2) {
                     return (int) (o2.getValue()-o1.getValue());} 
             });
                
             for(Map.Entry<Character, Double> entry: list){
                 System.out.println(entry);
            }}
        
        
        public static void Print(int[] count,int[] COUNT) {
            for(int i=0;i<26;i++) {
                if(count[i]>0) {
                    char lowerCase = (char)(i+97);
                    System.out.println(lowerCase+"的频率为:"+decimalFormat.format((float)count[i]/sum));
                }
                if(COUNT[i]>0) {
                    char upperCase = (char)(i+65);
                    System.out.println(upperCase+"的频率为:"+decimalFormat.format((float)COUNT[i]/sum));
                } 
            }
            
        }
        
        
        public static void Sum(int[] count,int[] COUNT ) {
            
            for(int i=0;i<26;i++) {
                sum+=count[i]+COUNT[i];
            }
            System.out.println("和为"+sum);
        }
      public static double[] print_doublelist(int[] count) {
          double a[]=new double[26];
          for(int i=0;i<26;i++) {
              a[i]=count[i]/sum;
          }
          return a;
      }
      
      public static double[] print_doublelist2(int[] COUNT) {
          double a[]=new double[26];
          for(int i=0;i<26;i++) {
              a[i]=COUNT[i]/sum;
          }
          return a;
      }
    }

 

posted @ 2019-06-20 23:08  我叫张小凡  阅读(938)  评论(0编辑  收藏  举报