Initial import into git.
[galago.git] / cpp / galago / appsrc / galago.cpp
blobb33c120fba6089fad706ff65df266d25115b4ac6
2 //
3 // galago
4 //
5 // 5 January 2007 -- tds
6 //
8 #include "indri/ScoredExtentResult.hpp"
9 #include "indri/Parameters.hpp"
10 #include "lemur/Exception.hpp"
11 #include "Retrieval.hpp"
12 #include <iostream>
13 #include <stdlib.h>
14 #include <fstream>
17 // usage
20 static void usage() {
21 std::cerr << "galago --query.text=hi --query.number=1 --count=10 --index=myindex" << std::endl;
22 exit(-1);
26 // require
29 static void require( indri::api::Parameters param, const std::string& p ) {
30 if( !param.exists(p) )
31 usage();
35 // command_line
38 static void command_line( Retrieval& retrieval, indri::api::Parameters& parameters ) {
39 // record results requested
40 int requested = parameters.get( "count", 1000 );
42 // for each query, run it, get the results, look up the docnos, print in TREC format
43 indri::api::Parameters queries = parameters["query"];
45 for( int i=0; i<queries.size(); i++ ) {
46 std::string queryText = queries[i]["text"];
47 std::string queryName = queries[i]["number"];
48 int threshold = (int)(INT64)queries[i].get( "threshold", -1 );
50 std::vector<indri::api::ScoredExtentResult> results;
51 std::vector<QueryTerm> terms = Query::parse( queryText );
52 results = retrieval.runQuery( terms, requested, threshold );
54 for( int i=0; i<results.size(); i++ ) {
55 std::string document = retrieval.getDocument(results[i].document);
56 int score = (int)results[i].score;
57 int rank = i+1;
59 printf( "%s Q0 %s %d %d galago\n", queryName.c_str(), document.c_str(), rank, score );
65 // main
68 int main( int argc, char** argv ) {
69 try {
70 // read in parameters
71 indri::api::Parameters::instance().loadCommandLine( argc, argv );
72 indri::api::Parameters& parameters = indri::api::Parameters::instance();
74 // open index
75 std::string indexPath = parameters["index"];
76 Retrieval* retrieval = Retrieval::open( indexPath );
78 command_line( *retrieval, parameters );
79 } catch( lemur::api::Exception& e ) {
80 LEMUR_ABORT(e);
83 return 0;