一個簡易的詞頻統計

package com.my;
import java.io.*;
import java.util.*;
public class CountOccurrenceOfWords {
	/**
	 * @param args
	 */
 
	public void count(String s)
	{	
		long startTime=System.currentTimeMillis();//記錄初始時間以計算程序運行時間
		try{	
			FileReader fr=new FileReader(s);//讀取指定位置的字符文件
			BufferedReader br=new BufferedReader(fr);//從字符輸入流中讀取文本,緩衝字符
			String string;
			String text="";
			while((string=br.readLine())!=null)
			{
				text+=string;//把文本中的字符連字符串成一個
			}
			TreeMap<String,Integer> map=new TreeMap<String,Integer>();
			String[] words=text.split("[ 0123456789\"+-=*\n\t\r.,;:!?(){}]");//單詞分隔
			for(int i=0;i<words.length;i++)
			{
				String key=words[i].toLowerCase();//都轉成小寫字母
				if(key.length()>0)
				{
					if(map.get(key)==null)
					{
						map.put(key, 1);
					}
					else
					{
						int value=map.get(key).intValue();
						value++;
						map.put(key, value);
					}
				}
			}
			Set<Map.Entry<String, Integer>> entrySet=map.entrySet();
			File f2=new File("E:/bb.txt");//建一個文本放統計的單詞和數量
			FileWriter fw=new FileWriter(f2);
			BufferedWriter bufw=new BufferedWriter(fw);
			for(Map.Entry<String, Integer> entry:entrySet)
			{
				bufw.write(entry.getKey()+"\t"+entry.getValue());
				bufw.newLine();
				bufw.flush();
				System.out.println(entry.getKey()+"\t"+entry.getValue());
			}
			bufw.close();
			fw.close();	
		}catch(FileNotFoundException e2){
			e2.printStackTrace();
		}catch(IOException e1){
			e1.printStackTrace();
		}
		finally{
			
		}
		long endTime=System.currentTimeMillis();
		long time=endTime-startTime;
		System.out.println("運行時間:"+time+"ms");
		
	}

}

下面是用AWT寫的一個簡單界面

package com.my;
import java.awt.*;
import java.awt.event.*;
public class AWTTest {
	Frame f=new Frame("詞頻統計");
	Label l1=new Label("  源文件位置:");
	Label l2=new Label("保存文件位置:");
	Label l3=new Label("保留語言種類:");
	Button b1=new Button("...");
	Button b2=new Button("...");
	Button b3=new Button("開始統計");
	Button b4=new Button("現在退出");
	TextField t1=new TextField();
	TextField t2=new TextField();
	Choice c1=new Choice();
	FileDialog d1=new FileDialog(f,"選擇需要統計的文件",FileDialog.LOAD);
	FileDialog d2=new FileDialog(f,"選擇保存文件的路徑",FileDialog.SAVE);
	String s1="";
	String s2="";
	public void init()
	{
		f.setLayout(null);
		l1.setBounds(40, 40, 100, 20);
		f.add(l1);
		t1.setBounds(140, 40, 300, 20);
		f.add(t1);
		b1.addActionListener(new ActionListener()//打開需要統計的文章
		{
			public void actionPerformed(ActionEvent e)
			{
				d1.setVisible(true);
				t1.setText(d1.getDirectory()+d1.getFile());
				s1=d1.getDirectory()+d1.getFile();
			}
		});
		b1.setBounds(450, 40, 30, 20);
		f.add(b1);
		l2.setBounds(40, 80, 100, 20);
		f.add(l2);
		t2.setBounds(140, 80, 300, 20);
		f.add(t2);
		b2.addActionListener(new ActionListener()//找到要放統計單詞和數量的文本
		{
			public void actionPerformed(ActionEvent e) 
			{
				d2.setVisible(true);
				t2.setText(d2.getDirectory()+d2.getFile());
				s2=d2.getDirectory()+d2.getFile();
			}
		});
		b2.setBounds(450, 80, 30, 20);
		f.add(b2);
		l3.setBounds(40, 120, 100, 20);
		f.add(l3);
		c1.setBounds(140, 120, 100, 20);
		c1.add("ENGLISH");
		c1.add("CHINESE");
		f.add(c1);
		b3.addActionListener(new ActionListener()//統計分析按鈕的驅動
		{
			public void actionPerformed(ActionEvent e)
			{
				try{
					new CountOccurrenceOfWords().count(s1);	
					}catch(Exception e1){
						e1.printStackTrace();
					}
			}
		});
		b3.setBounds(100, 200, 60, 20);
		f.add(b3);
		b4.addActionListener(new ActionListener()//退出按鈕的驅動
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		b4.setBounds(350, 200, 60, 20);
		f.add(b4);
		f.setBounds(30,30,600,400);
		f.setVisible(true);
	}
	
	public static void main(String[] args) {
		AWTTest at=new AWTTest();
		at.init();
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章