楼主: zhukovasky
1691 0

[程序分享] 一种快速统计文档的中文字符的JAVA代码 [推广有奖]

marshal

已卖:32份资源

教授

61%

还不是VIP/贵宾

-

威望
0
论坛币
551 个
通用积分
8.8026
学术水平
46 点
热心指数
49 点
信用等级
30 点
经验
18723 点
帖子
1106
精华
0
在线时间
1805 小时
注册时间
2008-7-27
最后登录
2022-6-18

楼主
zhukovasky 发表于 2013-12-17 21:09:13 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
现在有个需求,我要统计一个文档集合中每一个中文字符出现的个数,规模在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.                        
  27.                         e.printStackTrace();
  28.                 }finally{
  29.                         try {
  30.                                 oos.close();
  31.                         } catch (IOException e) {
  32.                                 e.printStackTrace();
  33.                         }
  34.                 }
  35.                 System.out.println(map.toString());
  36.         }
  37.         public static Map<String,Integer> getWordCount(File f){
  38.                 Reader reader=null;
  39.                 BufferedReader bf=null;
  40.                 String line=null;
  41.                 try {
  42.                         reader = new FileReader(f);
  43.                         bf=new BufferedReader(reader);
  44.                         line=bf.readLine();
  45.                 } catch (FileNotFoundException e) {
  46.                         e.printStackTrace();
  47.                 } catch (IOException e) {
  48.                         e.printStackTrace();
  49.                 }finally{
  50.                         try {
  51.                                 bf.close();
  52.                                 reader.close();
  53.                         } catch (IOException e) {
  54.                                 e.printStackTrace();
  55.                         }
  56.                 }
  57.                 MapWords mapword=new MapWords();
  58.                 for(int i=0;i<line.length();i++){
  59.                         String m=line.substring(i, i+1);
  60.                         Nodes node=new Nodes(m);
  61.                         mapword.addNewNodeElement(m, f.getName(), i);
  62.                 }
  63.                 Map<String,Integer> map=new HashMap<String,Integer>();
  64.                 for(int i=0;i<mapword.getMapSize()-1;i++){
  65.                         String Keyword=mapword.toGetKeyByIndex(i+1);
  66.                         Nodes node=mapword.getNode(Keyword);
  67.                         map.put(Keyword,node.getWordCountAll());
  68.                 }
  69.                 return map;
  70.         }
  71.         public static void writerMapIndex(File Origin,File newFile){
  72.                 Reader reader=null;
  73.                 BufferedReader bf=null;
  74.                 String line=null;
  75.                 try {
  76.                         reader = new FileReader(Origin);
  77.                         bf=new BufferedReader(reader);
  78.                         line=bf.readLine();
  79.                 } catch (FileNotFoundException e) {
  80.                         e.printStackTrace();
  81.                 } catch (IOException e) {
  82.                         e.printStackTrace();
  83.                 }finally{
  84.                         try {
  85.                                 bf.close();
  86.                                 reader.close();
  87.                         } catch (IOException e) {
  88.                                 e.printStackTrace();
  89.                         }
  90.                 }
  91.                 MapWords mapword=new MapWords();

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

  100. //                Set<Nodes> setArray=mapword.toGetValueSet();
  101. //                Nodes nodeArray[]=new Nodes[setArray.size()];
  102. //                Nodes node[]=setArray.toArray(nodeArray);
  103. //                System.out.println("node[].length="+node.length);
  104. //                System.out.println("Arrays.toString(node)="+Arrays.toString(node));
  105. //                for(int i=1;i<node.length;i++){
  106. //                        System.out.println("i="+i);
  107. //                        seriMapArray[i-1]=new seriMap(node[i].getKeyword(),node[i].getDocNameAll());
  108. //                }
  109. //               
  110.                 ObjectOutputStream oos=null;
  111.                 try {
  112.                         OutputStream out=new FileOutputStream(newFile);
  113.                         oos=new ObjectOutputStream(out);
  114.                         oos.writeObject(mapword);
  115.                 } catch (FileNotFoundException e) {       
  116.                         e.printStackTrace();
  117.                 } catch (IOException e) {
  118.                        
  119.                         e.printStackTrace();
  120.                 }finally{
  121.                         try {
  122.                                 oos.close();
  123.                         } catch (IOException e) {
  124.                                 e.printStackTrace();
  125.                         }
  126.                 }
  127.         }
  128.         public static void writerMapIndex(File[] Origin,File newFile){
  129.                 MapWords mapword=new MapWords();
  130.                 for(int n=0;n<Origin.length;n++){
  131.                         Reader reader=null;
  132.                         BufferedReader bf=null;
  133.                         String line=null;
  134.                         try {
  135.                                 reader = new FileReader(Origin[n]);
  136.                                 bf=new BufferedReader(reader);
  137.                                 line=bf.readLine();
  138.                                 for(int i=0;i<line.length();i++){
  139.                                         String m=line.substring(i, i+1);
  140.                                         mapword.addNewNodeElement(m, Origin[n].getName(), i);
  141.                                 }       
  142.                         } catch (FileNotFoundException e) {
  143.                                 e.printStackTrace();
  144.                         } catch (IOException e) {
  145.                                 e.printStackTrace();
  146.                         }finally{
  147.                                 try {
  148.                                         bf.close();
  149.                                         reader.close();
  150.                                 } catch (IOException e) {
  151.                                         e.printStackTrace();
  152.                                 }
  153.                         }
  154.                 }
  155.                 ObjectOutputStream oos=null;
  156.                 try {
  157.                         OutputStream out=new FileOutputStream(newFile);
  158.                         oos=new ObjectOutputStream(out);
  159.                         oos.writeObject(mapword);
  160.                 } catch (FileNotFoundException e) {       
  161.                         e.printStackTrace();
  162.                 } catch (IOException e) {
  163.                        
  164.                         e.printStackTrace();
  165.                 }finally{
  166.                         try {
  167.                                 oos.close();
  168.                         } catch (IOException e) {
  169.                                 e.printStackTrace();
  170.                         }
  171.                 }
  172.         }
  173.         public static void FileProcessAll(File[] files){
  174.                 File[] newFile=new File[files.length];
  175.                 String uri=files[0].getAbsolutePath();
  176.                 int m=uri.lastIndexOf(File.separator)+1;
  177.                 String newUrl=uri.substring(0, m);
  178.                 File f=new File(newUrl+"temp");
  179.                 if(f.exists()==false){
  180.                         f.mkdir();
  181.                 }
  182.                 for(int i=0;i<files.length;i++){
  183.                         newFile[i]=new File(f.getAbsoluteFile()+File.separator+files[i].getName());
  184.                 }
  185.                 for(int i=0;i<files.length;i++){
  186.                         FileProcess.processFile(files[i], newFile[i]);
  187.                 }
  188.         }
  189.         public static void getWordCountAll(File[] files,File newFile){
  190.                
  191.                 MapWords mapword=new MapWords();
  192.                
  193.                 for(int j=0;j<files.length;j++){
  194.                         Reader reader=null;
  195.                         BufferedReader bf=null;
  196.                         String line=null;
  197.                         try {
  198.                                 reader = new FileReader(files[j]);
  199.                                 bf=new BufferedReader(reader);
  200.                                 line=bf.readLine();
  201.                                 for(int i=0;i<line.length();i++){
  202.                                         String m=line.substring(i, i+1);
  203.                                         Nodes node=new Nodes(m);
  204.                                         mapword.addNewNodeElement(m, files[j].getName(), i);
  205.                                 }
  206.                         } catch (FileNotFoundException e) {
  207.                                 e.printStackTrace();
  208.                         } catch (IOException e) {
  209.                                 e.printStackTrace();
  210.                         }finally{
  211.                                 try {
  212.                                         bf.close();
  213.                                         reader.close();
  214.                                 } catch (IOException e) {
  215.                                         e.printStackTrace();
  216.                                 }
  217.                         }
  218.                 }
  219.                 Map<String,Integer> map=new HashMap<String,Integer>();
  220.                 for(int i=0;i<mapword.getMapSize()-1;i++){
  221.                         String Keyword=mapword.toGetKeyByIndex(i+1);
  222.                         Nodes node=mapword.getNode(Keyword);
  223.                         map.put(Keyword,node.getWordCountAll());
  224.                 }
  225.                 ObjectOutputStream oos=null;
  226.                 try {
  227.                         OutputStream out=new FileOutputStream(newFile);
  228.                         oos=new ObjectOutputStream(out);
  229.                         oos.writeObject(map);
  230.                 } catch (FileNotFoundException e) {       
  231.                         e.printStackTrace();
  232.                 } catch (IOException e) {
  233.                        
  234.                         e.printStackTrace();
  235.                 }finally{
  236.                         try {
  237.                                 oos.close();
  238.                         } catch (IOException e) {
  239.                                 e.printStackTrace();
  240.                         }
  241.                 }
  242.                 System.out.println(map.toString());
  243.                 System.out.println(map.size());
  244.         }
  245. }
复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Java jav exception tostring outputs import 中文 JAVA java 统计

src.zip
下载链接: https://bbs.pinggu.org/a-1458101.html

7.61 KB

需要: 2 个论坛币  [购买]

没有主方法,自己调用

za rujina!

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-2 08:29