统计文档中单词出现频率

一.先贴出自己的代码

 1 import java.io.BufferedReader;
 2 import java.io.File;
 3 import java.io.FileReader;
 4 import java.io.IOException;
 5 import java.util.Arrays;
 6 import java.util.HashMap;
 7 import java.util.Iterator;
 8 import java.util.Map;
 9 import java.util.Map.Entry;
10 public class Search {
11     public void ReadBook()throws Exception
12     {
13         String book=book("D:/book.txt");
14         System.out.println(book);
15     }
16
17     private static String book(String file) throws IOException
18     {
19         // TODO Auto-generated method stub
20         File f=new File(file);
21         BufferedReader bf=new BufferedReader(new FileReader(f));
22         String content="";
23         int lenth=0;
24         int x=0;
25         StringBuilder sb=new StringBuilder();
26         String[] sum= {""};
27         while(content!=null)
28         {
29
30                 content= bf.readLine();
31             if(content==null)
32                 break;
33
34             content=content.replace(",","");
35             content=content.replace(".","");
36             content=content.replace(";","");
37             content=content.replace("(","");
38             content=content.replace(")","");
39             String[] temp = content.split(" ");
40
41             int length1=temp.length;
42
43             int length2=sum.length;
44             sum = Arrays.copyOf(sum, length2 + length1);// 扩容
45 //            System.out.println("???");
46             x=0;
47             for(lenth=length2;lenth<(length1+length2);lenth++)
48             {
49
50                 sum[lenth]=temp[x];
51                 x++;
52             }
53 //            for(int xx=0;xx<sum.length;xx++)
54 //            {
55 //                System.out.println(sum[xx]);
56 //            }
57            Map<String,Integer> map=new HashMap<String,Integer>();
58            for(String str : sum){
59                if(map.containsKey(str)){
60                    map.put(str, map.get(str) + 1);
61                }else{
62                    map.put(str, 1);
63                }
64            }
65            Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
66             while(it.hasNext()){
67                 Entry ex = (Entry) it.next();
68                 System.out.println(ex.getKey() + ":" + ex.getValue());
69
70         }
71
72
73
74
75     }
76         bf.close();
77         return sb.toString();
78     }
79 }
 1 public class main {
 2
 3     public static void main(String[] args) throws Exception {
 4         // TODO Auto-generated method stub
 5         Search s=new Search();
 6         s.ReadBook();
 7
 8
 9
10     }
11
12 }

二.思路

1.当时看到实验题目第一反应就是要读文件,然后也没有多想,就去先搞文件

2.文件搞完了,就去想想要去吧符号去掉,然后在把所有的单词整合到一起

3.最后就是用方法进行统计

原文地址:https://www.cnblogs.com/heiyang/p/9775774.html

时间: 2024-07-31 02:25:10

统计文档中单词出现频率的相关文章

C语言K&R习题系列——统计文档中每个单词所占字母个数,以直方图形式输出

原题: Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging. 这也是我第一个过百行的代码(带注释,空格什么的) 主要分两个部分:输入和输出 #include < stdio.h > #define

python统计文档中词频

python统计文档中词频的小程序 python版本2.7 程序如下,测试文件与完整程序在我的github中 1 #统计空格数与单词数 本函数只返回了空格数 需要的可以自己返回多个值 2 def count_space(path): 3 number_counts = 0 4 space_counts = 0 5 number_list = [] 6 7 with open(path, 'r') as f: 8 for line in f: 9 line = line.strip() 10 sp

指定文件目录遍历所有子目录统计文档的单词出现数量

package javaClassHomework; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; import java.util.Comparator; import java.util.

Python - 统计一篇文章中单词的频率

def frenquence_statistic(file_name): frequence = {} for line in open(file_name,'r').readlines(): words =line.strip().split(" ") for word in words: word = ''.join(list(filter(str.isalpha,word))).lower() if frequence.get(word) == None : frequence[

散列表:WORD文档中如何检测单词的拼写错误

散列表:WORD文档中如何检测单词的拼写错误 散列表用的是数组支持按照下标随机访问数据的特性,所以散列其实是数组的一种扩展,由数组演化而来. 散列表(哈希表 Hash Table):由散列函数(哈希函数)和数组构成,底层存储数据的是数组. 散列函数(键转化为散列值即数组下标)的设计: 散列值是非负整数: if key1 = key2, then hash(key1) == hash(key2); if key1 != key2, then hash(key1) != hash(key2). 散列

统计txt文档中的单词个数

public class Bean { private char name; private double pinlv; public Bean(char name,double pl) { this.name=name; this.pinlv=pl; } public char getName() { return name; } public void setName(char name) { this.name = name; } public double getPinlv() { re

Aspose.Words使用教程之在文档中找到并替换文本

Aspose.Words使用教程之在文档中找到并替换文本 Aspose.Words是一款先进的文档处理控件,在不使用Microsoft Words的情况下,它可以使用户在各个应用程序中执行各种文档处理任务,其中包括文档的生成.修改.渲染.打印,文档格式转换和邮件合并等文档处理.此外,Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式. 使用范围:在前的范围内替换查找或替换特定的字符串,因为它会返回替换的数量,所以它

怎样翻译word文档中的英文,仅需三分钟即可搞定

怎样翻译word文档中的英文?在职场办公当中,难免会遇到外国客户.在与外国客户沟通.合作的过程当中,所使用得合作文件.资料的内容几乎都是英文.这也就使得英文不好的职员,除了准备合作资料外还需要花费大量时间去查阅单词,翻译语句,大大降低了工作效率.今天小编就将告诉大家如何快速有效地翻译word文档中的英文. 使用工具:迅捷pdf转换https://www.xunjiepdf.com/converter 1.大部分翻译工具,都只能单个单词或者逐句翻译,就算能翻译整段也是有次数限制.这样就会导致翻译出

Word文档中如何快速切换英文大小写?掌握这个方法轻松完成

最近很多小伙伴问我Word文档中如何快速切换英文大小写?遇到这样的问题相信很多人都是直接将单词一个个的进行修改,这样操作简直是太浪费时间了,看着大家这样操作,我在一旁看着实在是太着急了,于是总结了这方面的技巧来帮助有需要的朋友,一起看看吧! 一.快速切换大小写 常常为了统一英文格式需要切换成英文统一格式,其实很简单,我们可以直接通过快捷键Shift+F3进行操作,也可以点击开始菜单栏,在子菜单栏中找到更改大小写按钮,在弹出的栏目中点击选择更改大小写-切换大小写,之后点击确定即可: 这样操作起来是