linqy的个人博客分享 http://blog.sciencenet.cn/u/linqy

博文

lucene的中词中采用mmseg4j分词,并建立索引和搜索

已有 1553 次阅读 2017-10-24 14:14 |个人分类:Lucene|系统分类:科研笔记| Lucene, mmseg, java

mmseg中文分词具有较好的效果,因此Lucene使用的话会是搜索的结果更加合理

下面给出了Lucene中利用mmseg分词的示例:


//test mmseg4j 分词
class LuceneTest {
static Analyzer analyzer = null;
static Directory directory = null;
static String text = "CSDN.NET - 全球最大中文IT社区,为IT专业技术人员提供最全面的信息传播和服务平台";
static String text1 = "京华时报1月23日报道 昨天,受一股来自中西伯利亚的强冷空气影响,本市出现大风降温天气,白天最高气温只有零下7摄氏度,同时伴有6到7级的偏北风。";

public void mmesgtest() throws Exception {
analyzer = new ComplexAnalyzer();//mmseg分词的方法之一,也有simple和maxword方法
//创建索引目录
String indexpath = "D:\\java_test\\lucene\\data_index_mmesg";
File index_path = new File(indexpath);
Path path_mmseg = index_path.toPath();
Directory directory = FSDirectory.open(path_mmseg);
IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer);
iwConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter iwriter = new IndexWriter(directory, iwConfig);
List<String> list = new ArrayList<String>();
list.add(text);
list.add(text1);
for (String item : list) {
Document doc = new Document();
doc.add(new TextField("text", item, Field.Store.YES));
iwriter.addDocument(doc);
}
iwriter.close();

DirectoryReader ireader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(ireader);
Query q = new TermQuery(new Term("text", "西伯利亚"));
System.out.println(q);
TopDocs tds = searcher.search(q, 10);
System.out.println("======size:" + tds.totalHits + "========");
for (ScoreDoc sd : tds.scoreDocs) {
System.out.println(sd.score);
System.out.println(searcher.doc(sd.doc).get("text"));
}
}
}




https://blog.sciencenet.cn/blog-3134052-1082259.html

上一篇:获取Lucene分词的内容
下一篇:Lucene中进行分词,并按照特定的词对document或者field加权boost
收藏 IP: 111.195.160.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-13 14:21

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部