一种快速统计文档的中文字符的JAVA代码-经管之家官网!

人大经济论坛-经管之家 收藏本站
您当前的位置> 统计>>

统计库

>>

一种快速统计文档的中文字符的JAVA代码

一种快速统计文档的中文字符的JAVA代码

发布:zhukovasky | 分类:统计库

关于本站

人大经济论坛-经管之家:分享大学、考研、论文、会计、留学、数据、经济学、金融学、管理学、统计学、博弈论、统计年鉴、行业分析包括等相关资源。
经管之家是国内活跃的在线教育咨询平台!

经管之家新媒体交易平台

提供"微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯"等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】

提供微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】

现在有个需求,我要统计一个文档集合中每一个中文字符出现的个数,规模在1000份,每一份文档的大小在20KB左右,我用java写了一段代码。个人感触:程序=数据结构+算法。好的数据结构和算法对于程序的性能是不言而喻的 ...
扫码加入统计交流群


现在有个需求,我要统计一个文档集合中每一个中文字符出现的个数,规模在1000份,每一份文档的大小在20KB左右,我用java写了一段代码。
个人感触:程序=数据结构+算法。好的数据结构和算法对于程序的性能是不言而喻的。
其余类见附件
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStream;
  9. import java.io.Reader;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * 说明:当实现自定义容器时,千万不要忘记在进行读写操作时,使用serializable接口
  14. * */
  15. public class WordCount {
  16. public static void writeMap(File Origin,File newFile){
  17. Map<String,Integer> map=getWordCount(Origin);
  18. ObjectOutputStream oos=null;
  19. try {
  20. OutputStream out=new FileOutputStream(newFile);
  21. oos=new ObjectOutputStream(out);
  22. oos.writeObject(map);
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. } catch (IOException e) {

  26. e.printStackTrace();
  27. }finally{
  28. try {
  29. oos.close();
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. System.out.println(map.toString());
  35. }
  36. public static Map<String,Integer> getWordCount(File f){
  37. Reader reader=null;
  38. BufferedReader bf=null;
  39. String line=null;
  40. try {
  41. reader = new FileReader(f);
  42. bf=new BufferedReader(reader);
  43. line=bf.readLine();
  44. } catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }finally{
  49. try {
  50. bf.close();
  51. reader.close();
  52. } catch (IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. MapWords mapword=new MapWords();
  57. for(int i=0;i<line.length();i++){
  58. String m=line.substring(i, i+1);
  59. Nodes node=new Nodes(m);
  60. mapword.addNewNodeElement(m, f.getName(), i);
  61. }
  62. Map<String,Integer> map=new HashMap<String,Integer>();
  63. for(int i=0;i<mapword.getMapSize()-1;i++){
  64. String Keyword=mapword.toGetKeyByIndex(i+1);
  65. Nodes node=mapword.getNode(Keyword);
  66. map.put(Keyword,node.getWordCountAll());
  67. }
  68. return map;
  69. }
  70. public static void writerMapIndex(File Origin,File newFile){
  71. Reader reader=null;
  72. BufferedReader bf=null;
  73. String line=null;
  74. try {
  75. reader = new FileReader(Origin);
  76. bf=new BufferedReader(reader);
  77. line=bf.readLine();
  78. } catch (FileNotFoundException e) {
  79. e.printStackTrace();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }finally{
  83. try {
  84. bf.close();
  85. reader.close();
  86. } catch (IOException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90. MapWords mapword=new MapWords();

  91. for(int i=0;i<line.length();i++){
  92. String m=line.substring(i, i+1);
  93. // Nodes node=new Nodes(m);
  94. // List<String> list=new ArrayList<String>();
  95. // list=node.getDocNameAll();
  96. // seriMapArray[i]=new seriMap(m,list);
  97. mapword.addNewNodeElement(m, Origin.getName(), i);
  98. }

  99. // Set<Nodes> setArray=mapword.toGetValueSet();
  100. // Nodes nodeArray[]=new Nodes[setArray.size()];
  101. // Nodes node[]=setArray.toArray(nodeArray);
  102. // System.out.println("node[].length="+node.length);
  103. // System.out.println("Arrays.toString(node)="+Arrays.toString(node));
  104. // for(int i=1;i<node.length;i++){
  105. // System.out.println("i="+i);
  106. // seriMapArray[i-1]=new seriMap(node[i].getKeyword(),node[i].getDocNameAll());
  107. // }
  108. //
  109. ObjectOutputStream oos=null;
  110. try {
  111. OutputStream out=new FileOutputStream(newFile);
  112. oos=new ObjectOutputStream(out);
  113. oos.writeObject(mapword);
  114. } catch (FileNotFoundException e) {
  115. e.printStackTrace();
  116. } catch (IOException e) {

  117. e.printStackTrace();
  118. }finally{
  119. try {
  120. oos.close();
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126. public static void writerMapIndex(File[] Origin,File newFile){
  127. MapWords mapword=new MapWords();
  128. for(int n=0;n<Origin.length;n++){
  129. Reader reader=null;
  130. BufferedReader bf=null;
  131. String line=null;
  132. try {
  133. reader = new FileReader(Origin[n]);
  134. bf=new BufferedReader(reader);
  135. line=bf.readLine();
  136. for(int i=0;i<line.length();i++){
  137. String m=line.substring(i, i+1);
  138. mapword.addNewNodeElement(m, Origin[n].getName(), i);
  139. }
  140. } catch (FileNotFoundException e) {
  141. e.printStackTrace();
  142. } catch (IOException e) {
  143. e.printStackTrace();
  144. }finally{
  145. try {
  146. bf.close();
  147. reader.close();
  148. } catch (IOException e) {
  149. e.printStackTrace();
  150. }
  151. }
  152. }
  153. ObjectOutputStream oos=null;
  154. try {
  155. OutputStream out=new FileOutputStream(newFile);
  156. oos=new ObjectOutputStream(out);
  157. oos.writeObject(mapword);
  158. } catch (FileNotFoundException e) {
  159. e.printStackTrace();
  160. } catch (IOException e) {

  161. e.printStackTrace();
  162. }finally{
  163. try {
  164. oos.close();
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. }
  170. public static void FileProcessAll(File[] files){
  171. File[] newFile=new File[files.length];
  172. String uri=files[0].getAbsolutePath();
  173. int m=uri.lastIndexOf(File.separator)+1;
  174. String newUrl=uri.substring(0, m);
  175. File f=new File(newUrl+"temp");
  176. if(f.exists()==false){
  177. f.mkdir();
  178. }
  179. for(int i=0;i<files.length;i++){
  180. newFile[i]=new File(f.getAbsoluteFile()+File.separator+files[i].getName());
  181. }
  182. for(int i=0;i<files.length;i++){
  183. FileProcess.processFile(files[i], newFile[i]);
  184. }
  185. }
  186. public static void getWordCountAll(File[] files,File newFile){

  187. MapWords mapword=new MapWords();

  188. for(int j=0;j<files.length;j++){
  189. Reader reader=null;
  190. BufferedReader bf=null;
  191. String line=null;
  192. try {
  193. reader = new FileReader(files[j]);
  194. bf=new BufferedReader(reader);
  195. line=bf.readLine();
  196. for(int i=0;i<line.length();i++){
  197. String m=line.substring(i, i+1);
  198. Nodes node=new Nodes(m);
  199. mapword.addNewNodeElement(m, files[j].getName(), i);
  200. }
  201. } catch (FileNotFoundException e) {
  202. e.printStackTrace();
  203. } catch (IOException e) {
  204. e.printStackTrace();
  205. }finally{
  206. try {
  207. bf.close();
  208. reader.close();
  209. } catch (IOException e) {
  210. e.printStackTrace();
  211. }
  212. }
  213. }
  214. Map<String,Integer> map=new HashMap<String,Integer>();
  215. for(int i=0;i<mapword.getMapSize()-1;i++){
  216. String Keyword=mapword.toGetKeyByIndex(i+1);
  217. Nodes node=mapword.getNode(Keyword);
  218. map.put(Keyword,node.getWordCountAll());
  219. }
  220. ObjectOutputStream oos=null;
  221. try {
  222. OutputStream out=new FileOutputStream(newFile);
  223. oos=new ObjectOutputStream(out);
  224. oos.writeObject(map);
  225. } catch (FileNotFoundException e) {
  226. e.printStackTrace();
  227. } catch (IOException e) {

  228. e.printStackTrace();
  229. }finally{
  230. try {
  231. oos.close();
  232. } catch (IOException e) {
  233. e.printStackTrace();
  234. }
  235. }
  236. System.out.println(map.toString());
  237. System.out.println(map.size());
  238. }
  239. }
复制代码
「经管之家」APP:经管人学习、答疑、交友,就上经管之家!
免流量费下载资料----在经管之家app可以下载论坛上的所有资源,并且不额外收取下载高峰期的论坛币。
涵盖所有经管领域的优秀内容----覆盖经济、管理、金融投资、计量统计、数据分析、国贸、财会等专业的学习宝库,各类资料应有尽有。
来自五湖四海的经管达人----已经有上千万的经管人来到这里,你可以找到任何学科方向、有共同话题的朋友。
经管之家(原人大经济论坛),跨越高校的围墙,带你走进经管知识的新世界。
扫描下方二维码下载并注册APP
本文关键词:

本文论坛网址:https://bbs.pinggu.org/thread-2791616-1-1.html

人气文章

1.凡人大经济论坛-经管之家转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
经管之家 人大经济论坛 大学 专业 手机版