Lucene7.0與HanLP分詞器整合索引數據庫建立索引文件

HanLP官網:http://hanlp.linrunsoft.com/

GitHup地址:https://github.com/hankcs/HanLP

HanLP插件地址:https://github.com/hankcs/hanlp-lucene-plugin

需要一下jar包

package com.kyd.demo.hanLP;

import java.io.IOException;

import java.nio.file.Paths;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import org.apache.lucene.analysis.Analyzer;

import org.apache.lucene.analysis.TokenStream;

import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;

import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.LongPoint;

import org.apache.lucene.document.StringField;

import org.apache.lucene.document.TextField;

import org.apache.lucene.document.Field.Store;

import org.apache.lucene.document.IntPoint;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.index.IndexWriterConfig.OpenMode;

import org.apache.lucene.index.IndexableField;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.FSDirectory;

import org.junit.Test;

import com.hankcs.lucene.HanLPAnalyzer;

import com.hankcs.lucene.HanLPIndexAnalyzer;

/**

* 索引數據庫字段建立索引文件

*

* @author zhengzhen

*

*/

public class JdbcIndexDemo {

public static void main(String[] args) {

try {

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

String url = "jdbc:mysql://192.168.100.69:3306/xxxx?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false";

String password ="root";

String userName ="root";

String sql ="select * from xxxx";

try (

Connection conn = DriverManager.getConnection(url,userName,password);

PreparedStatement sta =conn.prepareStatement(sql);

ResultSet rs = sta.executeQuery();

){

/**

* 1.設置索引文件保存路徑

*/

Directory directory = FSDirectory.open(Paths.get("xxxx_index"));

/**

* 2.創(chuàng)建分詞器

*/

Analyzer analyzer = new HanLPIndexAnalyzer();

/**

* 3.分詞器配置

*/

IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);

indexWriterConfig.setOpenMode(OpenMode.CREATE);

/**

* 4.創(chuàng)建索引輸出流

*/

IndexWriter indexWriter = new IndexWriter(directory,indexWriterConfig);

/**

* 5.循環(huán)遍歷創(chuàng)建索引文檔

*/

while (rs.next()) {

/**

* 5.1.創(chuàng)建文檔

*/

Document document = new Document();

/**

* 5.2.添加字段

*/

Long id? =rs.getLong("unitId");

IndexableField unitIdField = new StringField("unitId", id+"",Store.YES);

document.add(unitIdField);

String title = rs.getString("title");

if( title != null) {

IndexableField sectionNameField = new TextField("sectionName", title, Store.YES);

document.add(sectionNameField);

}

String? unitName= rs.getString("unitName");

if( unitName != null) {

IndexableField unitNameField = new TextField("unitName", unitName, Store.YES);

document.add(unitNameField);

}

String? courseName= rs.getString("courseName");

if(courseName !=null) {

IndexableField courseNameField = new TextField("courseName", courseName, Store.YES);

document.add(courseNameField);

}

String? startPage= rs.getString("startPage");

if(startPage !=null) {

IndexableField startPageField = new StringField("startPage", startPage, Store.YES);

document.add(startPageField);

}

String? endPage= rs.getString("startEndPage");

if(endPage != null) {

IndexableField endPageField = new StringField("endPage", endPage,Store.YES);

document.add(endPageField);

}

indexWriter.addDocument(document);

}

indexWriter.commit();

} catch (Exception e) {

e.printStackTrace();

}

} catch (ClassNotFoundException e1) {

e1.printStackTrace();

}

}

/**

* HanLPAnalyzer

* 這個分詞器對于長詞不會切割 ,例如 “中華人民共和國” 是一個長詞會保留下來

* @throws IOException

*/

@Test

public void hanLPAnalyzerTest() throws IOException {

String text = "中華人民共和國很遼闊";

for (int i = 0; i < text.length(); ++i)

{

? ? System.out.print(text.charAt(i) + "" + i + " ");

}

System.out.println();

Analyzer analyzer = new HanLPAnalyzer();

TokenStream tokenStream = analyzer.tokenStream("field", text);

tokenStream.reset();

while (tokenStream.incrementToken())

{

? ? CharTermAttribute attribute = tokenStream.getAttribute(CharTermAttribute.class);

? ? // 偏移量

? ? OffsetAttribute offsetAtt = tokenStream.getAttribute(OffsetAttribute.class);

? ? // 距離

? ? PositionIncrementAttribute positionAttr = tokenStream.getAttribute(PositionIncrementAttribute.class);

? ? System.out.println(attribute + " " + offsetAtt.startOffset() + " " + offsetAtt.endOffset() + " " + positionAttr.getPositionIncrement());

}

/* 輸出:

* 中0 華1 人2 民3 共4 和5 國6 很7 遼8 闊9

* 中華人民共和國 0 7 1

* 很 7 8 1

* 遼闊 8 10 1

*/

}

/**

* HanLPIndexAnalyzer

* 這個分詞器會對長詞進行分割 “中華人民共和國” 會切分成“中華人民共和國” “中華” “人民”等等

* @throws IOException

*/

@Test

public void hanLPIndexAnalyzerTest() throws IOException {

String text = "中華人民共和國很遼闊";

for (int i = 0; i < text.length(); ++i)

{

? ? System.out.print(text.charAt(i) + "" + i + " ");

}

System.out.println();

Analyzer analyzer = new HanLPIndexAnalyzer();

TokenStream tokenStream = analyzer.tokenStream("field", text);

tokenStream.reset();

while (tokenStream.incrementToken())

{

? ? CharTermAttribute attribute = tokenStream.getAttribute(CharTermAttribute.class);

? ? // 偏移量

? ? OffsetAttribute offsetAtt = tokenStream.getAttribute(OffsetAttribute.class);

? ? // 距離

? ? PositionIncrementAttribute positionAttr = tokenStream.getAttribute(PositionIncrementAttribute.class);

? ? System.out.println(attribute + " " + offsetAtt.startOffset() + " " + offsetAtt.endOffset() + " " + positionAttr.getPositionIncrement());

}

/* 輸出:

* 中0 華1 人2 民3 共4 和5 國6 很7 遼8 闊9

* 中華人民共和國 0 7 1

* 中華人民 0 4 1

* 中華 0 2 1

* 華人 1 3 1

* 人民共和國 2 7 1

* 人民 2 4 1

* 共和國 4 7 1

* 共和 4 6 1

* 很 7 8 1

* 遼闊 8 10 1

*/

}

}




文章來源于雨夜星辰03的博客

?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現,斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 實驗目的 理解四種數據庫(MySQL,HBase,Redis,MongoDB)的概念以及不同點。 熟練使用四種數據...
    Tiny_16閱讀 18,165評論 5 12
  • 寫在前面:本文中用到的 Apache Lucene 版本號是 4.10.2 截止到文章發(fā)布時官方的最新版本是 6....
    SawyerZh閱讀 4,552評論 2 15
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,625評論 18 399
  • 風嘯雪飄枝綻放, 。 一片蒼茫獨傲立,你姐姐你。 只為伊人玉手香。那邊距不可開交急it急急不健康i
    久龍治水閱讀 408評論 0 0

友情鏈接更多精彩內容