LuceneChineseAnalyzerText.java
上传用户:cctqzzy
上传日期:2022-03-14
资源大小:12198k
文件大小:1k
- package chapter8;
- import java.io.IOException;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.TokenStream;
- import org.apache.lucene.analysis.Token;
- //import org.apache.lucene.analysis.cn;
- import java.util.*;
- import java.io.*;
- public class LuceneChineseAnalyzerText {
-
- private static String Dest_Index_Path = "D:\workshop\TextIndex";
- static protected String chinesedetail = "中文文档中最基础的结构是句子、短语、词汇、单个的汉字。中文环境的句子通常可以利用标点符号来分隔。" ;
- public static void main(String[] args) {
- try {
- Analyzer TextAnalyzer = new ChineseAnalyzer();
-
- IndexWriter TextIndex = new IndexWriter(Dest_Index_Path,TextAnalyzer,true);
- Document document = new Document();
- Field field_content = new Field("content", chinesedetail,
- Field.Store.YES,Field.Index.TOKENIZED);
- document.add(field_content);
- TextIndex.addDocument(document);
-
- TokenStream stream = TextAnalyzer.tokenStream("content", new StringReader(chinesedetail));
- while(true)
- {
- Token item = stream.next();
- if(null == item ) break;
- System.out.print("{"+item.termText()+"} ");
- }
- TextIndex.optimize();
- TextIndex.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("");
- System.out.println("Index success");
- }
- }