单词统计

  题目要求:

  英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?哈利波特》 中最常用的短语是什么,等等。我们就写一些程序来解决这个问题,满足一下我们的好奇心。第0步:输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位。字母频率 = 这个字母出现的次数 / (所有A-Z,a-z字母出现的总数)如果两个字母出现的频率一样,那么就按照字典序排列。  如果 S 和 T 出现频率都是 10.21%, 那么, S 要排在T 的前面。第1步:输出单个文件中的前 N 个最常出现的英语单词。作用:一个用于统计文本文件中的英语单词出现频率。单词:以英文字母开头,由英文字母和字母数字符号组成的字符串视为一个单词。单词以分隔符分割且不区分大小写。在输出时,所有单词都用小写字符表示。英文字母:A-Z,a-z字母数字符号:A-Z,a-z,0-9第1步:输出单个文件中的前 N 个最常出现的英语单词。分割符:空格,非字母数字符号 例:good123是一个单词,123good不是一个单词。good,Good和GOOD是同一个单词。

  题目要求文本文件操作,我只能在百度上找关于输入输出流操作文本文件的代码,学着自己从文件中读取,将字符串写入文件。有了这些基础之后,就可以做这个题了。我们可以将文本文件用java代码转为长字符串,然后再用split方法将其分割成字符串数组,每个字符串数组就存储一个单词。之后的统计和计算占比都根据这个字符串数组,设置一个计数count,循环统计就可以了,和之前做过的许多统计次数一样简单。输出前n个最常出现的单词,只需要求count数最多的n个单词,再输出即可。

  代码如下:

  

  1 package piao;
  2 import java.util.*;
  3 import java.io.*;
  4 
  5 class letter
  6 {
  7 private char l;
  8 private int count;
  9 
 10 public char getL() {
 11     return l;
 12 }
 13 public void setL(char l) {
 14     this.l = l;
 15 }
 16 public int getCount() {
 17     return count;
 18 }
 19 public void setCount(int count) {
 20     this.count = count;
 21 }
 22 
 23 public letter() {};
 24 public letter(char l,int count) 
 25 {
 26     this.l=l;
 27     this.count=count;
 28 }
 29 class word
 30 {
 31     private String value;
 32     private int count;
 33     
 34     public String getValue() {
 35         return value;
 36     }
 37     public void setValue(String value) {
 38         this.value = value;
 39     }
 40     public int getCount() {
 41         return count;
 42     }
 43     public void setCount(int count) {
 44         this.count = count;
 45     }
 46     public word() {};
 47     public word(String value,int count)
 48     {
 49         this.value=value;
 50         this.count=count;
 51     }
 52 }
 53 
 54 public class Piaotxt {
 55     
 56 
 57     public void main(String[] args)throws FileNotFoundException, IOException {
 58         // TODO 自动生成的方法存根
 59         File file=new File("F:/java开发/piao.txt");
 60         BufferedReader br=new BufferedReader(new FileReader(file));
 61         FileOutputStream out=new FileOutputStream("F:/java开发/jieguo.txt");
 62         PrintStream p=new PrintStream(out);//文件写入流
 63         
 64         String s;
 65         int i=0;
 66         double b=0;
 67         int j=0;
 68       
 69         double sum=0;
 70         int count=0;
 71         s=br.readLine();
 72         char [] strArr1 = s.toCharArray();//转化为单个字符
 73         String []s1=s.split(" ");//以空格为分割划分每个单词
 74         word []w=new word[s1.length];
 75         
 76         for(i=0;i<strArr1.length;i++)
 77         {
 78             if(strArr1[i]!=' '||strArr1[i]!=','||strArr1[i]!='.')
 79                 sum++;
 80         }//求全部字母数
 81         
 82         for(char ch='a';ch<'z';ch++)
 83         {
 84             count=0;//计数器
 85             for(i=0;i<strArr1.length;i++)
 86             {
 87                 if(ch==strArr1[i]||strArr1[i]==ch-32)
 88                     count++;
 89             }
 90             if(count!=0)
 91                 b=count/sum;
 92                 System.out.print("字符中"+ch+"有"+count+"个");
 93                 System.out.println("占比:"+String.format("%.2f", b));
 94         
 95         }  
 96         
 97         for(i=0;i<s1.length;i++)
 98         {
 99             count=0;
100             for(j=0;j<s1.length;j++)
101             {
102                 if(s1[i].equals(s1[j]))
103                 {
104                     count++;
105                 }
106                 
107             }
108 
109                  w[i]=new word(s1[i],count);
110         }
111         
112         word temp=new word();
113         for(i=0;i<s1.length;i++)
114         {
115             for(j=0;j<s1.length-1;j++)
116             {
117                 if(w[j].getCount()<w[j+1].getCount())
118                 {
119                     temp=w[j];
120                     w[j]=w[j+1];
121                         w[j+1]=temp;    
122                 }
123             }
124             
125         }
126         
127         for(i=0;i<w.length;i++)
128         {
129             System.out.println(w[i].getValue()+":"+w[i].getCount());
130         }
131     }
132 }
133 }
View Code
原文地址:https://www.cnblogs.com/Aduorisk/p/11033099.html