use of org.apache.lucene.search.similarities.LMDirichletSimilarity in project lucene-solr by apache.
the class LMDirichletSimilarityFactory method getSimilarity.
@Override
public Similarity getSimilarity() {
LMDirichletSimilarity sim = (mu != null) ? new LMDirichletSimilarity(mu) : new LMDirichletSimilarity();
sim.setDiscountOverlaps(discountOverlaps);
return sim;
}
use of org.apache.lucene.search.similarities.LMDirichletSimilarity in project Anserini by castorini.
the class DumpTweetsLtrData method main.
public static void main(String[] argv) throws Exception {
long curTime = System.nanoTime();
LtrArgs args = new LtrArgs();
CmdLineParser parser = new CmdLineParser(args, ParserProperties.defaults().withUsageWidth(90));
try {
parser.parseArgument(argv);
} catch (CmdLineException e) {
System.err.println(e.getMessage());
parser.printUsage(System.err);
System.err.println("Example: DumpTweetsLtrData" + parser.printExample(OptionHandlerFilter.REQUIRED));
return;
}
LOG.info("Reading index at " + args.index);
Directory dir = FSDirectory.open(Paths.get(args.index));
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
if (args.ql) {
LOG.info("Using QL scoring model");
searcher.setSimilarity(new LMDirichletSimilarity(args.mu));
} else if (args.bm25) {
LOG.info("Using BM25 scoring model");
searcher.setSimilarity(new BM25Similarity(args.k1, args.b));
} else {
LOG.error("Error: Must specify scoring model!");
System.exit(-1);
}
Qrels qrels = new Qrels(args.qrels);
FeatureExtractors extractors = null;
if (args.extractors != null) {
extractors = FeatureExtractors.loadExtractor(args.extractors);
}
PrintStream out = new PrintStream(new FileOutputStream(new File(args.output)));
RerankerCascade cascade = new RerankerCascade();
cascade.add(new RemoveRetweetsTemporalTiebreakReranker());
cascade.add(new TweetsLtrDataGenerator(out, qrels, extractors));
MicroblogTopicSet topics = MicroblogTopicSet.fromFile(new File(args.topics));
LOG.info("Initialized complete! (elapsed time = " + (System.nanoTime() - curTime) / 1000000 + "ms)");
long totalTime = 0;
int cnt = 0;
for (MicroblogTopic topic : topics) {
long curQueryTime = System.nanoTime();
Query filter = LongPoint.newRangeQuery(StatusField.ID.name, 0L, topic.getQueryTweetTime());
Query query = AnalyzerUtils.buildBagOfWordsQuery(StatusField.TEXT.name, IndexTweets.ANALYZER, topic.getQuery());
BooleanQuery.Builder builder = new BooleanQuery.Builder();
builder.add(filter, BooleanClause.Occur.FILTER);
builder.add(query, BooleanClause.Occur.MUST);
Query q = builder.build();
TopDocs rs = searcher.search(q, args.hits);
List<String> queryTokens = AnalyzerUtils.tokenize(IndexTweets.ANALYZER, topic.getQuery());
RerankerContext context = new RerankerContext(searcher, query, topic.getId(), topic.getQuery(), queryTokens, StatusField.TEXT.name, filter);
cascade.run(ScoredDocuments.fromTopDocs(rs, searcher), context);
long qtime = (System.nanoTime() - curQueryTime) / 1000000;
LOG.info("Query " + topic.getId() + " (elapsed time = " + qtime + "ms)");
totalTime += qtime;
cnt++;
}
LOG.info("All queries completed!");
LOG.info("Total elapsed time = " + totalTime + "ms");
LOG.info("Average query latency = " + (totalTime / cnt) + "ms");
reader.close();
out.close();
}
Aggregations