当前位置:   article > 正文

JAVA实现中文分词程序_java 中文分词实现

java 中文分词实现

中文分词程序

package test03;
import java.io.IOException;
import java.nio.file.Paths;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.hankcs.lucene.HanLPAnalyzer;

public class Demo2 {

	public static void main(String[] args) {
		try {
			Directory directory=FSDirectory.open(Paths.get("F:/index"));
			IndexReader reader=DirectoryReader.open(directory);
			IndexSearcher searcher=new IndexSearcher(reader);
			String text="我想要苹果手机";
			String field="title";
			Analyzer analyzer=new HanLPAnalyzer();
			QueryParser parser=new QueryParser(field,analyzer);
			Query query=parser.parse(text);
			TopDocs docs=searcher.search(query, 100);
			System.out.println("命中的记录数:"+docs.totalHits);
			ScoreDoc[] array=docs.scoreDocs;
			for(ScoreDoc one:array) {
				Document document=searcher.doc(one.doc);
				String id=document.get("id");
				String title=document.get("title");
				System.out.println("商品编号"+id);
				System.out.println("商品名称"+title);
			}
			reader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/609979
推荐阅读
相关标签
  

闽ICP备14008679号