xapian-core  2.1.0
index_utils.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2007,2013,2026 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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "index_utils.h"
24 
25 #include "errno_to_string.h"
26 #include "stringutils.h"
27 
28 #include <algorithm>
29 #include <cerrno>
30 #include <cstring>
31 
32 using namespace std;
33 
34 static string munge_term(const string &term);
35 
37 static string
39 {
40  string para, line;
41  while (true) {
42  getline(input, line);
43  if (find_if(line.begin(), line.end(), C_isnotspace) == line.end())
44  return para;
45  para += line;
46  para += '\n';
47  }
48 }
49 
50 void
52 {
53  Xapian::Stem stemmer("english");
54 
55  while (file != end || (input.is_open() && !input.eof())) {
56  if (input.eof()) next_file();
57 
58  Xapian::Document doc;
59  string para = get_paragraph(input);
60  doc.set_data(para);
61 
62  // Value 0 contains all possible character values so we can check that
63  // none of them cause problems.
64  string value0("X\0\0\0 \1\t"
65  "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
66  "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
67  "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
68  "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f"
69  "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f"
70  "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
71  "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
72  "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
73  "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
74  "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
75  "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
76  "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf"
77  "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf"
78  "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf"
79  "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
80  "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
81  7 + 256);
82  if (para.size() > 2) value0[0] = para[2];
83  value0 += para;
84  doc.add_value(0, value0);
85 
86  for (Xapian::valueno i = min(para.length(), size_t(10)); i >= 1; --i) {
87  doc.add_value(i, para.substr(i, 1));
88  }
89  // Value 11 is useful for tests of sorting
90  doc.add_value(11, Xapian::sortable_serialise(double(para.size())));
91 
92  // Value 12 is useful for tests of collapsing
93  doc.add_value(12, Xapian::sortable_serialise(double(para.size() % 5)));
94 
95  // Value 13 contains the first 3 letters of the paragraph
96  doc.add_value(13, para.substr(0, 3));
97 
98  Xapian::termpos pos = 0;
99  string::const_iterator word_end = para.begin();
100  // Need a const_iterator version of para.end() for find_if.
101  const string::const_iterator para_end = para.end();
102  while (word_end != para_end) {
103  string::const_iterator word_start;
104  word_start = find_if(word_end, para_end, C_isnotspace);
105  word_end = find_if(word_start, para_end, C_isspace);
106  string word = stemmer(munge_term(string(word_start, word_end)));
107  if (!word.empty()) doc.add_posting(word, ++pos);
108  }
109 
110  db.add_document(doc);
111  }
112 }
113 
114 // Strip unwanted characters, force to lower case, and handle \ escapes.
115 static string
116 munge_term(const string &term)
117 {
118  string result;
119  for (string::const_iterator i = term.begin(); i != term.end(); ++i) {
120  char ch = *i;
121  if (C_isalnum(ch))
122  result += C_tolower(ch);
123  else if (ch == '\\') {
124  ++i;
125  if (i != term.end()) {
126  switch (*i) {
127  case '\\': ch = '\\'; break;
128  case '0': ch = '\0'; break;
129  case 'n': ch = '\n'; break;
130  case 'r': ch = '\r'; break;
131  case 't': ch = '\t'; break;
132  case 'x': {
133  // Check we can read the next two characters.
134  if (size_t(i - term.begin()) >= term.size() - 2) {
135  --i;
136  break;
137  }
138  string::const_iterator j = i;
139  char b = *++i;
140  char c = *++i;
141  if (!C_isxdigit(b) || !C_isxdigit(c)) {
142  i = j - 1;
143  } else {
144  ch = hex_decode(b, c);
145  }
146  break;
147  }
148  }
149  }
150  result += ch;
151  }
152  }
153  return result;
154 }
155 
156 void
158 {
159  if (input.is_open()) {
160  input.close();
161  // MSVC doesn't clear fail() on close() and re-open().
162  input.clear();
163  }
164 
165  // Find the next non-empty filename.
166  while (file != end && (*file).empty()) {
167  ++file;
168  }
169  if (file == end) return;
170 
171  string filename;
172  if (!datadir.empty()) {
173  filename = datadir;
174  bool need_slash = true;
175  for (char dir_sep : DIR_SEPS_LIST) {
176  if (filename.back() == dir_sep) {
177  need_slash = false;
178  break;
179  }
180  }
181  if (need_slash) filename += '/';
182  }
183  filename += *file++;
184  filename += ".txt";
185 
186  input.open(filename.c_str());
187  // Need to check is_open() - just using operator! fails with MSVC.
188  if (!input.is_open()) {
189  string msg = "Can't read file '";
190  msg += filename;
191  msg += "' for indexing (";
192  errno_to_string(errno, msg);
193  msg += ')';
194  throw msg;
195  }
196 }
void index_to(Xapian::WritableDatabase &db)
Definition: index_utils.cc:51
void next_file()
Definition: index_utils.cc:157
Class representing a document.
Definition: document.h:64
void set_data(std::string_view data)
Set the document data.
Definition: document.cc:81
void add_value(Xapian::valueno slot, std::string_view value)
Add a value to a slot in this document.
Definition: document.cc:191
void add_posting(std::string_view term, Xapian::termpos term_pos, Xapian::termcount wdf_inc=1)
Add a posting for a term.
Definition: document.cc:111
Class representing a stemming algorithm.
Definition: stem.h:74
This class provides read/write access to a database.
Definition: database.h:963
Xapian::docid add_document(const Xapian::Document &doc)
Add a document to the database.
Definition: database.cc:561
#define DIR_SEPS_LIST
Definition: config.h:11
string term
Xapian::termpos pos
void errno_to_string(int e, string &s)
Convert errno value to std::string, thread-safe if possible.
static string get_paragraph(istream &input)
Read a paragraph from stream input.
Definition: index_utils.cc:38
static string munge_term(const string &term)
Definition: index_utils.cc:116
utility functions for indexing testcase data
std::string sortable_serialise(double value)
Convert a floating point number to a string, preserving sort order.
Definition: queryparser.h:1236
unsigned valueno
The number for a value slot in a document.
Definition: types.h:90
unsigned XAPIAN_TERMPOS_BASE_TYPE termpos
A term position within a document or query.
Definition: types.h:75
static Xapian::Stem stemmer
Definition: stemtest.cc:42
Various handy string-related helpers.
bool C_isalnum(char ch)
Definition: stringutils.h:208
char hex_decode(char ch1, char ch2)
Decode a pair of ASCII hex digits.
Definition: stringutils.h:248
bool C_isspace(char ch)
Definition: stringutils.h:213
bool C_isxdigit(char ch)
Definition: stringutils.h:187
bool C_isnotspace(char ch)
Definition: stringutils.h:224
char C_tolower(char ch)
Definition: stringutils.h:226
Definition: header.h:106