一种快速统计文档的中文字符的JAVA代码
发布:zhukovasky | 分类:统计库
关于本站
人大经济论坛-经管之家:分享大学、考研、论文、会计、留学、数据、经济学、金融学、管理学、统计学、博弈论、统计年鉴、行业分析包括等相关资源。
经管之家是国内活跃的在线教育咨询平台!
经管之家新媒体交易平台
提供"微信号、微博、抖音、快手、头条、小红书、百家号、企鹅号、UC号、一点资讯"等虚拟账号交易,真正实现买卖双方的共赢。【请点击这里访问】
哲学
- 哲学名言 | 【独家发布】经典 ...
- 哲学书籍 | 求推荐一本讲人生 ...
- 哲学书籍 | 20部必读的哲学书 ...
- 哲学书籍 | 经济人,开拓你逻 ...
- 哲学书籍 | 哲学书籍
- 哲学书籍 | 哲学书籍
- 哲学书籍 | 哲学书籍
- 哲学书籍 | 经典的哲学书籍
论文
- 毕业论文 | 写毕业论文
- 毕业论文 | 为毕业论文找思路
- 毕业论文 | 可以有时间好好写 ...
- 毕业论文 | 毕业论文如何选较 ...
- 毕业论文 | 毕业论文选题通过 ...
- 毕业论文 | 还有三人的毕业论 ...
- 毕业论文 | 毕业论文答辩过程 ...
- 毕业论文 | 本科毕业论文,wi ...
TOP热门关键词
现在有个需求,我要统计一个文档集合中每一个中文字符出现的个数,规模在1000份,每一份文档的大小在20KB左右,我用java写了一段代码。个人感触:程序=数据结构+算法。好的数据结构和算法对于程序的性能是不言而喻的 ...
扫码加入统计交流群 |
个人感触:程序=数据结构+算法。好的数据结构和算法对于程序的性能是不言而喻的。
其余类见附件
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.io.OutputStream;
- import java.io.Reader;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * 说明:当实现自定义容器时,千万不要忘记在进行读写操作时,使用serializable接口
- * */
- public class WordCount {
- public static void writeMap(File Origin,File newFile){
- Map<String,Integer> map=getWordCount(Origin);
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(map);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println(map.toString());
- }
- public static Map<String,Integer> getWordCount(File f){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(f);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- MapWords mapword=new MapWords();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- Nodes node=new Nodes(m);
- mapword.addNewNodeElement(m, f.getName(), i);
- }
- Map<String,Integer> map=new HashMap<String,Integer>();
- for(int i=0;i<mapword.getMapSize()-1;i++){
- String Keyword=mapword.toGetKeyByIndex(i+1);
- Nodes node=mapword.getNode(Keyword);
- map.put(Keyword,node.getWordCountAll());
- }
- return map;
- }
- public static void writerMapIndex(File Origin,File newFile){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(Origin);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- MapWords mapword=new MapWords();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- // Nodes node=new Nodes(m);
- // List<String> list=new ArrayList<String>();
- // list=node.getDocNameAll();
- // seriMapArray[i]=new seriMap(m,list);
- mapword.addNewNodeElement(m, Origin.getName(), i);
- }
- // Set<Nodes> setArray=mapword.toGetValueSet();
- // Nodes nodeArray[]=new Nodes[setArray.size()];
- // Nodes node[]=setArray.toArray(nodeArray);
- // System.out.println("node[].length="+node.length);
- // System.out.println("Arrays.toString(node)="+Arrays.toString(node));
- // for(int i=1;i<node.length;i++){
- // System.out.println("i="+i);
- // seriMapArray[i-1]=new seriMap(node[i].getKeyword(),node[i].getDocNameAll());
- // }
- //
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(mapword);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void writerMapIndex(File[] Origin,File newFile){
- MapWords mapword=new MapWords();
- for(int n=0;n<Origin.length;n++){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(Origin[n]);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- mapword.addNewNodeElement(m, Origin[n].getName(), i);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(mapword);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void FileProcessAll(File[] files){
- File[] newFile=new File[files.length];
- String uri=files[0].getAbsolutePath();
- int m=uri.lastIndexOf(File.separator)+1;
- String newUrl=uri.substring(0, m);
- File f=new File(newUrl+"temp");
- if(f.exists()==false){
- f.mkdir();
- }
- for(int i=0;i<files.length;i++){
- newFile[i]=new File(f.getAbsoluteFile()+File.separator+files[i].getName());
- }
- for(int i=0;i<files.length;i++){
- FileProcess.processFile(files[i], newFile[i]);
- }
- }
- public static void getWordCountAll(File[] files,File newFile){
-
- MapWords mapword=new MapWords();
-
- for(int j=0;j<files.length;j++){
- Reader reader=null;
- BufferedReader bf=null;
- String line=null;
- try {
- reader = new FileReader(files[j]);
- bf=new BufferedReader(reader);
- line=bf.readLine();
- for(int i=0;i<line.length();i++){
- String m=line.substring(i, i+1);
- Nodes node=new Nodes(m);
- mapword.addNewNodeElement(m, files[j].getName(), i);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bf.close();
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- Map<String,Integer> map=new HashMap<String,Integer>();
- for(int i=0;i<mapword.getMapSize()-1;i++){
- String Keyword=mapword.toGetKeyByIndex(i+1);
- Nodes node=mapword.getNode(Keyword);
- map.put(Keyword,node.getWordCountAll());
- }
- ObjectOutputStream oos=null;
- try {
- OutputStream out=new FileOutputStream(newFile);
- oos=new ObjectOutputStream(out);
- oos.writeObject(map);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- oos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- System.out.println(map.toString());
- System.out.println(map.size());
- }
- }
「经管之家」APP:经管人学习、答疑、交友,就上经管之家!
免流量费下载资料----在经管之家app可以下载论坛上的所有资源,并且不额外收取下载高峰期的论坛币。
涵盖所有经管领域的优秀内容----覆盖经济、管理、金融投资、计量统计、数据分析、国贸、财会等专业的学习宝库,各类资料应有尽有。
来自五湖四海的经管达人----已经有上千万的经管人来到这里,你可以找到任何学科方向、有共同话题的朋友。
经管之家(原人大经济论坛),跨越高校的围墙,带你走进经管知识的新世界。
扫描下方二维码下载并注册APP
免流量费下载资料----在经管之家app可以下载论坛上的所有资源,并且不额外收取下载高峰期的论坛币。
涵盖所有经管领域的优秀内容----覆盖经济、管理、金融投资、计量统计、数据分析、国贸、财会等专业的学习宝库,各类资料应有尽有。
来自五湖四海的经管达人----已经有上千万的经管人来到这里,你可以找到任何学科方向、有共同话题的朋友。
经管之家(原人大经济论坛),跨越高校的围墙,带你走进经管知识的新世界。
扫描下方二维码下载并注册APP
您可能感兴趣的文章
本站推荐的文章
人气文章
1.凡人大经济论坛-经管之家转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。