Turi Create  4.0
word_trimmer.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef _WORD_TRIMMER_H_
7 #define _WORD_TRIMMER_H_
8 #include <string>
9 #include <model_server/lib/toolkit_class_macros.hpp>
10 #include <toolkits/feature_engineering/transformer_base.hpp>
11 #include <toolkits/feature_engineering/topk_indexer.hpp>
12 #include <core/export.hpp>
13 
14 namespace turi {
15 namespace sdk_model {
16 namespace feature_engineering {
17 
18 class EXPORT word_trimmer : public transformer_base {
19 
20  static constexpr size_t WORD_TRIMMER_VERSION = 0;
21  std::map<std::string, std::shared_ptr<topk_indexer>> index_map;
22  bool exclude = false;
23  std::map<std::string, flex_type_enum> feature_types;
24  flexible_type stopwords;
25  flexible_type feature_columns; // Input provided by the user.
26  flexible_type delimiters;
27  bool to_lower;
28 
29  public:
30 
31  /**
32  * Methods that must be implemented in a new transformer model.
33  * -------------------------------------------------------------------------
34  */
35 
36  virtual inline ~word_trimmer() {}
37 
38  /**
39  * Set one of the options in the model. Use the option manager to set
40  * these options. If the option does not satisfy the conditions that the
41  * option manager has imposed on it. Errors will be thrown.
42  *
43  * \param[in] options Options to set
44  */
45  void init_options(const std::map<std::string, flexible_type>&_options) override;
46 
47  /**
48  * Get a version for the object.
49  */
50  size_t get_version() const override;
51 
52  /**
53  * Save the object using Turi's oarc.
54  */
55  void save_impl(turi::oarchive& oarc) const override;
56 
57  /**
58  * Load the object using Turi's iarc.
59  */
60  void load_version(turi::iarchive& iarc, size_t version) override;
61 
62 
63  /**
64  * Initialize the transformer.
65  */
66  void init_transformer(const std::map<std::string,
67  flexible_type>& _options) override;
68 
69  /**
70  * Set constant.
71  *
72  * \param[in] data (SFrame of data)
73  */
74  void fit(gl_sframe data) override;
75 
76  /**
77  * Transform the given data.
78  *
79  * \param[in] data (SFrame of data)
80  *
81  * Python side interface
82  * ------------------------
83  * This function directly interfaces with "transform" in python.
84  *
85  */
86  gl_sframe transform(gl_sframe data) override;
87 
88 
89  /**
90  * Fit and transform the given data. Intended as an optimization because
91  * fit and transform are usually always called together. The default
92  * implementaiton calls fit and then transform.
93  *
94  * \param[in] data (SFrame of data)
95  */
96  gl_sframe fit_transform(gl_sframe data) {
97  data.materialize();
98  fit(data);
99  return transform(data);
100  }
101 
102 
103  // Functions that all transformers need to register. Can be copied verbatim
104  // for other classes.
105  // --------------------------------------------------------------------------
106  BEGIN_CLASS_MEMBER_REGISTRATION("_RareWordTrimmer")
107  REGISTER_CLASS_MEMBER_FUNCTION(word_trimmer::init_transformer, "_options");
108  REGISTER_CLASS_MEMBER_FUNCTION(word_trimmer::fit, "data");
109  REGISTER_CLASS_MEMBER_FUNCTION(word_trimmer::fit_transform, "data");
110  REGISTER_CLASS_MEMBER_FUNCTION(word_trimmer::transform, "data");
111  REGISTER_CLASS_MEMBER_FUNCTION(word_trimmer::get_current_options);
112  REGISTER_CLASS_MEMBER_FUNCTION(word_trimmer::list_fields);
113  REGISTER_NAMED_CLASS_MEMBER_FUNCTION("_get_default_options",
114  word_trimmer::get_default_options);
116  word_trimmer::get_value_from_state,
117  "key");
119 
120 };
121 
122 
123 } // feature_engineering
124 } // sdk_model
125 } // turicreate
126 #endif
#define BEGIN_CLASS_MEMBER_REGISTRATION(python_facing_classname)
#define REGISTER_CLASS_MEMBER_FUNCTION(function,...)
The serialization input archive object which, provided with a reference to an istream, will read from the istream, providing deserialization capabilities.
Definition: iarchive.hpp:60
#define END_CLASS_MEMBER_REGISTRATION
#define REGISTER_NAMED_CLASS_MEMBER_FUNCTION(name, function,...)
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
Definition: oarchive.hpp:80
void transform(S &&input, T &&output, TransformFn transformfn, std::set< size_t > constraint_segments=std::set< size_t >())
Definition: algorithm.hpp:64