免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4302 | 回复: 2
打印 上一主题 下一主题

Java统计文档中英文单词个数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-15 16:25 |只看该作者 |倒序浏览
Java统计文档中英文单词个数







Java代码
  1. 1.public class CountWords {   
  2. 2.    public static void main(String[] args) {   
  3. 3.        BufferedReader br = null;   
  4. 4.        try {   
  5. 5.            br = new BufferedReader(new FileReader("english.txt"));   
  6. 6.        } catch (FileNotFoundException e) {   
  7. 7.            e.printStackTrace();   
  8. 8.        }   
  9. 9.        StringBuffer sb = new StringBuffer();   
  10. 10.        String line = null;   
  11. 11.        try {   
  12. 12.            while((line = br.readLine()) != null) {   
  13. 13.                sb = sb.append(line);   
  14. 14.            }   
  15. 15.        } catch (IOException e) {   
  16. 16.            e.printStackTrace();   
  17. 17.        }   
  18. 18.        try {   
  19. 19.            br.close();   
  20. 20.        } catch (IOException e1) {   
  21. 21.            e1.printStackTrace();   
  22. 22.        }   
  23. 23.           
  24. 24.        Pattern pattern = Pattern.compile("[a-zA-Z']+");   
  25. 25.        Matcher matcher = pattern.matcher(sb);   
  26. 26.        Map<String, Integer> map = new HashMap<String, Integer>();   
  27. 27.        String word = "";   
  28. 28.        Integer num = null;   
  29. 29.        int total = 0;   
  30. 30.           
  31. 31.        while(matcher.find()) {   
  32. 32.            word = matcher.group();   
  33. 33.            total ++;   
  34. 34.            if(map.containsKey(word)) {   
  35. 35.                num = map.get(word);   
  36. 36.                num += 1;   
  37. 37.            } else {   
  38. 38.                num = 1;   
  39. 39.            }   
  40. 40.            map.put(word, num);   
  41. 41.        }   
  42. 42.           
  43. 43.        PrintWriter pw = null;   
  44. 44.        try {   
  45. 45.            pw = new PrintWriter(new FileWriter("result.txt"), true);   
  46. 46.        } catch (IOException e) {   
  47. 47.            e.printStackTrace();   
  48. 48.        }   
  49. 49.        Iterator<String> iterator = map.keySet().iterator();   
  50. 50.        while(iterator.hasNext()) {   
  51. 51.            String tmp = iterator.next();   
  52. 52.            pw.println(tmp  + " : " + map.get(tmp));   
  53. 53.        }   
  54. 54.        pw.println("total words : " + total);   
  55. 55.        pw.println("different words : " + map.size());   
  56. 56.        pw.close();   
  57. 57.  
  58. 58.    }   
  59. 59.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-03-15 16:25 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-08-16 18:16 |只看该作者
单词不只是字母的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP