jieba java 磁性_jieba分词/jieba-analysis(java版)

史智志
2023-12-01

packagecom.analysis;importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.List;importjava.util.UUID;importorg.junit.Before;importorg.junit.Test;importcom.huaban.analysis.jieba.JiebaSegmenter;importcom.huaban.analysis.jieba.JiebaSegmenter.SegMode;importcom.huaban.analysis.jieba.SegToken;public classjiebaTest {private Connection con = null;private PreparedStatement pstmt = null;/*** 连接*/@Beforepublic void beforeDemo() throwsException {

Class.forName("com.mysql.jdbc.Driver");

String url= "jdbc:mysql://localhost:3306/test?user=root&password=root";

con=DriverManager.getConnection(url);

}/*** 分词查询测试*/@Testpublic void getDemo() throwsException {

BufferedReader br= new BufferedReader(newInputStreamReader(System.in));

String str=br.readLine();

String sql= "select * from t_jieba where name = ?";

pstmt=con.prepareStatement(sql);

pstmt.setString(1, str);

ResultSet rs=pstmt.executeQuery();while(rs.next()) {

System.out.println(rs.getInt(1)+"--"+rs.getString(2)+"--"+rs.getString(3)+"--"+rs.getString(4)+"--"+rs.getString(5));

pstmt.clearParameters();

String sql1= "update t_jieba set times = ? where id = ?";

pstmt=con.prepareStatement(sql1);

pstmt.setInt(1, 1+ new Integer(rs.getString(5)));

pstmt.setInt(2, rs.getInt(1));

pstmt.executeUpdate();

}

rs.close();

pstmt.close();

}/*** 分词插入测试*/@Testpublic void addDemo() throwsException {

String sql= "insert into t_jieba (name,cid,c_name,times) select ?,?,?,? from DUAL where not EXISTS(select name from t_jieba where name=?)";

pstmt=con.prepareStatement(sql);

JiebaSegmenter segmenter= newJiebaSegmenter();

String[] sentences= new String[] { "大话数据结构", "深入浅出设计模式", "JavaEE开发的颠覆者: Spring Boot实战", "java从入门到放弃"};for(String sentence : sentences) {//System.out.println(segmenter.process(sentence, SegMode.INDEX).toString());

String uuid =UUID.randomUUID().toString();

uuid= uuid.replace("-", "");

List list =segmenter.process(sentence, SegMode.INDEX);for(SegToken segToken : list) {

String name=segToken.word.trim();if (name != null && !"".equals(name)) {

pstmt.setString(1, segToken.word);

pstmt.setString(2, uuid);

pstmt.setString(3, sentence);

pstmt.setString(4, "0");

pstmt.setString(5, segToken.word);

pstmt.executeUpdate();

pstmt.clearParameters();

}

}

}

pstmt.close();

System.out.println("插入成功!");

}

}

 类似资料: