2 * @brief Index each paragraph of a text file as a Xapian document.
4 /* Copyright (C) 2007-2022 Olly Betts
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.
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.
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
30 #include <cstdlib> // For exit().
36 main(int argc
, char **argv
)
38 if (argc
!= 2 || argv
[1][0] == '-') {
41 if (strcmp(argv
[1], "--version") == 0) {
42 cout
<< "simpleindex\n";
45 if (strcmp(argv
[1], "--help") == 0) {
49 cout
<< "Usage: " << argv
[0] << " PATH_TO_DATABASE\n"
50 "Index each paragraph of a text file as a Xapian document.\n";
54 // Open the database for update, creating a new database if necessary.
55 Xapian::WritableDatabase
db(argv
[1], Xapian::DB_CREATE_OR_OPEN
);
57 Xapian::TermGenerator indexer
;
58 Xapian::Stem
stemmer("english");
59 indexer
.set_stemmer(stemmer
);
60 indexer
.set_stemming_strategy(indexer
.STEM_SOME_FULL_POS
);
66 if (para
.empty()) break;
73 // We've reached the end of a paragraph, so index it.
77 indexer
.set_document(doc
);
78 indexer
.index_text(para
);
80 // Add the document to the database.
86 if (!para
.empty()) para
+= ' ';
91 // Explicitly commit so that we get to see any errors. WritableDatabase's
92 // destructor will commit implicitly (unless we're in a transaction) but
93 // will swallow any exceptions produced.
95 } catch (const Xapian::Error
&e
) {
96 cout
<< e
.get_description() << '\n';