单词统计小助手

#用Java实现的单词统计小助手

**

已完成要求

1:可导入任意的英文文本文件。
2:统计文件中单词和各自的数目,并且按字典顺序进行排序并且输出
3:简单实现了Gui页面
4:将单词及频率写入到数据库当中

代码说明

在这里插入图片描述
(1):Counts类:主要利用Treemap进行计算统计单词以及相关的数目。

(2):Inputfile类:文件输入类,利用流逐行读取文件,并且,将文件中的大写转换成小写,去除英文文本中的符号,按空分割成字符串存储在字符数组中

(3):page类:页面类,主要用来实现简单的页面,并且将结果输出到页面上来

(4):utilsql类:sql工具类:主要用来连接数据库,并1且将结果写入到数据库中

##运行截图
在这里插入图片描述

在这里插入图片描述

源代码

Counts类:主要利用Treemap进行计算统计单词以及相关的数目

public class Counts {
	Map wordsmap=new TreeMap();//新建map对象
	public Map Count(String word){//定义的Count方法,用来数英文文档中单词的个数
		if(wordsmap.containsKey(word)){//判断,集合中是否存在这个键,(单词)
			int data=(int)(wordsmap.get(word))+1;//如果存在,则对应的值,也就是单词数,加一
			wordsmap.put(word, data);	
		}else{
			wordsmap.put(word, 1);//如果不存在,则直接存入容器
		}
		return wordsmap;//将排好序的键值对返回
	}
}

Inputfile类:文件输入类

public class InputFile {
	public Map inFile(String Inf) throws IOException {//  为了方便在页面输出,返回集合
		Map wordsmap=new TreeMap();//定义一个treemap类的集合框架
		try {
			BufferedReader inf=new BufferedReader(new FileReader(new File(Inf)));//定义文件输入流
			String str=null;//定义字符串,临时存储每一行的英文单词
			Counts co=new Counts();//实例化一个count类,方便调用Count方法
			while((str=inf.readLine())!=null){//,如果不为空逐行读取
				String str1 = str.replaceAll("\\pP"," ");//利用正则表达式,去除一些英文文档中的符号
				String str2=str1.toLowerCase();//将英文文档中的大写全部转换成小写,方便进行排序,按字典顺序输出
				String[] words=str2.split(" ");//将读取的每行文档,按空格划分,存入字符串数组中。
				for(int i=0;i<words.length;i++)
					wordsmap=co.Count(words[i].trim());//调用Count方法,计算每个单词的数量,按字典进行排序,返回一个treemap
			}
					
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("您输入的地址不正确");
		}
		return wordsmap;//将排好序的键值对返回
	}
}

page类:页面类

public class page {
	public static void main(String[] args) {
		JFrame frm = new JFrame();//定义一个窗口
		JTextField inputpanel;//  声明一个文本框
		frm.setBounds(100, 100, 400, 600);  //设置窗口的位置以及大小
		frm.setTitle("单词统计小助手");//设置窗口的名称
		inputpanel=new JTextField();//定义一个文本框
		Container c = frm.getContentPane(); // frm中包含一个内容窗格, 需要获取内容窗格,再设置背景颜色,直接设置frm的背景颜色会被内容窗格挡住
		c.setBackground(Color.green);//设置背景颜色
		frm.setLayout(null);         // 如过不设置为null默认,按钮会充满整个内容框,挡住背景颜色
		JButton btn = new JButton("确定");//定义确定按钮
		JOptionPane  jo=new JOptionPane("请输入地址");//提示框
		JTextArea textA = new JTextArea();//定义文本域
		JScrollPane jsp = new JScrollPane(textA);//定义滚动条
		jsp.setSize(300, 400);//设置滚动条的大小
		jsp.setLocation(40, 140);//设置文本域的大小
		btn.setLocation(150, 100);//设置确定按钮的坐标
		btn.setSize(80, 30);//设置确定按钮的大小
		jo.setLocation(20, 40);//提示框的位置
		jo.setSize(100, 30);//提示框的大小
		inputpanel.setLocation(150,40);//设置文本框位置
		inputpanel.setSize(200, 30);//设置文本框大小
		frm.add(inputpanel);//  将文本框添加到窗口中
		frm.add(btn);   // 将确定按钮添加到窗口中
		frm.add(jo);//将提示框添加到窗口中
		frm.add(jsp);//将带有文本域的滚动条添加到窗口中
		frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置退出
		frm.setVisible(true); //设置窗口可见
		btn.addMouseListener(new MouseAdapter() {//为确定按钮添加监听器,采用内部类的形式
			public void mousePressed(MouseEvent e) {
				String addres=inputpanel.getText().toString();	//获取文本框输入的地址
				InputFile inputfile=new InputFile();//实例化 文件输入类
				Map wordsmap=new TreeMap();//定义一个集合容易,接收返回的单词和数量
				utilsql insertsql=new utilsql();//新建sql工具类,用来连接数据库
				try {
					wordsmap=inputfile.inFile(addres);//调用自定义的文件输入方法
					textA.append("  "+"单词"+"       "+"数量"+"\n");
					wordsmap.remove("");//输出前,再次去空,去掉一些键为空的键值对
						for(Object key:wordsmap.keySet()){//利用增强循环遍历容器
							String sql1="INSERT INTO words VALUES('"+key+"',"+Integer.valueOf(wordsmap.get(key).toString())+")";
							insertsql.connect(sql1);//调用连接数的方法,将数据输入到数据库中
							textA.append(" "+key+"      "+wordsmap.get(key)+"\n");//将单词和对应的数量添加到文本域中
							System.out.println(" "+key+"      "+wordsmap.get(key));
						}
					
				} catch (Exception e1) {
					// TODO Auto-generated catch block
//					System.out.println("您输入的地址不正确");
					e1.printStackTrace();
				}
			}
			
		});
	}
}

utilsql类:sql工具类

public class utilsql {
		// 声明Connection对象
		Connection con;
		// 驱动程序名
		String driver = "countwords.mysql.jdbc.Driver";
		// URL指向要访问的数据库名mydata
		String url = "jdbc:mysql://localhost:3306/words?useUnicod=true&characterEncoding=utf8";
		// MySQL配置时的用户名
		String user = "root";
		// MySQL配置时的密码
		String paw = "root";
		
	public void connect(String sql1) throws SQLException{
		 con = DriverManager.getConnection(url, user, paw);
			// 2.创建statement类对象,用来执行SQL语句!!
			java.sql.Statement statement =  con.createStatement();
			// 要执行的SQL语句
			String sql =sql1;
			// 3.ResultSet类,用来存放获取的结果集!!
			statement.execute(sql);

	}
		  
}

猜你喜欢

转载自blog.csdn.net/SZ996795/article/details/82817579