nlib
WordFilter.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_SUCCINCT_WORDFILTER_H_
4 #define INCLUDE_NN_NLIB_SUCCINCT_WORDFILTER_H_
5 
6 #include <string.h>
7 
8 #include "nn/nlib/UniquePtr.h"
10 #include "nn/nlib/succinct/Trie.h"
11 
12 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS)
13 #undef NLIB_VIS_PUBLIC
14 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
15 #endif
16 
17 NLIB_NAMESPACE_BEGIN
18 namespace succinct {
19 
20 class WordFilterImpl;
22  public:
23  typedef bool (*MatchCallback)(const char* first, const char* last, uint32_t nodeid,
24  void* user_obj);
27  NLIB_VIS_PUBLIC WordFilter(WordFilter& rhs, move_tag) NLIB_NOEXCEPT; // NOLINT
28  WordFilter& assign(WordFilter& rhs, move_tag) NLIB_NOEXCEPT { // NOLINT
29  WordFilter().swap(*this);
30  this->swap(rhs);
31  return *this;
32  }
33 #ifdef NLIB_CXX11_RVALUE_REFERENCES
34  NLIB_MOVE_MEMBER_HELPER_X_COMMON(WordFilter)
36 #endif
37  void swap(WordFilter& rhs) NLIB_NOEXCEPT { m_Impl.swap(rhs.m_Impl); }
38  NLIB_VIS_PUBLIC bool Init() NLIB_NOEXCEPT;
39  void Match(const char* doc, MatchCallback callback, void* user_obj = NULL) NLIB_NOEXCEPT {
40  this->Match(doc, nlib_strlen(doc), callback, user_obj);
41  }
42  NLIB_VIS_PUBLIC void Match(const char* data, size_t n, MatchCallback callback,
43  void* user_obj = NULL) NLIB_NOEXCEPT;
44  NLIB_VIS_PUBLIC void Reset() NLIB_NOEXCEPT;
45  NLIB_VIS_PUBLIC bool Export(BinaryWriter* w) const NLIB_NOEXCEPT;
46  NLIB_VIS_PUBLIC bool Import(BinaryReader* r) NLIB_NOEXCEPT;
47 
48  private:
49  UniquePtr<WordFilterImpl> m_Impl;
50  friend class WordFilterBuilder;
52 };
53 
54 class WordFilterBuilder NLIB_FINAL {
55  public:
56  WordFilterBuilder() NLIB_NOEXCEPT {}
57  ~WordFilterBuilder() NLIB_NOEXCEPT {}
58  NLIB_VIS_PUBLIC bool Init() NLIB_NOEXCEPT;
59  NLIB_VIS_PUBLIC WordFilter* Build() NLIB_NOEXCEPT;
60  NLIB_VIS_PUBLIC bool AddWord(const char* str) NLIB_NOEXCEPT;
61  NLIB_VIS_PUBLIC bool AddWords(const char* str, size_t len) NLIB_NOEXCEPT;
62  bool AddWords(const char* str) NLIB_NOEXCEPT {
63  return AddWords(str, strlen(str));
64  }
65  NLIB_VIS_PUBLIC bool AddExcludeWord(const char* str) NLIB_NOEXCEPT;
66  NLIB_VIS_PUBLIC bool AddExcludeWords(const char* str, size_t len) NLIB_NOEXCEPT;
67  bool AddExcludeWords(const char* str) NLIB_NOEXCEPT {
68  return AddExcludeWords(str, strlen(str));
69  }
70 
71  private:
72  ReallocCstringVec m_MatchWords;
73  ReallocCstringVec m_ExcludeWords;
74  NLIB_DISALLOW_COPY_AND_ASSIGN(WordFilterBuilder);
75 };
76 
77 } // namespace succinct
78 NLIB_NAMESPACE_END
79 
80 #if defined(_MSC_VER) && defined(nx_succinct_EXPORTS)
81 #undef NLIB_VIS_PUBLIC
82 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
83 #endif
84 
85 #endif // INCLUDE_NN_NLIB_SUCCINCT_WORDFILTER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
Implements Trie using LOUDS.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
void swap(WordFilter &rhs) noexcept
Swaps the contents of an object.
Definition: WordFilter.h:37
The class for realloc-based implementations of C string vectors.
Definition: ReallocVec.h:214
UniquePtr owns the pointer, and when it goes out of scope, the pointer is released by the destructor ...
Definition: UniquePtr.h:96
Defines that class that is corresponding to std::unique_ptr.
Class that checks whether text contains the predefined set of words.
Definition: WordFilter.h:21
An empty structure indicating that an argument to a function needs to be moved.
Definition: Config.h:219
Defines the class for searching text strings using the Aho-Corasick string-matching algorithm...
bool AddExcludeWords(const char *str) noexcept
Adds a string from an array containing a set of excluded terms. The strings must be delimited by newl...
Definition: WordFilter.h:67
size_t nlib_strlen(const char *s)
Internally calls strlen(). In some cases, it may operate as an independent implementation.
Class to create a WordFilter object that corresponds to a term and excluded terms.
Definition: WordFilter.h:54
The class for writing binary to streams (to OutputStream).
Definition: BinaryWriter.h:13
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
The class for reading binary from streams (from InputStream).
Definition: BinaryReader.h:13
WordFilterBuilder() noexcept
Instantiates the object.
Definition: WordFilter.h:56
~WordFilterBuilder() noexcept
Destructor.
Definition: WordFilter.h:57