首页 > 其他 > 详细

取前k个出现频率最大的单词

时间:2014-08-16 19:50:51      阅读:422      评论:0      收藏:0      [点我收藏+]



package aprioriproduct;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.print.DocFlavor.BYTE_ARRAY;

public class AprioriProduct {
	private static TreeMap<String,Integer>tm=new TreeMap();
	private static int k=4;
	
        	
	public static void main(String []args)
	{
		FileReader fr;
		try{
			fr=new FileReader("C:\\Examples6.txt");
			BufferedReader br=new BufferedReader(fr);
			String aline;
			while((aline=br.readLine())!=null)
			{
				String str =new String(aline);
				//System.out.println(str);
				String arrayList[]=str.split(",|\\.|:|;| ");
				for(String temp:arrayList)
				{   
					//System.out.println(temp);
					
					if(!tm.containsKey(temp))
					{
						tm.put(temp,1);
					}
					else 
					{
						tm.put(temp,tm.get(temp)+1);
					}
				}
			} 
		
			tm.remove(tm.firstKey());
			
			List arrayList = new ArrayList(tm.entrySet());
	        Collections.sort(arrayList, new Comparator() {
	            public int compare(Object o1, Object o2) {
	                Map.Entry obj1 = (Map.Entry) o1;
	                Map.Entry obj2 = (Map.Entry) o2;
	                return -((Integer) obj1.getValue()).compareTo((Integer) obj2
	                        .getValue());
	            }
	        });
	        for (int i = 0; i < k; i++) 
	        {
	            System.out.println(((Map.Entry) arrayList.get(i)).getKey()
	                    + " " + ((Map.Entry) arrayList.get(i)).getValue());
	        }
			for(Iterator it =tm.keySet().iterator();it.hasNext();)
			{
				
				String key =it.next().toString();
				System.out.print(key+" ");
				System.out.println(tm.get(key));
			}
			
			fr.close();
			br.close();
		}
		
		
		
		catch(Exception e)
		{
			e.printStackTrace();
		}	
	}
}




取前k个出现频率最大的单词,布布扣,bubuko.com

取前k个出现频率最大的单词

原文:http://blog.csdn.net/huaweitman/article/details/38615453

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!