单词统计

通过文件操作将书本导入程序,在通过逐行读取统计一本书中的单词出现频率

源码:

package dao;import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.text.NumberFormat;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 Dao{

    static String str;

    static boolean flag=true;

    static int i;

    static int j;

    static int m;

    static Scanner scan=new Scanner(System.in);

    public static void main(String[] args) {

        System.out.println("运行成功,请稍等!");

        readFile();

        while(flag) {

            System.out.print("输入1查询字母所占的比例,输入2查询占前几个的单词,输入3退出:");

            int n=scan.nextInt();

            switch(n) {

            case 1:

                findletter();               

 break;

            case 2:

                findword();               break;

            case 3:

                System.out.println("退出成功!");

                flag=false;

                break;

            }

        }

    }

    public static  void readFile() {

        String pathname = "Harry Potter and the Sorcerer's Stone.txt";

try (FileReader reader = new FileReader(pathname);

             BufferedReader br = new BufferedReader(reader)

        ) {

            String line;

            while ((line = br.readLine()) != null) {

                str+=line;

            }

        }

        catch (IOException e) {

            e.printStackTrace();

        }

    }

    public static void findletter() {

        char s[]=str.toCharArray();

        int[] a=new int[52];

        char[] b=new char[52];

        String str1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        b=str1.toCharArray();

        for(j=0;j<str.length();j++) {

            if(s[j]>='a'&&s[j]<='z') {

                m=s[j]-'a';

                a[m]++;

            }

            else if(s[j]>='A'&&s[j]<='Z') {

                m=s[j]-'A'+26;

                a[m]++;

            }

        }

        int t = 0;

        char ts;

        for(i=0;i<52;i++) {

            for(j=i+1;j<52;j++) {

                if(a[i]<a[j]) {

                    t=a[i];

                    a[i]=a[j];

                    a[j]=t;

                    ts=b[i];

                    b[i]=b[j];

                    b[j]=ts;

                }

            }

        }

        double sum=0;

        for(i=0;i<52;i++) {

            sum+=a[i];

        }

        NumberFormat nt = NumberFormat.getPercentInstance();

        nt.setMinimumFractionDigits(2);

        for(i=0;i<52;i++) {

            System.out.println(b[i]+" "+nt.format(a[i]/sum*1.0));

        }

    }

    public static void findword() {

        str.toLowerCase();

        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.put(word,1);

            }else{

                map.put(word,map.get(word)+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) {

             int i=left.getValue()-right.getValue();

                if(i==0) {

                 return (right.getKey().compareTo(left.getKey()));

                }

                return (left.getValue().compareTo(right.getValue()));

            }

        };

      

        // 集合默认升序        Collections.sort(list,comparator);

        int n=list.size();

        System.out.println("共有"+n+"单词");

        System.out.println("请输入你要排序前几个单词:");

        Scanner scanner=new Scanner(System.in);

        n=scanner.nextInt();

        for(int i=0;i<n;i++){/            System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue());

        }    

    }

}

截图:

 

posted @ 2020-05-01 16:58  wumingxiaozu  阅读(84)  评论(0编辑  收藏  举报