用户需求:英语的26 个字母的频率在一本小说中是如何分布的?某类型文章中常出现的单词是什么?某作家最常用的词汇是什么?《哈利波特》 中最常用的短语是什么,等等。 要求:输出单个文件中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。
这个程序代码如下:
package tongji;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class zimu {
public static void main(String[] args) throws Exception
{
File file=new File("D:\eclipse\tongji\src\English.txt");
@SuppressWarnings("resource")
FileReader reader=new FileReader(file);
int len=(int)file.length();
char[] chars=new char[len];
reader.read(chars);
String txt=String.valueOf(chars);
String[] a=txt.split("[^a-zA-Z]+"); //利用正则表达式将字符串划分为单词字符串数组
int n=a.length;
int nind=0,wind=0;
Object[][] b=new Object[n][2];
for(;wind<n;wind++){
int k=0; //用k标记单词是否已经出现,单词已经出现,在已存数组中匹配,若匹配则相应单词数目加1
for(int i=0;i<nind;i++){
if(((String) b[i][0]).equalsIgnoreCase(a[wind])){
b[i][1]=(int)b[i][1]+1;
k=1;
break;
}
}
//单词未出现则在记录数组末尾记录此单词
if(k==0){
b[nind][0]=a[wind];
b[nind][1]=1;
nind++;
}
}
@SuppressWarnings("resource")
BufferedReader br1=new BufferedReader(new FileReader("D:\eclipse\tongji\src\English.txt"));
String str=br1.readLine();
String[] arg=str.split("[^a-zA-Z]+"); //利用正则表达式将字符串划分为单词字符串数组
int m=arg.length;
//判断文章中词语是否为虚词,如果为虚词则在比较频率时不计较在内
for(int i=0;i<nind;i++){
for(int j=0;j<m;j++ ){
if(((String) b[i][0]).equalsIgnoreCase(arg[j])){
b[i][1]=0;
}
}
}
for(int i=0;i<10;i++){
int max=0;
for(int j=0;j<nind;j++){
if((int)b[j][1]>(int)b[max][1]){
max=j;
}
}
System.out.println(b[max][0]+"出现次数为:"+b[max][1]);
b[max][1]=0;
}
}
}
代码截图:
txt文件内容:
From the distance, it looked like a skinny tube,but as we got closer, we could see it flesh out before our eyes.
It was tubular, all right, but fatter than wecould see from far away.Furthermore, we were also astonished to notice that the building was really in two parts: a pagoda sitting on top of a tubular one-story structure. Standing ten feet away, we could marvel at how much of the pagoda was made up of glass windows. Almost everything under the wonderful Chinese roof was made of glass, unlike the tube that it was sitting on, which only had four. Inside, the tube was gloomy, because of the lack of light. Then a steep, narrow staircase took us up inside the pagoda and the light changed dramatically. All those windows let in a flood of sunshine and we could see out for miles across the flat land.