dao文件:
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import bean.keyword;
import bean.paper;
public class dao {
public Connection getConnection()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:mysql://localhost:3307/paper?serverTimezone=GMT%2B8";
String username = "root";
String password="123456";
Connection a=null;
try {
a = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return a;
}
public List<paper>getallpaper() throws NumberFormatException, SQLException//获取所有论文数据
{
List<paper> list=new ArrayList<paper>();
Connection coon=getConnection();
String sql="select* from pypaper";
PreparedStatement pa=coon.prepareStatement(sql);
ResultSet rs=pa.executeQuery(sql);
while(rs.next())
{
paper p;
try {
p = new paper(Integer.parseInt(rs.getString("id")),rs.getString("name"),rs.getString("herf"),rs.getString("writer"),rs.getString("Abstract"),rs.getString("time"),rs.getString("keywords"));
list.add(p);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
rs.close();
pa.cancel();
coon.close();
return list;
}
public List<paper>findpaper(String id,String name,String writer,String time,String keyword) throws SQLException//模糊查询论文
{
List<paper> list=new ArrayList<paper>();
Connection coon=getConnection();
String sql="select * from pypaper";
//拼接模糊查询sql语句
if(id.length()>0) sql=sql+" where id = '"+id+"'";
if(name.length()>0&&sql.length()>21) sql=sql+" and name like '%"+name+"%'";
if(name.length()>0&&sql.length()==21) sql=sql+" where name like '%"+name+"%'";
if(writer.length()>0&&sql.length()>21) sql=sql+" and writer like '%"+writer+"%'";
if(writer.length()>0&&sql.length()==21) sql=sql+" where writer like '%"+writer+"%'";
if(time.length()>0&&sql.length()>21) sql=sql+" and time like '%"+time+"%'";
if(time.length()>0&&sql.length()==21) sql=sql+" where time like '%"+time+"%'";
if(keyword.length()>0&&sql.length()>21) sql=sql+" and keywords like '%"+keyword+"%'";
if(keyword.length()>0&&sql.length()==21) sql=sql+" where keywords like '%"+keyword+"%'";
//System.out.println(sql);
PreparedStatement pa=coon.prepareStatement(sql);
ResultSet rs=pa.executeQuery(sql);
while(rs.next())
{
paper p;
try {
p = new paper(Integer.parseInt(rs.getString("id")),rs.getString("name"),rs.getString("herf"),rs.getString("writer"),rs.getString("Abstract"),rs.getString("time"),rs.getString("keywords"));
list.add(p);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
rs.close();
pa.cancel();
coon.close();
return list;
}
public List<keyword>getkey(String id,String name,String writer,String time,String keyword) throws SQLException//获取关键词
{
List<keyword> list=new ArrayList<keyword>();
Connection coon=getConnection();
String sql="select* from pypaper";
if(id.length()>0) sql=sql+" where id = '"+id+"'";
if(name.length()>0&&sql.length()>21) sql=sql+" and name like '%"+name+"%'";
if(name.length()>0&&sql.length()==21) sql=sql+" where name like '%"+name+"%'";
if(writer.length()>0&&sql.length()>21) sql=sql+" and writer like '%"+writer+"%'";
if(writer.length()>0&&sql.length()==21) sql=sql+" where writer like '%"+writer+"%'";
if(time.length()>0&&sql.length()>21) sql=sql+" and time like '%"+time+"%'";
if(time.length()>0&&sql.length()==21) sql=sql+" where time like '%"+time+"%'";
if(keyword.length()>0&&sql.length()>21) sql=sql+" and keywords like '%"+keyword+"%'";
if(keyword.length()>0&&sql.length()==21) sql=sql+" where keywords like '%"+keyword+"%'";
PreparedStatement pa=coon.prepareStatement(sql);
ResultSet rs=pa.executeQuery(sql);
String TEMP="";
while(rs.next())
{
TEMP+=rs.getString("keywords").replace('+', ' ');
}
int P=30;
TEMP=TEMP.replace('+', ' ');//将关键词字符串的+替换为空格
list=countToEng(TEMP);//对热词排序并生成keyword的list对象
return list;
}
// 统计英文单词
public List<keyword> countToEng(String str) {//生成keyword的list对象
List<keyword> arraykey=new ArrayList<keyword>();
// 定义返回数组
String[] re_str = new String[30];
// 定义ArrayList对象存储匹配到的单词
ArrayList<String> array = new ArrayList<>();
// 使用正则获取单词
Pattern pattern = Pattern.compile("\\b[\\w+\\-']+\\b");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
array.add(matcher.group());
}
// 定义Map集合存储单词,键为单词不重复,值为统计的数量
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
// 遍历字符数组,获取到每一个字符
for (String tstr : array) {
// 用每一个字符作为键,在TreeMap中查找
Integer val = map.get(tstr);
if (val == null) {
// 返回null,则不存在,存储1
map.put(tstr, 1);
//System.out.println("不存在:"+tstr);
} else {
// 返回非null,则把值加1,重新存储
val++;
map.put(tstr, val);
}
}
// // 获取键值对的Set集合
Set<Map.Entry<String, Integer>> sme = map.entrySet();
// 遍历拼接
for (Map.Entry<String, Integer> me : sme) {
keyword k=new keyword();
k.setName(me.getKey());
k.setValue(me.getValue());
arraykey.add(k);
}
arraykey=maopao(arraykey);//排序
return arraykey;
}
public List<keyword> maopao(List<keyword> arr)//冒泡排序
{
for(int i=0;i<arr.size()-1;i++)
{
int a=i;
keyword max=arr.get(i);
for(int j=i+1;j<arr.size();j++)
{
if(max.getValue()<arr.get(j).getValue())
{
a=j;
max=arr.get(j);
}
}
arr.set(a, arr.get(i));
arr.set(i, max);
if(i==arr.size()-10) break;
}
return arr;
}
}
package dao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String p="data quantization quantized BN statistics level DSG scheme model real training methods synthetic homogenization distribution significant even W4A4 calibrated Quantization \r\n"
+ "VOS based attention over video object segmentation scalability end method SST spatiotemporal Our paper introduce Transformer approach address compounding error \r\n";
// p=p.replace('+', ' ');
// String[] a=countToEng(p, 2);
// for(String i:a) {
// System.out.println(i);
// }
str(p);
}
public static void str(String info)
{
String[] a=info.split(" ");
Map<String, Integer> map = new HashMap<String,Integer>();
for(String t : a) {
if(map.containsKey(t))
map.put(t, map.get(t)+1);
else
map.put(t, 0);
}
System.out.println(map.get(0));
}
// 统计英文单词
public static String[] countToEng(String str, int index) {
// 定义返回数组
String[] re_str = new String[20];
// 定义ArrayList对象存储匹配到的单词
ArrayList<String> array = new ArrayList<>();
// 使用正则获取单词
Pattern pattern = Pattern.compile("\\b[\\w+\\-']+\\b");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
array.add(matcher.group());
}
// 定义Map集合存储单词,键为单词不重复,值为统计的数量
TreeMap<String, Integer> map = new TreeMap<String, Integer>();
// 遍历字符数组,获取到每一个字符
for (String tstr : array) {
// 用每一个字符作为键,在TreeMap中查找
Integer val = map.get(tstr);
if (val == null) {
// 返回null,则不存在,存储1
map.put(tstr, 1);
} else {
// 返回非null,则把值加1,重新存储
val++;
map.put(tstr, val);
}
}
// key value拼接后存在TreeSet中会自动排序,将value与key拼接key在前边
TreeSet<String> sortSet = new TreeSet<>();
// 获取键值对的Set集合
Set<Map.Entry<String, Integer>> sme = map.entrySet();
// 遍历拼接
for (Map.Entry<String, Integer> me : sme) {
String s = me.getValue().toString() + me.getKey();
sortSet.add(s);
}
// 获取后出现次数最多的index个单词,带有出现次数
int o = sortSet.size();
// 记数
int c = 0;
for (int i = o-20; i < sortSet.size();) {
String te = sortSet.last();
sortSet.remove(te);
String temp = te.replaceAll("[^\\d]", "") + ":"
+ te.replaceAll("[\\d+]", "");
re_str[c++] = temp;
}
return re_str;
}
}