Пример #1
0
int main(int argc, char **argv)
{
    // Simplest possible options parsing: we just require two or more
    // parameters.
    if (argc < 3) {
        cout << "usage: " << argv[0] << " <path to database> <search terms>" << endl;
        exit(1);
    }
 
    // Catch any Xapian::Error exceptions thrown
    try {
        // Make the database
	Xapian::Database db(argv[1]);
 
        // Start an enquire session
	Xapian::Enquire enquire(db);
         
        // Set percent and/or weight cutoffs
        enquire.set_cutoff(90,0.2);
         
        // Set weighting schema
        BM25Weight bm1(1.0,0.0,1.0,0.5,0.3);
        enquire.set_weighting_scheme(bm1);
 
        // Build the query object
	Xapian::Query query(Xapian::Query::OP_AND, argv + 2, argv + argc);
        cout << "Performing query" << query.get_description() << "'" << endl;
	
        // Set Stopper
        string stop[8]={"的","了","呵","吧","就","你","我","他"};
        SimpleStopper *ss=new SimpleStopper;
        for(int i=0;i<8;i++){
            ss->add(stop[i]);
        }
        QueryParser qparser;
        qparser.set_stopper(ss);
        qparser.set_database(db);
 
        // Give the query object to the enquire session
        enquire.set_query(query);
 
        // Get the top 10 results of the query
	Xapian::MSet matches = enquire.get_mset(0, 10);                     //最多返回10个文档
 
        // Display the results
        cout << matches.size() << " results found" << endl;
 
        for (Xapian::MSetIterator i = matches.begin();i != matches.end(); ++i) {
	    Xapian::Document doc = i.get_document();
            cout << "Document ID " << *i << "\nPercent " <<i.get_percent() << "%\n" << doc.get_data() << "\n" << endl;
        }
        db.close();
    } catch(const Xapian::Error &error) {
        cout << "Exception: "  << error.get_msg() << endl;
    }
}
Пример #2
0
int
get(int argc, char** argv) {
    // parse arguments
    parseArguments(argc, argv);
    string backend = options['t'];
    string indexdir = options['d'];

    // check arguments: indexdir
    if (indexdir.length() == 0) {
        pe("Provide the directory with the index.\n");
        return usage(argc, argv);
    }

    // check arguments: dirs
    if (arguments.size() == 0) {
        pe("'%s' '%s'\n", backend.c_str(), indexdir.c_str());
        pe("Provide one or more files to search.\n");
        return usage(argc, argv);
    }
    
    // create an index manager
    IndexManager* manager = getIndexManager(backend, indexdir);
    if (manager == 0) {
        return usage(argc, argv);
    }
    IndexReader* reader = manager->indexReader();
    QueryParser parser;
    
    for (vector<string>::iterator iter = arguments.begin();
         iter != arguments.end(); ++iter) {
        Query query = parser.buildQuery( "system.location:'"+ *iter + '\'');
        vector<IndexedDocument> matches = reader->query(query, 0, 10);
        if (matches.size() == 0)
            printf ("%s: is not indexed\n", iter->c_str());
        else
        {
            printf ("Information associated to %s:\n", iter->c_str());
            for (vector<IndexedDocument>::iterator it = matches.begin();
                 it != matches.end(); ++it)
            {
                printIndexedDocument(*it);
            }
        }
    }
    
    IndexPluginLoader::deleteIndexManager(manager);
    return 0;
}
// static
Query* MultiFieldQueryParser::parse(const TCHAR* query, const TCHAR** _fields, const uint8_t* flags, Analyzer* analyzer) {
	BooleanQuery* bQuery = _CLNEW BooleanQuery();
	for (size_t i = 0; _fields[i]!=NULL; i++) {
	  //TODO: this is really confusing... why not refactor _fields and flags to use a array object.
	  //flags can be NULL since NULL == 0...
		/*if (flags[i] == NULL) {
			_CLLDELETE(bQuery);
			_CLTHROWA(CL_ERR_IllegalArgument, "_fields.length != flags.length");
		}*/
		QueryParser* qp = _CLNEW QueryParser(_fields[i], analyzer);
		Query* q = qp->parse(query);
		if (q!=NULL && // q never null, just being defensive
			(!(q->instanceOf(BooleanQuery::getClassName())) || ((BooleanQuery*)q)->getClauseCount()>0)) {
				bQuery->add(q, true, (BooleanClause::Occur)flags[i]);
		} else
			_CLLDELETE(q);
		_CLLDELETE(qp);
	}
	return bQuery;
}
//static
Query* MultiFieldQueryParser::parse(const TCHAR** _queries, const TCHAR** _fields, Analyzer* analyzer)
{
	BooleanQuery* bQuery = _CLNEW BooleanQuery();
	for (size_t i = 0; _fields[i]!=NULL; i++)
	{
		if (_queries[i] == NULL) {
			_CLLDELETE(bQuery);
			_CLTHROWA(CL_ERR_IllegalArgument, "_queries.length != _fields.length");
		}
		// TODO: Reuse qp instead of creating it over and over again
		QueryParser* qp = _CLNEW QueryParser(_fields[i], analyzer);
		Query* q = qp->parse(_queries[i]);
		if (q!=NULL && // q never null, just being defensive
			(!(q->instanceOf(BooleanQuery::getClassName()) || ((BooleanQuery*)q)->getClauseCount() > 0))) {
				bQuery->add(q, true, BooleanClause::SHOULD);
		} else
			_CLLDELETE(q);
		_CLLDELETE(qp);
	}
	return bQuery;
}
Пример #5
0
int main(int argc, char* argv[])
{
// Get the top level suite from the registry
CPPUNIT_NS::Test *suite = CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
CppUnit::TextUi::TestRunner runner;

//parse the SIMPLE soource code into the parser
//Parse();
string t;
Parser* parser;
PKBController* controller;

parser = new Parser("../Debug/Sample-Source.txt");
controller = parser->controller;

//string query = "assign a; Select a pattern a(\"oSCar\", \"1 + beta + tmp\" )";
string query = "assign a; Select a pattern a(\"oSCar\", \"_\" )";

QueryParser* qp = new QueryParser(query);
QueryEvaluator* qe = new QueryEvaluator(controller);

qp->getQueryTree()->printTree();
cout << qp->getQueryTree()->getRootNode()->getChild(2)->getKey() << endl;;
cin >> t;



//Call of DesignExtractor
//Extract();

runner.addTest(suite);
bool wasSucessful = runner.run();


//getchar();

return wasSucessful ? 0 : 1;
}
Пример #6
0
EngineQuery SearchStore::constructContainsQuery(const QByteArray& prefix, const QString& value)
{
    QueryParser parser;
    return parser.parseQuery(value, prefix);
}
void queryDriver(string query, list<string> &result, PKB *pkb){
	//// cout<<"Begin parse query"<<endl;
	pkb->printParentTable();
	pkb->printModifiesTable();
	pkb->printModifiesProcTable();

	/*cout<<"test here"<<endl;
	cout<<"getChildren 15"<<endl;
	set<STMTNUM> temp = pkb->getChildren(15);
	for(set<STMTNUM>::iterator it = temp.begin();it!=temp.end();it++){
		cout<<*it<<endl;
	}
	cout<<"getParent 63"<<endl;
	cout<<pkb->getParent(63)<<endl;*/



	QueryParser qp;
	QueryEvaluator qe = QueryEvaluator::QueryEvaluator(pkb);

	bool isValid = true;
	Query parsedQuery = qp.queryParse(query,isValid);

	if(!isValid){
		//// cout << "Query Invalid" << endl;
		return;
	}

	//// cout<<"End parse query"<<endl;
	

	//// cout<<"Begin evaluate query"<<endl;

	vector<Relationship> newRelations;
	unordered_map<string, int> synIndexMap;
	unordered_map<int, vector<Pair>> clauseAnswers = qe.evaluateQuery(parsedQuery, &newRelations);

	//// cout<<"End evaluate query"<<endl;

	//// cout <<"Begin projecting results"<< endl;
	vector<vector<int>> tupleTable = createTupleTable(clauseAnswers, newRelations, &synIndexMap );

	vector<string> selectedSyn = parsedQuery.getSelectedSyn();
	unordered_map<string, TypeTable::SynType> synTable = parsedQuery.getSynTable();
	set<string> ansSet;
	ansSet.insert("");

	if(synTable.find(selectedSyn.at(0))->second == TypeTable::BOOLEAN) {
		if(tupleTable.size()!=0)
			result.push_back("true");
		else
			result.push_back("false");

		return;
	}

	//// cout << "Begin handling unprocessed selected synonyms" << endl;
	for(int i=0; i<int(selectedSyn.size()); i++) {
		//// cout << "Checking for the following syn: " << selectedSyn.at(i) << endl;
		if(synIndexMap.count(selectedSyn.at(i)) == 0) {
			//// cout << selectedSyn.at(i) << " has not been processed" << endl;
			unordered_map<string, TypeTable::SynType>::iterator it = synTable.find(selectedSyn.at(i));

			vector<vector<int>> newTable;
			set<int> allOfType;

			if(it->second == TypeTable::VARIABLE) {
				allOfType = pkb->getAllVarIndex();
			}
			else if(it->second == TypeTable::CONSTANT) {
				set<int> allConstIndex = pkb->getAllConstIndex();
				ConstTable* constTable = pkb->getConstTable();
				for(set<int>::iterator it=allConstIndex.begin(); it!=allConstIndex.end(); it++) {
					allOfType.insert(atoi(constTable->getConst(*it).c_str()));
				}
			}
			else if(it->second == TypeTable::PROCEDURE) {
				allOfType = pkb->getAllProcIndexes();
			}
			else if(it->second == TypeTable::STMTLST) {
				allOfType = pkb->getAllStmtList();
			}
			else {
				allOfType = pkb->getAllStmts(it->second);
			}

			for(vector<vector<int>>::iterator it1=tupleTable.begin(); it1!=tupleTable.end(); it1++) {
				for(set<int>::iterator it2=allOfType.begin(); it2!=allOfType.end(); it2++) {
					vector<int> newTuple;
					newTuple.insert(newTuple.begin(), it1->begin(), it1->end());
					newTuple.push_back(*it2);
					newTable.push_back(newTuple);
				}				
			}

			synIndexMap.insert(make_pair(selectedSyn.at(i), synIndexMap.size()));
			tupleTable = newTable;
		}
		else {
			//// cout << "Syn has already been processed." << endl;
		;}
	}
	//// cout << "End handling unprocessed selected synonyms" << endl;

	if(tupleTable.size()==0)
		return;

	//// cout << "Begin creating tuple of answers" << endl;
	for(int i=0; i<int(tupleTable.size()); i++) {
		//// cout << "Creating tuple of answer " << i << endl;
		string ans = "";
		for(int j=0; j<int(selectedSyn.size()); j++) {
			//// cout << "Adding \"" << selectedSyn.at(j) << " \"to tuple." << endl;
			unordered_map<string, TypeTable::SynType>::iterator it = synTable.find(selectedSyn.at(j));
			int index = synIndexMap.at(selectedSyn.at(j));
			//// cout << selectedSyn.at(j) << " has index " << index << endl;

			int tempAns = tupleTable.at(i).at(index);

			if(it->second == TypeTable::VARIABLE) {
				VarTable* varTable = pkb->getVarTable();
				ans = ans + varTable->getVarName(tempAns) + " ";
				//// cout << "Adding " << varTable->getVarName(tempAns) << " to tuple" << endl;
			}
			/*else if(it->second == TypeTable::CONSTANT) {
				ConstTable* constTable = pkb->getConstTable();
				ans = ans + constTable->getConst(tempAns) + " ";
				//// cout << "Adding " << constTable->getConst(tempAns) << " to tuple" << endl;
			}*/
			else if(it->second == TypeTable::PROCEDURE && !parsedQuery.getSelectedSynIsCallProcedure().at(j)) {
				ProcTable* procTable = pkb->getProcTable();
				ans = ans + procTable->getProcName(tempAns) + " ";
				//// cout << "Adding " << procTable->getProcName(tempAns) << " to tuple" << endl;
			}
			else {
				ans = ans + to_string(static_cast<long long>(tempAns)) + " ";
				//// cout << "Adding " << to_string(static_cast<long long>(tempAns)) << " to tuple" << endl;
			}
			//// cout << endl;
		}
		ans = ans.substr(0, ans.length()-1);
		//// cout << "Final Tuple: " << ans << endl;
		//// cout << endl;
		//// cout << endl;
		if(ansSet.find(ans) == ansSet.end()) {
			result.push_back(ans);
			ansSet.insert(ans);
		}
	}
	//// cout << "End creating tuple of answers" << endl;

	//// cout <<"End projecting results"<< endl;
	
}
Пример #8
0
std::unique_ptr<QueryTree> unpackFromQuery(const Query& query)
{
	QueryParser parser;
	return parser.parse(query);
}
Пример #9
0
int
query(int argc, char** argv) {
    // parse arguments
    parseArguments(argc, argv);
    string backend = options['t'];
    string indexdir = options['d'];

    // check arguments: indexdir
    if (indexdir.length() == 0) {
        pe("Provide the directory with the index.\n");
        return usage(argc, argv);
    }

    // check arguments: dirs
    if (arguments.size() == 0) {
        pe("'%s' '%s'\n", backend.c_str(), indexdir.c_str());
        pe("Provide one or more files to search.\n");
        return usage(argc, argv);
    }
    
    // create an index manager
    IndexManager* manager = getIndexManager(backend, indexdir);
    if (manager == 0) {
        return usage(argc, argv);
    }
    IndexReader* reader = manager->indexReader();
    QueryParser parser;
    
    for (vector<string>::iterator iter = arguments.begin();
            iter != arguments.end(); ++iter) {
        int results = 0;
        Query query = parser.buildQuery(*iter);
        const uint batchsize = 10;
        vector<IndexedDocument> matches = reader->query(query, 0, batchsize);

        if (matches.size() != 0) {
            printf ("Results for search \"%s\"\n", (*iter).c_str());
        } else {
            printf ("No results for search \"%s\"\n", (*iter).c_str());
        }

        while (matches.size() > 0) {
            for (vector<IndexedDocument>::iterator it = matches.begin();
                 it != matches.end(); it++) {
                printf ("\"%s\" matched\n", it->uri.c_str());
                printIndexedDocument(*it);
            }

            results += (int)matches.size();
            
            if (matches.size() == batchsize) {
                // maybe there're other results
                matches = reader->query(query, results + 1, 10);
            } else {
                matches.clear(); // force while() exit
            }
        }

        if (results != 0) {
            printf ("Query \"%s\" returned %i results\n", (*iter).c_str(),
                                                           results);
        }
    }
    
    IndexPluginLoader::deleteIndexManager(manager);
    return 0;
}