java统计单词出现的次数并排序_JAVA统计一篇文章中所有单词出现的次数,并按字典序将单词及频数输出到文件中...

该博客介绍了一个JAVA程序,用于统计文本中所有单词出现的次数,并按字典顺序排序。程序首先读取文件,识别单词,然后存储并去除重复的单词。最终,它按单词出现的频率进行排序并显示结果。

展开全部

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

import java.io.File;

import java.util.Vector;

class WordStatistic

{ Vector allWorsd,noSameWord;

WordStatistic()

{ allWorsd=new Vector();

noSameWord=new Vector();

}

public void wordStatistic(File file)

{ try{ RandomAccessFile inOne=new RandomAccessFile(file,"rw");//创建指向文件file的inOne 的对象

RandomAccessFile inTwo=new RandomAccessFile(file,"rw"); //创建指向文件file的inTwo 的对象

long wordStarPostion=0,wordEndPostion=0;

long length=inOne.length();

int flag=1;

int c=-1;

for(int k=0;k<=length;k++)

{ c=inOne.read(); // inOne调用read()方法

boolean boo=(c<='Z'&&c>='A')||(c<='z'&&c>='a');

if(boo)

{ if(flag==1)

{ wordStarPostion=inOne.getFilePointer()-1;

flag=0;

}

}

else

{ if(flag==0)

{

if(c==-1)

wordEndPostion=inOne.getFilePointer();

else

wordEndPostion=inOne.getFilePointer()-1;

inTwo.seek(wordStarPostion);// inTwo调用seek方法将e68a84e8a2ad3231313335323631343130323136353331333330333633读写位置移动到wordStarPostion

byte cc[]=new byte[(int)wordEndPostion-(int)wordStarPostion];

inTwo.readFully(cc); // inTwo调用readFully(byte a)方法,向a传递cc

String word=new String(cc);

allWorsd.add(word);

if(!(noSameWord.contains(word)))

noSameWord.add(word);

}

flag=1;

}

}

inOne.close();

inTwo.close();

}

catch(Exception e){}

}

public Vector getAllWorsd()

{ return allWorsd;

}

public Vector getNoSameWord()

{ return noSameWord;

}

}

class StatisticFrame extends Frame implements ActionListener

{ WordStatistic statistic;

TextArea showMessage;

Button openFile;

FileDialog openFileDialog;

Vector allWord,noSameWord;

public StatisticFrame()

{ statistic=new WordStatistic();

showMessage=new TextArea();

openFile=new Button("Open File");

openFile.addActionListener(this);

add(openFile,BorderLayout.NORTH);

add(showMessage,BorderLayout.CENTER);

openFileDialog=new FileDialog(this,"打开文件话框",FileDialog.LOAD);

allWord=new Vector();

noSameWord=new Vector();

setSize(350,300);

setVisible(true);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{ System.exit(0);

}

});

validate();

}

public void actionPerformed(ActionEvent e)

{ noSameWord.clear();

allWord.clear();

showMessage.setText(null);

openFileDialog.setVisible(true);

String fileName=openFileDialog.getFile();

if(fileName!=null)

{ statistic.wordStatistic(new File(fileName));

allWord=statistic.getAllWorsd();

noSameWord=statistic.getNoSameWord();

showMessage.append("\n"+fileName+"中有"+allWord.size()+"个英文单词");

showMessage.append("\n其中有"+noSameWord.size()+"个互不相同英文单词");

showMessage.append("\n按使用频率排列:\n");

int count[]=new int[noSameWord.size()];

for(int i=0;i

{ String s1=(String)noSameWord.elementAt(i);

for(int j=0;j

{ String s2=(String)allWord.elementAt(j);

if(s1.equals(s2))

count[i]++;

}

}

for(int m=0;m

{ for(int n=m+1;n

{ if(count[n]>count[m])

{ String temp=(String)noSameWord.elementAt(m);

noSameWord.setElementAt((String)noSameWord.elementAt(n),m);

noSameWord.setElementAt(temp,n);

int t=count[m];

count[m]=count[n];

count[n]=t;

}

}

}

for(int m=0;m

{ showMessage.append("\n"+(String)noSameWord.elementAt(m)+

":"+count[m]+"/"+allWord.size()+

"="+(1.0*count[m])/allWord.size());

}

}

}

}

public class sy6_2

{ public static void main(String args[])

{ new StatisticFrame();

}

}

本回答被提问者采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值