词频统计程序

词频统计程序

程序的功能

(1)可导入任意英文文本文件
(2)统计该英文文件中单词数和各单词出现的频率(次数),并能将单词按字典顺序输出。
(3)将单词及频率写入文件。
(4)实现GUI界面。
(5)将单词及频率写入数据库。

功能的实现

GUI界面的实现:

  • 界面组成比较简单
    三个按钮:
    open(用来打开文件资源管理器)
    check(用来统计单词数量,并将英文单词以及频率写入数据库)
    order(用来排序并打开新窗口显示排序后的文本)
    一个标签:
    num(用来显示统计的单词数量)
  • 界面实现代码:
        this.setBounds(700,400,400,300);
		JButton open = new JButton("打开");
		JButton check = new JButton("查询");
		JButton order=new JButton("按字典排序");
		this.setLayout(null);
		open.setBounds(100, 100, 80, 30);
		check.setBounds(200, 100,80, 30);
		order.setBounds(150,150,100,30);
		this.add(open);
		this.add(check);
		this.add(order);
		num=new JLabel("英文文本的单词数量");
	    num.setBounds(120,40,150,40);
		this.add(num);
		/*此处省略的按钮监听器的添加将在后面各个功能介绍中实现中*/
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);

任意英文文本文件的导入:

  • 利用FileDialog类直接打开本地文件资源管理器,通过选择的方式导入任意英文文本文件。并将其保存在File中,通过BufferedReader读入到StringBuilder类的一个对象中。
  • 实现代码如下:
open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				FileDialog jf = new FileDialog(frame, "打开文件", FileDialog.LOAD);
				jf.setVisible(true);
				String dirName = jf.getDirectory();
				String fileName = jf.getFile();
				File f = new File(dirName, fileName);
				textFile = new StringBuilder();
				String b = null;
				BufferedReader br;
				try {
					br = new BufferedReader(new FileReader(f));
					while ((b = br.readLine()) != null) {
						textFile.append(b);
					}
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}

		});

统计该英文文件中单词数

  • 通过字符串的切割使文本变成单词,其中在字符串的切割中遇到了一系列问题,比如文本中的标点符号多样,还有空格,我们需要按照标点符号切割后,在按照空格切换,在这个过程中,有字符串数组到字符串的转换时会自动添加逗号。为了解决这个问题,我使用了字符串的替换。将所有的标点符号用空格替换,然后按照空格切割。
  • 实现代码如下:
public String[] Dell(StringBuilder b){
	 String s=String.valueOf(b);//转换为字符串
	 String s1=s.replace(',', ' ');//将逗号用空格替换
	 String s2=s1.replace('.', ' ');//将句号用空格替换
	 String s3=s2.replace(';',' ');//将分好用空格替换
	 String textArry[]=s3.split(" ");//按照空格切割字符串,得到存储着文本中所有英文单词的字符数组
	 
	 for(int i=0;i<textArry.length;i++){
	      if( textArry[i].length()==0){//将字符串数组中为空的字符串删除
	    	  textArry[i]=textArry[i+1];
	      }
		 }
	    //统计英文单词的数量
	    String a=String.valueOf(textArry.length);
	    num.setText("文本中英文单词数量为:"+a);//设置标签的值,显示单词数量
	    return textArry;
	    
	}

将单词按字典顺序输出:

  • 使用冒泡排序将得到的字符数组进行排序输入即可,输出的显示采用了textArea
  • 代码实现如下:
order.addActionListener(new ActionListener(){
       	@Override
			public void actionPerformed(ActionEvent e) {
       		JFrame f=new JFrame("排序结果");//新窗体的创建
       		f.setBounds(800,400,500,600);
            JTextArea ta=new JTextArea(10,5);//显示多行文本
            ta.setLineWrap(true);
            f.add(ta);
           
				String textArry[]=Dell(textFile);
				for(int i=0;i<textArry.length;i++){ //冒泡排序
					for(int j=0;j<textArry.length-i-1;j++){
						if((textArry[j].compareTo(textArry[j+1]))>0){
							String temp=textArry[j];
							textArry[j]=textArry[j+1];
							textArry[j+1]=temp;
						}
					}
				}
				StringBuilder sb=new StringBuilder();
				for(int i=0;i<textArry.length;i++){	    
				    sb.append(textArry[i]);
				    sb.append(" \n");//每个单词之间添加一个换行
				}
				
				ta.setText(String.valueOf(sb));
			f.setVisible(true);
			}	
		});

将单词及频率写入数据库

  • 创建数据库count,创建一个表cout保存单词以及出现频率,jdbc连接mysql,然后用insert向数据库加入相应数据
  • 数据连接的代码如下:
public class DBUtil {
public static Connection getConnection() throws Exception{
	Connection conn=null;
	try {
		 Class.forName("com.mysql.jdbc.Driver");
		conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/count","root","root");
	} catch (Exception e) {
		e.printStackTrace();
		//向外抛出异常,谁调用此方法就抛给谁
		throw e;
	}
	return conn;
}
/*
 * 3.关闭连接的方法
 */
public static void close(java.sql.Connection conn){
	try {
		if (conn!=null) {
			conn.close();
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}
}

public static void main(String[] args) throws Exception {
	//getConnection()方法是一个static修饰的静态方法,
	//方法的调用可以使用类名点
	Connection conn= DBUtil.getConnection();
	System.out.println(conn);
	DBUtil.close(conn);
}
}
  • 向数据库中中存入单词以及频率的代码如下:
public class CountDao {
	public void save(String words,int counts){
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			//1.加载驱动并建立连接
			conn=DBUtil.getConnection();
			String sql="insert into count values(?,?) ";
			//预编译sql语句
			ps=conn.prepareStatement(sql);
			//PreparedStatement类中的set方法中的第1个参数
			//表示sql语句中第几个问号,第2个参数表示给这个问号赋值
			ps.setString(1, words);
			ps.setInt(2, counts);
			//2.执行sql语句
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			
		}finally{
			//3.关闭连接
			DBUtil.close(conn);
		}
	} 
	
}
  • 单词频率的统计代码如下:
check.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] textArry=Dell(textFile);
				CountDao cd=new CountDao();
			    int tenpCount=0;//单词频率
			    for(int i=0;i<textArry.length;i++){
			    	for(int j=0;j<textArry.length;j++){
			    		if(textArry[i].equals(textArry[j])){
			    			tenpCount++;
			    		}	
			    	}
			    	cd.save( textArry[i],tenpCount);//将单词和出现的频率保存在数据库中   	
			    }
			}

		});

最终运行结果如下

在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值