62,615
社区成员
发帖
与我相关
我的任务
分享
Provides a convenient implementation of the HttpServletRequest interface that can
be subclassed by developers wishing to adapt the request to a Servlet. This class
implements the Wrapper or Decorator pattern. Methods default to calling through to
the wrapped request object.
Objects that are bound to a session may listen to container events notifying them
that sessions will be passivated and that session will be activated. A container
that migrates session between VMs or persists sessions is required to notify all
attributes bound to sessions implementing HttpSessionActivationListener.
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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;
public class LuceneTest {
public static void main(String[] args) {
// 超纲词汇列表
List<String> wordList = new ArrayList<String>();
wordList.add("convenient");
wordList.add("attributes");
wordList.add("required");
wordList.add("human");
wordList.add("computer");
wordList.add("object");
// 索引存放的路径
File indexDir = new File("f:/test/index");
// 文章文件名
File srcFile = new File("f:/test/english.txt");
// 建立 Lucene 索引,在文件不改动时,只需要执行一次
createIndex(indexDir, srcFile);
System.out.println("超纲词汇如下:");
List<String> overList = getOverWords(indexDir, wordList);
for (String s : overList) {
System.out.println(s);
}
}
private static void createIndex(File indexDir, File srcFile) {
IndexWriter writer = null;
try {
writer = new IndexWriter(indexDir, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
Document doc = new Document();
System.out.println("Indexing...");
doc.add(new Field("content", new FileReader(srcFile)));
writer.addDocument(doc);
System.out.println("Index finshed.");
writer.optimize();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static List<String> getOverWords(File indexDir,
List<String> wordList) {
IndexWriter writer = null;
List<String> overList = new ArrayList<String>();
try {
writer = new IndexWriter(indexDir, new StandardAnalyzer(), false);
Directory fsDir = writer.getDirectory();
IndexSearcher is = new IndexSearcher(fsDir);
QueryParser parse = new QueryParser("content",
new StandardAnalyzer());
for (int i = 0, k = wordList.size(); i < k; i++) {
String word = wordList.get(i);
Query query = parse.parse(word);
Hits hits = is.search(query);
if (hits.length() > 0) {
overList.add(word);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return overList;
}
}