|
Revision 8874, 1.9 kB
(checked in by olly, 19 months ago)
|
|
examples/simpleindex.cc: Tweak the logic to be clearer.
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
Author Date Id Revision
|
| Line | |
|---|
| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include <xapian.h> |
|---|
| 22 | |
|---|
| 23 | #include <iostream> |
|---|
| 24 | #include <string> |
|---|
| 25 | |
|---|
| 26 | #include <stdlib.h> // For exit(). |
|---|
| 27 | |
|---|
| 28 | using namespace std; |
|---|
| 29 | |
|---|
| 30 | int |
|---|
| 31 | main(int argc, char **argv) |
|---|
| 32 | try { |
|---|
| 33 | if (argc != 2) { |
|---|
| 34 | cout << "Usage: " << argv[0] << " PATH_TO_DATABASE" << endl; |
|---|
| 35 | exit(1); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | Xapian::WritableDatabase db(argv[1], Xapian::DB_CREATE_OR_OPEN); |
|---|
| 40 | |
|---|
| 41 | Xapian::TermGenerator indexer; |
|---|
| 42 | Xapian::Stem stemmer("english"); |
|---|
| 43 | indexer.set_stemmer(stemmer); |
|---|
| 44 | |
|---|
| 45 | string para; |
|---|
| 46 | while (true) { |
|---|
| 47 | string line; |
|---|
| 48 | if (cin.eof()) { |
|---|
| 49 | if (para.empty()) break; |
|---|
| 50 | } else { |
|---|
| 51 | getline(cin, line); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | if (line.empty()) { |
|---|
| 55 | if (!para.empty()) { |
|---|
| 56 | |
|---|
| 57 | Xapian::Document doc; |
|---|
| 58 | doc.set_data(para); |
|---|
| 59 | |
|---|
| 60 | indexer.set_document(doc); |
|---|
| 61 | indexer.index_text(para); |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | db.add_document(doc); |
|---|
| 65 | |
|---|
| 66 | para = ""; |
|---|
| 67 | } |
|---|
| 68 | } else { |
|---|
| 69 | if (!para.empty()) para += ' '; |
|---|
| 70 | para += line; |
|---|
| 71 | } |
|---|
| 72 | } |
|---|
| 73 | } catch (const Xapian::Error &e) { |
|---|
| 74 | cout << e.get_description() << endl; |
|---|
| 75 | exit(1); |
|---|
| 76 | } |
|---|