统计英文文章单词JAVA代码

问题:  

设计相应的数据结构和算法,尽量高效的统计一片英文文章(总单词数目)里出现的所有英文单词,按照在文章中首次出现的顺序打印输出该单词和它的出现次数

代码:


 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author liujia
 * @date 2011-9-27
 * @email liujia9234@126.com
 *
 */
public class WordStatistics {
	List<String> words = new ArrayList<String>();
	Map<String,Integer> statistics = new HashMap<String, Integer>();
	
	private void addWord(String word){
		String uword = word.trim().toUpperCase();
		if(statistics.containsKey(uword)){
			statistics.put(uword, statistics.get(uword)+1);
		}else{
			statistics.put(uword, 1);
			words.add(uword);
		}	
	}
	
	public void readFile(File file){
		StringBuilder sb = new StringBuilder();
		try {
			FileReader fr = new FileReader(file);
			BufferedReader reader = new BufferedReader(fr);
			String tempString = null;
            int line = 1;
            // 一次读入一行,直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
                sb.append(tempString);
                line++;
            }
            reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("===========英文文章===============");
		System.out.println(sb.toString());
		String []words = sb.toString().replace('.',' ').replace(',',' ').replace("  ", " ").split(" ");
		for (String word : words) {
			addWord(word);
		}
	}
	
	public void getStatisticsInfo(){
		for (String word : words) {
			System.out.println("单词:"+word.toLowerCase()+"出现了"+statistics.get(word)+"次");
		}
	}
	
	public static void main(String[] args) {
		long beginTime = System.currentTimeMillis();
		WordStatistics ws = new WordStatistics();
		ws.readFile(new File("D:\\words.txt"));
		ws.getStatisticsInfo();
		long endTime = System.currentTimeMillis();
		System.out.println("============总共耗时"+(endTime-beginTime)+"毫秒");
	}
	
}


结果如下:

===========英文文章===============
In the Orient young bulls are tested for the fight arena in a certain manner. Each is brought to the ring and allowed to attack a picador who pricks them with a lance. The bravery of each bull is then rated with care according to the number of times he demonstrates his willingness to charge in spite of the sting of the blade. Henceforth will I recognize that each day I am tested by life in like manner. If I persist, if I continue to try, if I continue to charge forward, I will succeed.
单词:in出现了4次
单词:the出现了7次
单词:orient出现了1次
单词:young出现了1次
单词:bulls出现了1次
单词:are出现了1次
单词:tested出现了2次
单词:for出现了1次
单词:fight出现了1次
单词:arena出现了1次
单词:a出现了3次
单词:certain出现了1次
单词:manner出现了2次
单词:each出现了3次
单词:is出现了2次
单词:brought出现了1次
单词:to出现了6次
单词:ring出现了1次
单词:and出现了1次
单词:allowed出现了1次
单词:attack出现了1次
单词:picador出现了1次
单词:who出现了1次
单词:pricks出现了1次
单词:them出现了1次
单词:with出现了2次
单词:lance出现了1次
单词:bravery出现了1次
单词:of出现了4次
单词:bull出现了1次
单词:then出现了1次
单词:rated出现了1次
单词:care出现了1次
单词:according出现了1次
单词:number出现了1次
单词:times出现了1次
单词:he出现了1次
单词:demonstrates出现了1次
单词:his出现了1次
单词:willingness出现了1次
单词:charge出现了2次
单词:spite出现了1次
单词:sting出现了1次
单词:blade出现了1次
单词:henceforth出现了1次
单词:will出现了2次
单词:i出现了6次
单词:recognize出现了1次
单词:that出现了1次
单词:day出现了1次
单词:am出现了1次
单词:by出现了1次
单词:life出现了1次
单词:like出现了1次
单词:if出现了3次
单词:persist出现了1次
单词:continue出现了2次
单词:try出现了1次
单词:forward出现了1次
单词:succeed出现了1次
============总共耗时15毫秒

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值