ModErn Text Analysis
META Enumerates Textual Applications
multi_transformer.h
Go to the documentation of this file.
1 
9 #ifndef META_PARSER_MULTI_TRANSFORMER_H_
10 #define META_PARSER_MULTI_TRANSFORMER_H_
11 
12 #include <array>
14 #include "parser/trees/node.h"
16 #include "parser/trees/leaf_node.h"
17 #include "util/shim.h"
18 
19 namespace meta
20 {
21 namespace parser
22 {
23 
28 template <class... Transformers>
30 {
31  public:
32  static_assert(sizeof...(Transformers) > 1, "multi_transformer needs to "
33  "be specified with at least two "
34  "transformers to be run");
35 
40  multi_transformer() : transforms_{{make_unique<Transformers>()...}}
41  {
42  // nothing
43  }
44 
49  multi_transformer(std::unique_ptr<Transformers>... trans)
50  : transforms_{trans...}
51  {
52  // nothing
53  }
54 
55  std::unique_ptr<node> operator()(const leaf_node& lnode) override
56  {
57  return run_transforms(lnode);
58  }
59 
60  std::unique_ptr<node> operator()(const internal_node& inode) override
61  {
62  return run_transforms(inode);
63  }
64 
65  private:
71  std::unique_ptr<node> run_transforms(const node& n)
72  {
73  auto res = n.accept(*transforms_[0]);
74  for (size_t i = 1; i < transforms_.size(); ++i)
75  res = res->accept(*transforms_[i]);
76  return res;
77  }
78 
82  std::array<std::unique_ptr<tree_transformer>, sizeof...(Transformers)>
83  transforms_;
84 };
85 
93 template <class... Transformers>
94 multi_transformer<Transformers...>
95  make_transformer(std::unique_ptr<Transformers>... trans)
96 {
97  return {trans...};
98 }
99 }
100 }
101 #endif
multi_transformer()
Constructs each of the composed transformers using their default constructor.
Definition: multi_transformer.h:40
A single node in a parse tree for a sentence.
Definition: node.h:24
An internal node in a parse tree.
Definition: internal_node.h:28
multi_transformer< Transformers...> make_transformer(std::unique_ptr< Transformers >...trans)
Helper function for constructing a multi_transformer from a set of pointers to transformers.
Definition: multi_transformer.h:95
A template class for composing tree transformers.
Definition: multi_transformer.h:29
virtual std::unique_ptr< node > operator()(const leaf_node &)=0
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retreival, data mining, and other applications of text processing.
Definition: analyzer.h:24
Abstract base class for tree transformers.
Definition: tree_transformer.h:25
std::remove_reference< Visitor >::type::result_type accept(Visitor &&vtor)
Accepts a visitor.
Definition: node.h:68
A leaf node (pre-terminal) in a parse tree.
Definition: leaf_node.h:24