有趣的统计英文单词频率的例子.docx
有趣的统计英文单词频率的例子统计一篇英文文档或一本小说中单词出现的次数,下面代码使用的是英文版小说"悲惨世界"做例子。 有两个需要注意的地方,一个是如何使用正则式分割单词,一个是HashMap中对元素按值排序无法直接完成,中间做了一下转化: Java代码 1.import java.io.BufferedReader; 2.import java.io.File; 3.import java.io.FileReader; 4.import java.io.FileWriter; 5.import java.io.IOException; 6.import java.util.ArrayList; 7.import java.util.Collections; 8.import java.util.HashMap; 9.import java.util.List; 10.import java.util.regex.Pattern; 11. 12.public class EnglishWordsStatics 13. public static final String EN_FOLDER_FILE = "C:/resources/Books/English/Les Miserables.txt" 14. public static final String OUTPUT = "C:/resources/Books/English/Les Miserables - Words.txt" 15. 16. private HashMap<String, Integer> result = new HashMap<String, Integer>(); 17. private int total = 0; 18. 19. /* 20. * Handle one English fiction 21. * 22. * param file 23. * throws IOException 24. */ 25. public void handleOneFile(File file) throws IOException 26. if (file = null) 27. throw new NullPointerException(); 28. 29. BufferedReader in = new BufferedReader(new FileReader(file); 30. String line; 31. 32. / split by space ' ( ) * + ' . / 0-9 : ; ? | 33. Pattern pattern = Pattern 34. .compile(" ,?;.!"'|0-9:-()+"); 35. 36. while (line = in.readLine() != null) 37. line = line.toLowerCase(); 38. String words = pattern.split(line); 39. 40. for (String word : words) 41. if (word.length() > 0) 42. total+; 43. if (!result.containsKey(word) 44. result.put(word, 1); 45. else 46. Integer i = result.get(word); 47. i+; 48. result.put(word, i); 49. 50. 51. 52. 53. in.close(); 54. System.out.println("Total words: " + total); 55. System.out.println("Total different words: " + result.size(); 56. 57. 58. /* 59. * Print the statics result 60. * throws IOException 61. */ 62. public void saveResult() throws IOException 63. / Sorting 64. List<Node> list = new ArrayList<Node>(); 65. for (String word : result.keySet() 66. Node p = new Node(word, result.get(word); 67. list.add(p); 68. 69. 70. Collections.sort(list); 71. 72. FileWriter fw = new FileWriter (new File (OUTPUT); 73. for (Node p : list) 74. fw.write(p.getWord() + "t" + p.getNum()+"n"); 75. 76. fw.close(); 77. System.out.println ("Done"); 78. 79. 80. /* 81. * param args 82. */ 83. public static void main(String args) throws IOException 84. EnglishWordsStatics ews = new EnglishWordsStatics(); 85. ews.handleOneFile(new File(EN_FOLDER_FILE); 86. ews.saveResult(); 87. 88. 89. 90./* 91. * For sorting, store the words - num 92. * 93. */ 94.class Node implements Comparable<Node> 95. private String word; 96. private int num; 97. 98. public Node() 99. 100. 101. public Node(String word, int num) 102. super(); 103. this.word = word; 104. this.num = num; 105. 106. 107. public String getWord() 108. return word; 109. 110. 111. public int getNum() 112. return num; 113. 114. 115. Override 116. public int compareTo(Node o) 117. return o.getNum() - num; 118. 119.