root / tags / 1.0.8 / xapian-core / examples / simpleexpand.cc
| Revision 9248, 3.3 kB (checked in by olly, 16 months ago) | |
|---|---|
|
|
| Line | |
|---|---|
| 1 | /** @file simpleexpand.cc |
| 2 | * @brief Simple example program demonstrating query expansion. |
| 3 | */ |
| 4 | /* Copyright (C) 2007 Olly Betts |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <xapian.h> |
| 22 | |
| 23 | #include <iostream> |
| 24 | #include <string> |
| 25 | |
| 26 | #include <stdlib.h> // For exit(). |
| 27 | #include <string.h> |
| 28 | |
| 29 | using namespace std; |
| 30 | |
| 31 | int |
| 32 | main(int argc, char **argv) |
| 33 | try { |
| 34 | // We require at least two command line arguments. |
| 35 | if (argc < 3) { |
| 36 | cout << "Usage: " << argv[0] << " PATH_TO_DATABASE QUERY [-- [DOCID...]]" << endl; |
| 37 | exit(1); |
| 38 | } |
| 39 | |
| 40 | // Open the database for searching. |
| 41 | Xapian::Database db(argv[1]); |
| 42 | |
| 43 | // Start an enquire session. |
| 44 | Xapian::Enquire enquire(db); |
| 45 | |
| 46 | // Combine command line arguments up to "--" with spaces between |
| 47 | // them, so that simple queries don't have to be quoted at the shell |
| 48 | // level. |
| 49 | string query_string(argv[2]); |
| 50 | argv += 3; |
| 51 | while (*argv && strcmp(*argv, "--") != 0) { |
| 52 | query_string += ' '; |
| 53 | query_string += *argv++; |
| 54 | } |
| 55 | |
| 56 | // Create an RSet with the listed docids in. |
| 57 | Xapian::RSet rset; |
| 58 | if (*argv) { |
| 59 | while (*++argv) { |
| 60 | rset.add_document(atoi(*argv)); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Parse the query string to produce a Xapian::Query object. |
| 65 | Xapian::QueryParser qp; |
| 66 | Xapian::Stem stemmer("english"); |
| 67 | qp.set_stemmer(stemmer); |
| 68 | qp.set_database(db); |
| 69 | qp.set_stemming_strategy(Xapian::QueryParser::STEM_SOME); |
| 70 | Xapian::Query query = qp.parse_query(query_string); |
| 71 | cout << "Parsed query is: " << query.get_description() << endl; |
| 72 | |
| 73 | // Find the top 10 results for the query. |
| 74 | enquire.set_query(query); |
| 75 | Xapian::MSet matches = enquire.get_mset(0, 10, &rset); |
| 76 | |
| 77 | // Display the results. |
| 78 | cout << matches.get_matches_estimated() << " results found:" << endl; |
| 79 | |
| 80 | for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) { |
| 81 | cout << i.get_rank() + 1 << ": " << i.get_percent() << "% docid=" << *i |
| 82 | << " [" << i.get_document().get_data() << "]\n\n"; |
| 83 | } |
| 84 | |
| 85 | // If no relevant docids were given, invent an RSet containing the top 5 |
| 86 | // matches (or all the matches if there are less than 5). |
| 87 | if (rset.empty()) { |
| 88 | int c = 5; |
| 89 | Xapian::MSetIterator i = matches.begin(); |
| 90 | while (c-- && i != matches.end()) { |
| 91 | rset.add_document(*i); |
| 92 | ++i; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // Generate an ESet containing terms that the user might want to add to |
| 97 | // the query. |
| 98 | Xapian::ESet eset = enquire.get_eset(10, rset); |
| 99 | |
| 100 | // List the terms. |
| 101 | Xapian::ESetIterator t; |
| 102 | for (t = eset.begin(); t != eset.end(); ++t) { |
| 103 | cout << *t << ": weight = " << t.get_weight() << endl; |
| 104 | } |
| 105 | } catch (const Xapian::Error &e) { |
| 106 | cout << e.get_description() << endl; |
| 107 | exit(1); |
| 108 | } |
Note: See TracBrowser
for help on using the browser.
