xapian-core  1.4.21
api_stem.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2010,2012,2019 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 <config.h>
22 
23 #include "api_stem.h"
24 
25 #include <xapian.h>
26 
27 #include "apitest.h"
28 #include "testsuite.h"
29 #include "testutils.h"
30 
31 using namespace std;
32 
34  string operator()(const string & word) {
35  if (word == "vanish")
36  return string();
37  return word.substr(0, 3);
38  }
39 
40  string get_description() const {
41  return "MyStem()";
42  }
43 };
44 
46 DEFINE_TESTCASE(stem1, !backend) {
47  Xapian::Stem st(new MyStemImpl);
48 
49  TEST_EQUAL(st.get_description(), "Xapian::Stem(MyStem())");
50  TEST_EQUAL(st("a"), "a");
51  TEST_EQUAL(st("foo"), "foo");
52  TEST_EQUAL(st("food"), "foo");
53 }
54 
56 DEFINE_TESTCASE(stem2, !backend) {
57  Xapian::Stem st_norwegian("norwegian");
58  TEST_EQUAL(st_norwegian.get_description(),
60  TEST_EQUAL(st_norwegian.get_description(),
62  TEST_EQUAL(st_norwegian.get_description(),
64  TEST_NOT_EQUAL(st_norwegian.get_description(),
66  TEST_NOT_EQUAL(st_norwegian.get_description(),
67  Xapian::Stem("none").get_description());
68 }
69 
71 DEFINE_TESTCASE(stem3, !backend) {
72  Xapian::Stem earlyenglish("earlyenglish");
73  TEST_EQUAL(earlyenglish("loved"), "love");
74  TEST_EQUAL(earlyenglish("loving"), "love");
75  TEST_EQUAL(earlyenglish("loveth"), "love");
76  TEST_EQUAL(earlyenglish("givest"), "give");
77 }
78 
80 // Regression test for https://trac.xapian.org/ticket/741 fixed in 1.4.2.
81 DEFINE_TESTCASE(stemempty1, !backend) {
82  Xapian::Stem st(new MyStemImpl);
83 
85  Xapian::Document doc;
86  tg.set_document(doc);
87  tg.set_stemmer(st);
89 
90  tg.index_text("watch me vanish now");
91  auto i = doc.termlist_begin();
92  TEST(i != doc.termlist_end());
93  TEST_EQUAL(*i, "me");
94  TEST(++i != doc.termlist_end());
95  TEST_EQUAL(*i, "now");
96  TEST(++i != doc.termlist_end());
97  TEST_EQUAL(*i, "wat");
98  TEST(++i == doc.termlist_end());
99 }
100 
102 DEFINE_TESTCASE(stemlangs2, !backend) {
103  string lang("xdummy");
104  for (unsigned ch = 0; ch <= 255; ++ch) {
105  lang[0] = char(ch);
107  }
108 
109  // Test fallback=false throws too.
111 
112  // Test fallback=true gives "none" stemmer.
113  Xapian::Stem stem(lang, true);
114  TEST(stem.is_none());
115  TEST_EQUAL(stem.get_description(), "Xapian::Stem(none)");
116 }
bool is_none() const
Return true if this is a no-op stemmer.
Definition: stem.h:166
#define TEST(a)
Test a condition, without an additional explanation for failure.
Definition: testsuite.h:275
string get_description() const
Return a string describing this object.
Definition: api_stem.cc:40
Class representing a stemming algorithm.
Definition: stem.h:62
void set_document(const Xapian::Document &doc)
Set the current document.
Parses a piece of text and generate terms.
Definition: termgenerator.h:48
void set_stemming_strategy(stem_strategy strategy)
Set the stemming strategy.
a generic test suite engine
STL namespace.
TermIterator termlist_end() const
Equivalent end iterator for termlist_begin().
Definition: document.h:260
string operator()(const string &word)
Stem the specified word.
Definition: api_stem.cc:34
void index_text(const Xapian::Utf8Iterator &itor, Xapian::termcount wdf_inc=1, const std::string &prefix=std::string())
Index some text.
test functionality of the Xapian API
#define TEST_NOT_EQUAL(a, b)
Test for non-equality of two things.
Definition: testsuite.h:305
DEFINE_TESTCASE(stem1, !backend)
Test user stemming algorithms.
Definition: api_stem.cc:46
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:241
std::string get_description() const
Return a string describing this object.
Definition: stem.cc:147
Public interfaces for the Xapian library.
#define TEST_EXCEPTION(TYPE, CODE)
Check that CODE throws exactly Xapian exception TYPE.
Definition: testutils.h:109
void set_stemmer(const Xapian::Stem &stemmer)
Set the Xapian::Stem object to be used for generating stemmed terms.
Class representing a stemming algorithm implementation.
Definition: stem.h:38
Xapian-specific test helper functions and macros.
#define TEST_EQUAL(a, b)
Test for equality of two things.
Definition: testsuite.h:278
TermIterator termlist_begin() const
Iterator for the terms in this document.
Definition: omdocument.cc:197
A handle representing a document in a Xapian database.
Definition: document.h:61