统计单词个数小工具
来源:互联网 发布:java敏捷开发平台 编辑:程序博客网 时间:2024/04/28 07:00
统计单词个数小工具
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) { StringTokenizertokens = 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(); } }}
0 0
- 统计单词个数小工具
- [小工具]代码统计小工具编写
- python 单词翻译小工具
- JS实现的文章字符串中某个字符总个数统计在线小工具实例
- vc 统计单词个数
- 统计单词个数
- 统计单词个数
- 统计单词个数
- 统计单词个数
- 统计单词个数
- 统计单词个数
- 状态机统计单词个数
- wikioi1040 统计单词个数
- C统计单词个数
- 统计不同单词个数
- 统计单词个数
- acm-统计单词个数
- 17.2统计单词个数
- codeforces 518B Tanya and Postcard
- 天声人語 20150225
- 常用meta整理
- ======c语言告一段落======
- LVS三种模式配置及优点缺点比较
- 统计单词个数小工具
- 自学宝典:10个学习Android开发的网站推荐
- 日经春秋 20150225
- PreferenceActivity-android 参数设置
- Android组织首选项PreferenceScreen及首选项框架 ListPreference
- 日经社説 20150225 M&Aの腕を磨いて世界展開の加速を
- 日经社説 20150225 恩恵だけでない資源・素材安
- Leetcode: N-Queens && N-Queens II
- 爆破吧~2015