文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 工程科技 > 建筑/土木 > lucene IKAnalyzer 中文分词及索引,简单实例
侵权投诉

lucene IKAnalyzer 中文分词及索引,简单实例

lucene IKAnalyzer 中文分词及索引,简单实例

lucene + IKAnalyzer 中文分词及索引,简单实例.txt你看得见我打在屏幕上的字,却看不到我掉在键盘上的泪!自己选择45°仰视别人,就休怪他人135°俯视着看你。doc.guandang.net


import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.mira.lucene.analysis.IK_CAnalyzer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.util.Date;

public class Searcher {
private static String INDEX_DIR = Searcher.class.getResource("/").getPath()+"/data/index";//"c:\\lucene\\index";
private static String DOC_DIR = Searcher.class.getResource("/").getPath();//"c:\\lucene\\doc";
public static void main(String[] args) throws Exception {
String queryString;
queryString = "测试";
File indexDir = new File(INDEX_DIR);
File docDir = new File(DOC_DIR);
Date start = new Date();
//必须先建索引
try {
IndexWriter writer = new IndexWriter(INDEX_DIR, new IK_CAnalyzer(), true);
System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
indexDocs(writer, docDir);
System.out.println("Optimizing...");
writer.optimize();
writer.close();
Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");

} catch (IOException e) {
System.out.println(" caught a " + e.getClass() +
"\n with message: " + e.getMessage());
}
if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir
+ " does not exist or is not a directory.");
}
search(indexDir, queryString);
}

private static void indexDocs(IndexWriter writer, File file)
throws IOException {
if (file.canRead()) {
if (file.isDirectory()) {
String[] files = file.list();
if (files != null) {
for (int i = 0; i < files.length; i++) {
indexDocs(writer, new File(file, files[i]));
}
}
} else {
System.out.println("adding " + file);
try {

第1页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email