统计单词个数小工具

统计单词个数小工具

package learn;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class WorldStatistical {


    public static void main(String[] argvs) {
        WorldStatistical ws = new WorldStatistical();
        ws.dataParser();
    }


    void  dataParser() {

        Map<String, Integer> worldsMap = new TreeMap<String, Integer>();

        String encoding = "UTF-8";
        File dir = new File("C:\\Users\\acer\\Desktop\\许海华\\A Song of Ice and Fire(1-5)");
        File[] fs = dir.listFiles();


        for(int i=0; i<fs.length; i++){
            try{

                String filePath = fs[i].getAbsolutePath();
                File file=new File(filePath);

                if(file.isFile() && file.exists())
                {
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
                    BufferedReader bufferedReader = new BufferedReader(read);

                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null) {


                        StringTokenizer	tokens = new StringTokenizer(lineTxt, " \t");

                        int worldCount = tokens.countTokens();

                        for(int j =0; j < worldCount; j++) {
                            String dirtyWorld = tokens.nextToken().toLowerCase().trim();
                            String curWorld = cleanWorld(dirtyWorld);
                            if(curWorld == null) continue;
                            Integer curCount = worldsMap.get(curWorld);
                            if(curCount == null) {
                                worldsMap.put(curWorld, 1);
                            } else {
                                worldsMap.put(curWorld, curCount+1);
                            }


                        }
                    }
                    bufferedReader.close();
                }
            } catch (Exception e) {
                System.out.print("exception :" );
                e.printStackTrace();
            }
        }

        System.out.println(worldsMap.size());
        writeWorldMap2File("C:\\Users\\acer\\Desktop\\许海华\\A Song of Ice and Fire(1-5)\\dict.txt", worldsMap);
    }


    String cleanWorld(String dirtyWorld) {

        if (dirtyWorld.substring(0, 1).compareTo("a") < 0|| dirtyWorld.substring(0, 1).compareTo("z") > 0) {
            if(dirtyWorld.length() < 3) return null; 
            return cleanWorld(dirtyWorld.substring(1, dirtyWorld.length()));
        } else {
            int indexWorldEnd = 0;
            while(indexWorldEnd < dirtyWorld.length() && dirtyWorld.substring(indexWorldEnd, indexWorldEnd+1).compareTo("a") >= 0 && dirtyWorld.substring(indexWorldEnd, indexWorldEnd+1).compareTo("z") <= 0) {
                indexWorldEnd++;
            }
            if(indexWorldEnd >= 4 && indexWorldEnd < 20) {
                return dirtyWorld.substring(0, indexWorldEnd);
            } else {
                return null;
            }
        }
    }


    void writeWorldMap2File(String fileName, Map<String, Integer> worldsMap){

        File destFile = new File(fileName);
        if (!destFile.exists()) {
            try {
                destFile.createNewFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {

            FileWriter fw = new FileWriter(destFile.getAbsoluteFile(), true);
            BufferedWriter bw = new BufferedWriter(fw);

            for(String world : worldsMap.keySet()) {
                String content =  world + "\t" + worldsMap.get(world); 
                bw.write(content);
                bw.newLine();
            }

            bw.close();

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值