From d3723f8796812e8637e3b5312a02ede0b0f748a8 Mon Sep 17 00:00:00 2001 From: Adam Wardynski <award@.(win7-laptop)> Date: Sat, 4 Dec 2010 14:17:47 +0100 Subject: [PATCH] Add operator<< to dump raw expression to a stream. --- libwccl/CMakeLists.txt | 2 + libwccl/ops/expression.h | 31 +++++- libwccl/ops/functions/bool/iteration.cpp | 15 ++- libwccl/ops/functions/bool/iteration.h | 19 ++-- .../ops/functions/bool/iterations/atleast.cpp | 16 ++-- .../ops/functions/bool/iterations/atleast.h | 20 ++-- .../ops/functions/bool/predicates/equals.h | 58 ++++++++---- .../functions/bool/predicates/intersects.h | 19 ++-- .../functions/bool/predicates/isinside.cpp | 22 +++++ .../ops/functions/bool/predicates/isinside.h | 30 +++--- .../functions/bool/predicates/isoutside.cpp | 22 +++++ .../ops/functions/bool/predicates/isoutside.h | 30 +++--- .../functions/bool/predicates/issubsetof.h | 26 +++-- .../bool/predicates/logicalpredicate.cpp | 15 ++- .../bool/predicates/logicalpredicate.h | 20 ++-- .../ops/functions/bool/predicates/regex.cpp | 14 +-- libwccl/ops/functions/bool/predicates/regex.h | 17 ++-- .../functions/bool/predicates/setpredicate.h | 37 +++++--- libwccl/ops/functions/bool/varsetter.h | 66 ++++++++----- libwccl/ops/functions/conditional.h | 94 +++++++++---------- libwccl/ops/functions/constant.h | 41 +++++--- .../functions/position/relativeposition.cpp | 12 +-- .../ops/functions/position/relativeposition.h | 12 ++- libwccl/ops/functions/strset/affix.cpp | 9 +- libwccl/ops/functions/strset/affix.h | 20 ++-- libwccl/ops/functions/strset/getlemmas.cpp | 5 +- libwccl/ops/functions/strset/getlemmas.h | 20 ++-- libwccl/ops/functions/strset/getorth.cpp | 5 +- libwccl/ops/functions/strset/getorth.h | 20 ++-- libwccl/ops/functions/strset/tolower.cpp | 8 +- libwccl/ops/functions/strset/tolower.h | 20 ++-- libwccl/ops/functions/strset/toupper.cpp | 8 +- libwccl/ops/functions/strset/toupper.h | 17 ++-- libwccl/ops/functions/tset/getsymbols.cpp | 5 +- libwccl/ops/functions/tset/getsymbols.h | 21 +++-- .../ops/functions/tset/getsymbolsinrange.cpp | 14 ++- .../ops/functions/tset/getsymbolsinrange.h | 20 ++-- libwccl/ops/functions/vargetter.h | 39 +++++--- libwccl/ops/operator.h | 9 +- 39 files changed, 519 insertions(+), 359 deletions(-) create mode 100644 libwccl/ops/functions/bool/predicates/isinside.cpp create mode 100644 libwccl/ops/functions/bool/predicates/isoutside.cpp diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index ed0cd66..c501eb5 100644 --- a/libwccl/CMakeLists.txt +++ b/libwccl/CMakeLists.txt @@ -35,6 +35,8 @@ SET(libwccl_STAT_SRC ops/functions/bool/iterations/rightlook.cpp ops/functions/bool/predicate.cpp ops/functions/bool/predicates/and.cpp + ops/functions/bool/predicates/isinside.cpp + ops/functions/bool/predicates/isoutside.cpp ops/functions/bool/predicates/logicalpredicate.cpp ops/functions/bool/predicates/nor.cpp ops/functions/bool/predicates/or.cpp diff --git a/libwccl/ops/expression.h b/libwccl/ops/expression.h index a49cb1e..4c3b045 100644 --- a/libwccl/ops/expression.h +++ b/libwccl/ops/expression.h @@ -3,6 +3,7 @@ #include <libcorpus2/tagset.h> #include <boost/noncopyable.hpp> +#include <sstream> namespace Wccl { @@ -21,7 +22,15 @@ public: * require a tagset. * @note Might be incomplete and/or contain internal info. */ - virtual std::string to_raw_string() const = 0; + std::string to_raw_string() const; + + /** + * Writes the raw form of the expression directly to a stream. + * @note The written form is raw, may be incomplete and/or contain + * internal info + * @returns As usual, the stream written to, to allow chaining. + */ + friend std::ostream& operator<<(std::ostream& ostream, const Expression& expression); /** * Virtual destructor, so if delete is ever called on a base-class-type @@ -29,8 +38,28 @@ public: * in a derived class, if a special destructor was needed for that class. */ virtual ~Expression() {}; + +protected: + virtual std::ostream& write_to(std::ostream& ostream) const = 0; }; + +// +// ----- Implementation ----- +// + +inline +std::ostream& operator <<(std::ostream& ostream, const Expression& expression) { + return expression.write_to(ostream); +} + +inline +std::string Expression::to_raw_string() const { + std::ostringstream ss; + ss << *this; + return ss.str(); +} + } /* end ns Wccl */ #endif // LIBWCCL_OPS_EXPRESSION_H diff --git a/libwccl/ops/functions/bool/iteration.cpp b/libwccl/ops/functions/bool/iteration.cpp index 33b267e..e9be9e8 100644 --- a/libwccl/ops/functions/bool/iteration.cpp +++ b/libwccl/ops/functions/bool/iteration.cpp @@ -1,6 +1,5 @@ #include <libwccl/ops/functions/bool/iteration.h> #include <libwccl/ops/functions/bool/predicate.h> -#include <sstream> namespace Wccl { @@ -15,15 +14,13 @@ std::string Iteration::to_string(const Corpus2::Tagset& tagset) const return ss.str(); } -std::string Iteration::to_raw_string() const +std::ostream& Iteration::write_to(std::ostream& os) const { - std::ostringstream ss; - ss << raw_name() << "(" - << left_pos_expr_->to_raw_string() << ", " - << right_pos_expr_->to_raw_string() << ", " - << Position::var_repr(iter_var_acc_.get_name()) << ", " - << evaluating_expr_->to_raw_string() << ")"; - return ss.str(); + return os << raw_name() << "(" + << *left_pos_expr_ << ", " + << *right_pos_expr_ << ", " + << Position::var_repr(iter_var_acc_.get_name()) << ", " + << *evaluating_expr_ << ")"; } Iteration::BaseRetValPtr Iteration::apply_internal(const FunExecContext& context) const diff --git a/libwccl/ops/functions/bool/iteration.h b/libwccl/ops/functions/bool/iteration.h index f5f9290..64197d4 100644 --- a/libwccl/ops/functions/bool/iteration.h +++ b/libwccl/ops/functions/bool/iteration.h @@ -23,15 +23,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String reperesentation of Iteration Operator. This is - * default representation of - * raw_name(raw_left_pos_expr, raw_right_pos_expr, variable, raw_eval_expr) - * @note This version doesn't require a Tagset, but may - * be incomplete and/or contain internal info. - */ - std::string to_raw_string() const; - protected: const PosFunctionPtr& left_pos_expr_; const PosFunctionPtr& right_pos_expr_; @@ -78,6 +69,16 @@ protected: */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + /** + * Writes raw string reperesentation of Iteration Operator. This is + * default representation of + * raw_name(raw_left_pos_expr, raw_right_pos_expr, variable, raw_eval_expr) + * @note This version doesn't require a Tagset, but may + * be incomplete and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; + virtual bool iterate( int left, int right, diff --git a/libwccl/ops/functions/bool/iterations/atleast.cpp b/libwccl/ops/functions/bool/iterations/atleast.cpp index 6713682..0cadc0e 100644 --- a/libwccl/ops/functions/bool/iterations/atleast.cpp +++ b/libwccl/ops/functions/bool/iterations/atleast.cpp @@ -33,16 +33,14 @@ std::string AtLeast::to_string(const Corpus2::Tagset& tagset) const return ss.str(); } -std::string AtLeast::to_raw_string() const +std::ostream& AtLeast::write_to(std::ostream& os) const { - std::ostringstream ss; - ss << raw_name() << "(" - << left_pos_expr_->to_raw_string() << ", " - << right_pos_expr_->to_raw_string() << ", " - << Position::var_repr(iter_var_acc_.get_name()) << ", " - << evaluating_expr_->to_raw_string() << ", " - << min_matches_ << ")"; - return ss.str(); + return os << raw_name() << "(" + << *left_pos_expr_ << ", " + << *right_pos_expr_ << ", " + << Position::var_repr(iter_var_acc_.get_name()) << ", " + << *evaluating_expr_ << ")" + << min_matches_ << ")"; } } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/iterations/atleast.h b/libwccl/ops/functions/bool/iterations/atleast.h index 177a591..21d5536 100644 --- a/libwccl/ops/functions/bool/iterations/atleast.h +++ b/libwccl/ops/functions/bool/iterations/atleast.h @@ -38,15 +38,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String reperesentation of AtLeast Operator. This is - * default representation of - * atleast(raw_left_p_expr, raw_right_p_expr, var, raw_eval_expr, min_matches) - * @note This version doesn't require a Tagset, but may - * be incomplete and/or contain internal info. - */ - std::string to_raw_string() const; - protected: /** * @returns True, if for at least min_matches positions @@ -61,6 +52,17 @@ protected: int right, Position &p, const FunExecContext &context) const; + + /** + * Writes raw string reperesentation of Iteration Operator. This is + * default representation of + * atleast(raw_left_p_expr, raw_right_p_expr, var, raw_eval_expr, min_matches) + * @note This version doesn't require a Tagset, but may + * be incomplete and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; + private: int min_matches_; }; diff --git a/libwccl/ops/functions/bool/predicates/equals.h b/libwccl/ops/functions/bool/predicates/equals.h index c7294cc..acac3f3 100644 --- a/libwccl/ops/functions/bool/predicates/equals.h +++ b/libwccl/ops/functions/bool/predicates/equals.h @@ -48,7 +48,8 @@ public: * Predicate that checks for equality of values */ template <class T> -class Equals : public Predicate { +class Equals : public Predicate +{ public: typedef boost::shared_ptr<Function<T> > ArgFunctionPtr; @@ -63,19 +64,7 @@ public: * @returns String representation of the function in form of: * "equals(arg1_expr_string, arg2_expr_string)" */ - std::string to_string(const Corpus2::Tagset& tagset) const { - return BinaryFunctionFormatter::to_string(tagset, *this, *arg1_expr_, *arg2_expr_); - } - - /** - * @returns String representation of the function in form of: - * "equals(arg1_expr_raw_string, arg2_expr_raw_string)". - * @note This version does not require tagset but may be incomplete - * and/or contain internal info. - */ - std::string to_raw_string() const { - return BinaryFunctionFormatter::to_raw_string(*this, *arg1_expr_, *arg2_expr_); - } + std::string to_string(const Corpus2::Tagset& tagset) const; /** * @returns Name of the function: "equals". @@ -92,14 +81,43 @@ protected: * Take values of arguments from expressions and return True if they are equal, * False otherwise. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - return EqualityComparer<T>::apply( - *(this->arg1_expr_->apply(context)), - *(this->arg2_expr_->apply(context)), - context); - } + BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in form of: + * "equals(arg1_expr_raw_string, arg2_expr_raw_string)". + * @note This version does not require tagset but may be incomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; + +// +// ----- Implementation ----- +// + +template <class T> +FunctionBase::BaseRetValPtr Equals<T>::apply_internal(const FunExecContext& context) const { + return EqualityComparer<T>::apply( + *(this->arg1_expr_->apply(context)), + *(this->arg2_expr_->apply(context)), + context); +} + +template <class T> +std::string Equals<T>::to_string(const Corpus2::Tagset &tagset) const { + return BinaryFunctionFormatter::to_string(tagset, *this, *arg1_expr_, *arg2_expr_); +} + +template <class T> +std::ostream& Equals<T>::write_to(std::ostream &os) const { + return os << this->raw_name() << "(" + << *this->arg1_expr_ << ", " + << *this->arg2_expr_ << ")"; +} + } /* end ns Wccl */ #endif // LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_EQUALS_H diff --git a/libwccl/ops/functions/bool/predicates/intersects.h b/libwccl/ops/functions/bool/predicates/intersects.h index ab709a5..92c88c0 100644 --- a/libwccl/ops/functions/bool/predicates/intersects.h +++ b/libwccl/ops/functions/bool/predicates/intersects.h @@ -27,19 +27,26 @@ public: } protected: - typedef FunctionBase::BaseRetValPtr BaseRetValPtr; /** * Take values for both sets and return True if they intersect, * False otherwise. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - const boost::shared_ptr<const T>& set1 = this->set1_expr_->apply(context); - const boost::shared_ptr<const T>& set2 = this->set2_expr_->apply(context); - return Predicate::evaluate(set1->intersects(*set2), context); - } + FunctionBase::BaseRetValPtr apply_internal(const FunExecContext& context) const; }; + +// +// ----- Implementation ----- +// + +template <class T> +FunctionBase::BaseRetValPtr Intersects<T>::apply_internal(const FunExecContext& context) const { + const boost::shared_ptr<const T>& set1 = this->set1_expr_->apply(context); + const boost::shared_ptr<const T>& set2 = this->set2_expr_->apply(context); + return Predicate::evaluate(set1->intersects(*set2), context); +} + } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/isinside.cpp b/libwccl/ops/functions/bool/predicates/isinside.cpp new file mode 100644 index 0000000..a525d0b --- /dev/null +++ b/libwccl/ops/functions/bool/predicates/isinside.cpp @@ -0,0 +1,22 @@ +#include <libwccl/ops/functions/bool/predicates/isinside.h> +#include <libwccl/ops/formatters.h> + +namespace Wccl { + +IsInside::BaseRetValPtr IsInside::apply_internal(const FunExecContext& context) const +{ + const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); + return Predicate::evaluate(pos->is_inside(context.sentence_context()), context); +} + +std::string IsInside::to_string(const Corpus2::Tagset& tagset) const +{ + return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_); +} + +std::ostream& IsInside::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" << *pos_expr_ << ")"; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/isinside.h b/libwccl/ops/functions/bool/predicates/isinside.h index 2894915..8971db5 100644 --- a/libwccl/ops/functions/bool/predicates/isinside.h +++ b/libwccl/ops/functions/bool/predicates/isinside.h @@ -2,7 +2,6 @@ #define LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_ISINSIDE_H #include <libwccl/ops/functions/bool/predicate.h> -#include <libwccl/ops/formatters.h> #include <libwccl/values/position.h> namespace Wccl { @@ -10,7 +9,8 @@ namespace Wccl { /** * Predicate that checks if a position is within sentence boundaries */ -class IsInside : public Predicate { +class IsInside : public Predicate +{ public: typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; @@ -24,17 +24,7 @@ public: * @returns String representation of the function in the form of: * "inside(arg_expr_string)" */ - std::string to_string(const Corpus2::Tagset& tagset) const { - return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_); - } - - /** - * @returns String representation of the function in the form of: - * "inside(arg_expr_raw_string)" - */ - std::string to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *pos_expr_); - } + std::string to_string(const Corpus2::Tagset& tagset) const; /** * @returns Name of the function: "inside" @@ -51,10 +41,16 @@ protected: * boundaries. * @returns True value if position is within sentence boundaries, False otherwise. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); - return Predicate::evaluate(pos->is_inside(context.sentence_context()), context); - } + BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "inside(arg_expr_raw_string)" + * @note This version doesn't require a tagset, but may be incomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& os) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/isoutside.cpp b/libwccl/ops/functions/bool/predicates/isoutside.cpp new file mode 100644 index 0000000..d6228a6 --- /dev/null +++ b/libwccl/ops/functions/bool/predicates/isoutside.cpp @@ -0,0 +1,22 @@ +#include <libwccl/ops/functions/bool/predicates/isoutside.h> +#include <libwccl/ops/formatters.h> + +namespace Wccl { + +IsOutside::BaseRetValPtr IsOutside::apply_internal(const FunExecContext& context) const +{ + const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); + return Predicate::evaluate(pos->is_outside(context.sentence_context()), context); +} + +std::string IsOutside::to_string(const Corpus2::Tagset& tagset) const +{ + return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_); +} + +std::ostream& IsOutside::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" << *pos_expr_ << ")"; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/isoutside.h b/libwccl/ops/functions/bool/predicates/isoutside.h index d9019c4..96d78c8 100644 --- a/libwccl/ops/functions/bool/predicates/isoutside.h +++ b/libwccl/ops/functions/bool/predicates/isoutside.h @@ -2,7 +2,6 @@ #define LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_ISOUTSIDE_H #include <libwccl/ops/functions/bool/predicate.h> -#include <libwccl/ops/formatters.h> #include <libwccl/values/position.h> namespace Wccl { @@ -10,7 +9,8 @@ namespace Wccl { /** * Predicate that checks if a position is outside of the sentence boundaries */ -class IsOutside : public Predicate { +class IsOutside : public Predicate +{ public: typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; @@ -24,17 +24,7 @@ public: * @returns String representation of the function in the form of: * "outside(arg_expr_string)" */ - std::string to_string(const Corpus2::Tagset& tagset) const { - return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_); - } - - /** - * @returns String representation of the function in the form of: - * "outside(arg_expr_raw_string)" - */ - std::string to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *pos_expr_); - } + std::string to_string(const Corpus2::Tagset& tagset) const; /** * @returns Name of the function: "outside" @@ -51,10 +41,16 @@ protected: * sentence boundaries, in the given context (i.e. relative to current position) * @returns True value if position is outside of the sentence boundaries, False otherwise. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); - return Predicate::evaluate(pos->is_outside(context.sentence_context()), context); - } + BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "outside(arg_expr_raw_string)" + * @note This version doesn't require a tagset, but may be incomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& os) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/issubsetof.h b/libwccl/ops/functions/bool/predicates/issubsetof.h index 3cf3e5f..0822aab 100644 --- a/libwccl/ops/functions/bool/predicates/issubsetof.h +++ b/libwccl/ops/functions/bool/predicates/issubsetof.h @@ -28,7 +28,6 @@ public: } protected: - typedef FunctionBase::BaseRetValPtr BaseRetValPtr; /** * Take value of possible subset in question. If it is an empty set, return False. * Otherwise, take value of the set that is being compared to. @@ -36,18 +35,25 @@ protected: * @note Take note this is not true "subset" function, because empty * set is considered not to be "in" any other set so False is returned. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - const boost::shared_ptr<const T>& possible_subset = this->set1_expr_->apply(context); - if(!possible_subset->empty()) - { - const boost::shared_ptr<const T>& set_compared_to = this->set2_expr_->apply(context); - return Predicate::evaluate(possible_subset->is_subset_of(*set_compared_to), context); - } - return Predicate::False(context); - } + FunctionBase::BaseRetValPtr apply_internal(const FunExecContext& context) const; }; +// +// ----- Implementation ----- +// + +template <class T> +FunctionBase::BaseRetValPtr IsSubsetOf<T>::apply_internal(const FunExecContext& context) const { + const boost::shared_ptr<const T>& possible_subset = this->set1_expr_->apply(context); + if(!possible_subset->empty()) + { + const boost::shared_ptr<const T>& set_compared_to = this->set2_expr_->apply(context); + return Predicate::evaluate(possible_subset->is_subset_of(*set_compared_to), context); + } + return Predicate::False(context); +} + } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/logicalpredicate.cpp b/libwccl/ops/functions/bool/predicates/logicalpredicate.cpp index 40db27e..db7d743 100644 --- a/libwccl/ops/functions/bool/predicates/logicalpredicate.cpp +++ b/libwccl/ops/functions/bool/predicates/logicalpredicate.cpp @@ -1,7 +1,5 @@ #include <libwccl/ops/functions/bool/predicates/logicalpredicate.h> -#include <sstream> - namespace Wccl { std::string LogicalPredicate::to_string(const Corpus2::Tagset& tagset) const @@ -19,19 +17,18 @@ std::string LogicalPredicate::to_string(const Corpus2::Tagset& tagset) const return ss.str(); } -std::string LogicalPredicate::to_raw_string() const +std::ostream& LogicalPredicate::write_to(std::ostream& ostream) const { - std::stringstream ss; - ss << raw_name() << "("; + ostream << raw_name() << "("; BoolFunctionPtrVector::const_iterator it = expressions_->begin(); while(it != expressions_->end()) { - ss << (*it)->to_raw_string(); + ostream << (*it)->to_raw_string(); if(++it != expressions_->end()) { - ss << ", "; + ostream << ", "; } } - ss << ")"; - return ss.str(); + ostream << ")"; + return ostream; } } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/logicalpredicate.h b/libwccl/ops/functions/bool/predicates/logicalpredicate.h index e03edb0..7587b11 100644 --- a/libwccl/ops/functions/bool/predicates/logicalpredicate.h +++ b/libwccl/ops/functions/bool/predicates/logicalpredicate.h @@ -10,7 +10,8 @@ namespace Wccl { * Abstract base class for predicates which are logical functions * (over any number of arguments) */ -class LogicalPredicate : public Predicate { +class LogicalPredicate : public Predicate +{ public: typedef boost::shared_ptr<Function<Bool> > BoolFunctionPtr; typedef std::vector<BoolFunctionPtr> BoolFunctionPtrVector; @@ -23,21 +24,22 @@ public: } /** - * String representation of the logical predicate, realised by default + * @returns String representation of the logical predicate, realised by default * as "operator_name(expr1_string, ..., exprn_string)" */ std::string to_string(const Corpus2::Tagset& tagset) const; +protected: + const boost::shared_ptr<BoolFunctionPtrVector> expressions_; + /** - * String representation of the logical predicate, realised by default + * Writes raw string representation of the logical predicate, realised by default * as "raw_operator_name(raw_expr1_string, ..., raw_exprn_string)" - * (this version doesn't require tagset, but may be incomplete and/or - * contain internal info) + * @note This version doesn't require tagset, but may be incomplete and/or + * contain internal info. + * @returns Stream written to. */ - std::string to_raw_string() const; - -protected: - const boost::shared_ptr<BoolFunctionPtrVector> expressions_; + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/predicates/regex.cpp b/libwccl/ops/functions/bool/predicates/regex.cpp index 93cfaf9..6991833 100644 --- a/libwccl/ops/functions/bool/predicates/regex.cpp +++ b/libwccl/ops/functions/bool/predicates/regex.cpp @@ -1,7 +1,6 @@ #include <libwccl/ops/functions/bool/predicates/regex.h> #include <libpwrutils/foreach.h> -#include <sstream> #include <libpwrutils/util.h> namespace Wccl { @@ -62,14 +61,15 @@ std::string Regex::to_string(const Corpus2::Tagset& tagset) const return ss.str(); } -std::string Regex::to_raw_string() const { - std::stringstream ss; - ss << raw_name() << "(" << strset_expr_->to_raw_string() - << ", \"" << PwrNlp::to_utf8(patstr_) << "\")"; //TODO: utf escaping? - return ss.str(); +std::ostream& Regex::write_to(std::ostream& os) const +{ + //TODO: utf escaping? + return os << raw_name() << "(" + << *strset_expr_ << ", \"" << PwrNlp::to_utf8(patstr_) << "\")"; } -Regex::BaseRetValPtr Regex::apply_internal(const FunExecContext& context) const { +Regex::BaseRetValPtr Regex::apply_internal(const FunExecContext& context) const +{ const boost::shared_ptr<const StrSet>& set = strset_expr_->apply(context); if(set->empty()) { return Predicate::False(context); diff --git a/libwccl/ops/functions/bool/predicates/regex.h b/libwccl/ops/functions/bool/predicates/regex.h index 367eb96..e32721e 100644 --- a/libwccl/ops/functions/bool/predicates/regex.h +++ b/libwccl/ops/functions/bool/predicates/regex.h @@ -26,14 +26,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in form of: - * "regex(strset_expr_raw_string, regex_string)" - * @note This version does not require a tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "regex" */ @@ -50,6 +42,15 @@ protected: */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + /** + * Writes raw string representation of the function in form of: + * "regex(strset_expr_raw_string, regex_string)" + * @note This version doesn't require a tagset, but may be incomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& os) const; + private: const StrSetFunctionPtr strset_expr_; const UnicodeString patstr_; diff --git a/libwccl/ops/functions/bool/predicates/setpredicate.h b/libwccl/ops/functions/bool/predicates/setpredicate.h index d845c47..863cac2 100644 --- a/libwccl/ops/functions/bool/predicates/setpredicate.h +++ b/libwccl/ops/functions/bool/predicates/setpredicate.h @@ -31,25 +31,38 @@ public: * @returns String representation of the function in form of: * "name_string(arg1_expr_string, arg2_expr_string)" */ - std::string to_string(const Corpus2::Tagset& tagset) const { - return BinaryFunctionFormatter::to_string(tagset, *this, *set1_expr_, *set2_expr_); - } + std::string to_string(const Corpus2::Tagset& tagset) const; + +protected: + const SetFunctionPtr set1_expr_; + const SetFunctionPtr set2_expr_; /** - * @returns String representation of the function in form of: + * Writes raw string representation of the function in form of: * "raw_name_string(arg1_expr_raw_string, arg2_expr_raw_string)" * @note This version does not require a tagset, but may be inclomplete * and/or contain internal info. - */ - std::string to_raw_string() const { - return BinaryFunctionFormatter::to_raw_string(*this, *set1_expr_, *set2_expr_); - } - -protected: - const SetFunctionPtr set1_expr_; - const SetFunctionPtr set2_expr_; + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; +// +// ----- Implementation ----- +// + +template <class T> +std::string SetPredicate<T>::to_string(const Corpus2::Tagset &tagset) const { + return BinaryFunctionFormatter::to_string(tagset, *this, *set1_expr_, *set2_expr_); +} + +template <class T> +std::ostream& SetPredicate<T>::write_to(std::ostream &os) const { + return os << this->raw_name() << "(" + << *this->set1_expr_ << ", " + << *this->set2_expr_ << ")"; +} + } /* end ns Wccl */ #endif // LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_SETPREDICATE_H diff --git a/libwccl/ops/functions/bool/varsetter.h b/libwccl/ops/functions/bool/varsetter.h index cd68211..2d74ae9 100644 --- a/libwccl/ops/functions/bool/varsetter.h +++ b/libwccl/ops/functions/bool/varsetter.h @@ -11,7 +11,8 @@ namespace Wccl { * and returns True. */ template<class T> -class VarSetter : public Function<Bool> { +class VarSetter : public Function<Bool> +{ public: typedef typename boost::shared_ptr<Function<T> > ArgFunctionPtr; @@ -29,40 +30,27 @@ public: return "setvar"; } - /** - * @returns String representation of the variable setter which is - * setvar(var_repr, arg_expr_str) - * @note This version does not require a tagset, but may be incomplete - * and/or contain internal info. - */ - std::string to_raw_string() const { - return BinaryFunctionFormatter::to_raw_string( - *this, - T::var_repr(var_acc_.get_name()), - *arg_expr_); - } - /** * @returns String representation of the variable setter which is * setvar(var_repr, arg_raw_expr_str) */ - std::string to_string(const Corpus2::Tagset& tagset) const { - return BinaryFunctionFormatter::to_string( - tagset, - *this, - T::var_repr(var_acc_.get_name()), - *arg_expr_); - } + std::string to_string(const Corpus2::Tagset& tagset) const; + protected: /** * Evaluate argument expression and assign the result to underlying variable. * @returns True. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - context.variables()->get_fast(var_acc_)->set_value( - arg_expr_->apply(context)->get_value()); - return Predicate::True(context); - } + BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the variable setter which is + * setvar(var_repr, arg_expr_str) + * @note This version does not require a tagset, but may be incomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; private: const VariableAccessor<T> var_acc_; @@ -70,6 +58,32 @@ private: }; +// +// ----- Implementation ----- +// + +template <class T> +FunctionBase::BaseRetValPtr VarSetter<T>::apply_internal(const Wccl::FunExecContext &context) const { + context.variables()->get_fast(var_acc_)->set_value(arg_expr_->apply(context)->get_value()); + return Predicate::True(context); +} + +template <class T> +std::string VarSetter<T>::to_string(const Corpus2::Tagset &tagset) const { + return BinaryFunctionFormatter::to_string( + tagset, + *this, + T::var_repr(var_acc_.get_name()), + *arg_expr_); +} + +template <class T> +std::ostream& VarSetter<T>::write_to(std::ostream &os) const { + return os << this->raw_name() << "(" + << T::var_repr(var_acc_.get_name()) << ", " + << *this->arg_expr_ << ")"; +} + } /* end ns Wccl */ #endif // LIBWCCL_OPS_FUNCTIONS_BOOL_VARSETTER_H diff --git a/libwccl/ops/functions/conditional.h b/libwccl/ops/functions/conditional.h index af07a74..ef040ea 100644 --- a/libwccl/ops/functions/conditional.h +++ b/libwccl/ops/functions/conditional.h @@ -1,11 +1,8 @@ #ifndef LIBWCCL_OPS_FUNCTIONS_CONDITIONAL_H #define LIBWCCL_OPS_FUNCTIONS_CONDITIONAL_H -#include <sstream> #include <boost/format.hpp> - #include <libwccl/ops/functions/constant.h> -#include <libwccl/ops/formatters.h> namespace Wccl { @@ -15,7 +12,8 @@ namespace Wccl { * This class is targeted towards if..then..else expression */ template<class T> -class Conditional : public Function<T> { +class Conditional : public Function<T> +{ public: typedef boost::shared_ptr<const Function<T> > ArgFunctionPtr; typedef boost::shared_ptr<const Function<Bool> > BoolFunctionPtr; @@ -37,14 +35,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * String representation of conditional operator in form of: - * "if(cond_expr_raw_s, iftrue_expr_raw_s, iffalse_expr_raw_s)" - * This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "if" */ @@ -57,18 +47,22 @@ protected: const ArgFunctionPtr iftrue_expr_; const ArgFunctionPtr iffalse_expr_; - typedef FunctionBase::BaseRetValPtr BaseRetValPtr; /** * Evaluate the predicate. If it is true, evaluate and return value of * iftrue_expression. If predicate is false, evalute and return value * of iffalse_expression. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - if(this->cond_expr_->apply(context)->get_value()) { - return iftrue_expr_->apply(context); - } - return iffalse_expr_->apply(context); - } + FunctionBase::BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of conditional operator in form of: + * "if(cond_expr_raw_s, iftrue_expr_raw_s, iffalse_expr_raw_s)" + * This version does not require tagset, but may be inclomplete + * and/or contain internal info. + * @returns Stream written to. + */ + virtual std::ostream& write_to(std::ostream& ostream) const; + private: static const ArgFunctionPtr Default() { return detail::DefaultFunction<T>(); @@ -83,7 +77,8 @@ private: * and string representation is different. */ template<class T> -class ConditionalOp : public Conditional<T> { +class ConditionalOp : public Conditional<T> +{ public: typedef typename Conditional<T>::ArgFunctionPtr ArgFunctionPtr; typedef typename Conditional<T>::BoolFunctionPtr BoolFunctionPtr; @@ -101,37 +96,47 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of conditional operator in form of: - * "? if_true_expr_raw_string ? cond_expr_raw_string" - * @note This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "?". */ std::string raw_name() const { return "?"; } + +protected: + /** + * Writes String representation of conditional operator in form of: + * "? if_true_expr_raw_string ? cond_expr_raw_string" + * @note This version does not require tagset, but may be inclomplete + * and/or contain internal info. + * @returns Stream written to + */ + virtual std::ostream& write_to(std::ostream& ostream) const; }; + +// +// ----- Implementation ----- +// + template<class T> -std::string Conditional<T>::to_raw_string() const -{ - std::stringstream ss; - ss << boost::format("%1%(%2%, %3%, %4%)") - % this->raw_name() - % this->cond_expr_->to_raw_string() - % this->iftrue_expr_->to_raw_string() - % this->iffalse_expr_->to_raw_string(); - return ss.str(); +FunctionBase::BaseRetValPtr Conditional<T>::apply_internal(const FunExecContext& context) const { + if(this->cond_expr_->apply(context)->get_value()) { + return this->iftrue_expr_->apply(context); + } + return this->iffalse_expr_->apply(context); } template<class T> -std::string Conditional<T>::to_string(const Corpus2::Tagset &tagset) const -{ +std::ostream& Conditional<T>::write_to(std::ostream& os) const { + return os << raw_name() << "(" + << *this->cond_expr_ << ", " + << *this->iftrue_expr_ << ", " + << *this->iffalse_expr_ << ")"; +} + +template<class T> +std::string Conditional<T>::to_string(const Corpus2::Tagset &tagset) const { std::stringstream ss; ss << boost::format("%1%(%2%, %3%, %4%)") % this->name(tagset) @@ -141,16 +146,9 @@ std::string Conditional<T>::to_string(const Corpus2::Tagset &tagset) const return ss.str(); } - template<class T> -std::string ConditionalOp<T>::to_raw_string() const -{ - std::stringstream ss; - ss << boost::format("%1% %2% ? %3%") - % this->raw_name() - % this->iftrue_expr_->to_raw_string() - % this->cond_expr_->to_raw_string(); - return ss.str(); +std::ostream& ConditionalOp<T>::write_to(std::ostream& os) const { + return os << this->raw_name() << " " << *this->iftrue_expr_ << " ? " << *this->cond_expr_; } template<class T> diff --git a/libwccl/ops/functions/constant.h b/libwccl/ops/functions/constant.h index 4f298e3..e196162 100644 --- a/libwccl/ops/functions/constant.h +++ b/libwccl/ops/functions/constant.h @@ -7,12 +7,12 @@ namespace Wccl { - /** * Functional realisation of constant value of a given type */ template<class T> -class Constant : public Function<T> { +class Constant : public Function<T> +{ BOOST_CONCEPT_ASSERT((boost::CopyConstructible<T>)); public: /* @@ -35,27 +35,21 @@ public: * @returns String representation of the Constant, which is the * the string representation of the underlying Value. */ - std::string to_string(const Corpus2::Tagset& tagset) const { - return value_->to_string(tagset); - } + std::string to_string(const Corpus2::Tagset& tagset) const; +protected: /** * @returns String representation of the Constant that does * not require a tagset. It is same as raw string representation * of underlying Value. * @note May be incomplete and/or containt internal info. */ - std::string to_raw_string() const { - return value_->to_raw_string(); - } -protected : - typedef FunctionBase::BaseRetValPtr BaseRetValPtr ; + std::ostream& write_to(std::ostream& ostream) const; + /** - * Applying Constant function returns the held value of a constant + * @returns Underlying constant Value. */ - BaseRetValPtr apply_internal(const FunExecContext&) const { - return value_; - } + FunctionBase::BaseRetValPtr apply_internal(const FunExecContext&) const; private: const boost::shared_ptr<const T> value_; @@ -75,6 +69,25 @@ static const boost::shared_ptr<const Function<T> > DefaultFunction() { } +// +// ----- Implementation ----- +// + +template <class T> +std::string Constant<T>::to_string(const Corpus2::Tagset& tagset) const { + return value_->to_string(tagset); +} + +template <class T> +FunctionBase::BaseRetValPtr Constant<T>::apply_internal(const FunExecContext&) const { + return value_; +} + +template <class T> +std::ostream& Constant<T>::write_to(std::ostream& ostream) const { + return ostream << value_->to_raw_string(); +} + } /* end ns Wccl */ #endif // LIBWCCL_OPS_FUNCTIONS_CONSTANT_H diff --git a/libwccl/ops/functions/position/relativeposition.cpp b/libwccl/ops/functions/position/relativeposition.cpp index c9cf64f..86e1759 100644 --- a/libwccl/ops/functions/position/relativeposition.cpp +++ b/libwccl/ops/functions/position/relativeposition.cpp @@ -1,5 +1,4 @@ #include <libwccl/ops/functions/position/relativeposition.h> -#include <sstream> #include <libwccl/ops/functions/constant.h> namespace Wccl { @@ -27,16 +26,15 @@ std::string RelativePosition::to_string(const Corpus2::Tagset &tagset) const return ss.str(); } -std::string RelativePosition::to_raw_string() const +std::ostream& RelativePosition::write_to(std::ostream& ostream) const { - std::ostringstream ss; - ss << pos_expr_->to_raw_string(); + ostream << *pos_expr_; if(offset_ >= 0) { - ss << " + " << offset_; + ostream << " + " << offset_; } else { - ss << " - " << -offset_; + ostream << " - " << -offset_; } - return ss.str(); + return ostream; } } /* end ns Wccl */ diff --git a/libwccl/ops/functions/position/relativeposition.h b/libwccl/ops/functions/position/relativeposition.h index a29c2d0..f5786e3 100644 --- a/libwccl/ops/functions/position/relativeposition.h +++ b/libwccl/ops/functions/position/relativeposition.h @@ -26,11 +26,6 @@ public: * "arg_expr_string [+-] offset" */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "arg_expr_raw_string [+-] offset" - */ - std::string to_raw_string() const; /** * @returns Name of the function: "relpos" @@ -43,6 +38,13 @@ protected: const PosFunctionPtr pos_expr_; const int offset_; + /** + * Writes string representation of the function in the form of: + * "arg_expr_raw_string [+-] offset" + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; + /** * Takes the value of a Position from argument expression, and returns * a Position relative to it, shifted by the offset that this diff --git a/libwccl/ops/functions/strset/affix.cpp b/libwccl/ops/functions/strset/affix.cpp index 5344fcc..eabfaaf 100644 --- a/libwccl/ops/functions/strset/affix.cpp +++ b/libwccl/ops/functions/strset/affix.cpp @@ -1,5 +1,4 @@ #include <libwccl/ops/functions/strset/affix.h> -#include <sstream> #include <libpwrutils/foreach.h> namespace Wccl { @@ -12,11 +11,9 @@ std::string Affix::to_string(const Corpus2::Tagset& tagset) const return str.str(); } -std::string Affix::to_raw_string() const { - std::stringstream str; - str << raw_name() << "(" << strset_expr_->to_raw_string() - << ", " << affix_length_ << ")"; - return str.str(); +std::ostream& Affix::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" << *strset_expr_ << ", " << affix_length_ << ")"; } Affix::BaseRetValPtr Affix::apply_internal(const FunExecContext& context) const diff --git a/libwccl/ops/functions/strset/affix.h b/libwccl/ops/functions/strset/affix.h index 2dbc16f..ce6bd6f 100644 --- a/libwccl/ops/functions/strset/affix.h +++ b/libwccl/ops/functions/strset/affix.h @@ -11,7 +11,8 @@ namespace Wccl { * set with corresponding values that are prefixes or * suffixes of given length */ -class Affix : public Function<StrSet> { +class Affix : public Function<StrSet> +{ public: typedef boost::shared_ptr<Function<StrSet> > StrSetFunctionPtr; @@ -28,14 +29,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * String representation of conditional operator in form of: - * "affix(strset_expr_raw_string)" - * This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "affix" */ @@ -52,6 +45,15 @@ protected: * with all strings converted into prefixes or suffixes of given length */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the operator in form of: + * "affix(strset_expr_raw_string)" + * @note This version doesn't require tagset, but may be incomplete and/or + * contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/strset/getlemmas.cpp b/libwccl/ops/functions/strset/getlemmas.cpp index 1d95df3..750b677 100644 --- a/libwccl/ops/functions/strset/getlemmas.cpp +++ b/libwccl/ops/functions/strset/getlemmas.cpp @@ -9,8 +9,9 @@ std::string GetLemmas::to_string(const Corpus2::Tagset& tagset) const return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_, "[", "]"); } -std::string GetLemmas::to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *pos_expr_, "[", "]"); +std::ostream& GetLemmas::write_to(std::ostream& os) const +{ + return os << raw_name() << "[" << *pos_expr_ << "]"; } GetLemmas::BaseRetValPtr GetLemmas::apply_internal(const FunExecContext& context) const diff --git a/libwccl/ops/functions/strset/getlemmas.h b/libwccl/ops/functions/strset/getlemmas.h index 741a163..3e473f3 100644 --- a/libwccl/ops/functions/strset/getlemmas.h +++ b/libwccl/ops/functions/strset/getlemmas.h @@ -13,7 +13,8 @@ namespace Wccl { * Returns empty string set if position pointed outside of * the sentence boundaries. */ -class GetLemmas : public Function<StrSet> { +class GetLemmas : public Function<StrSet> +{ public: typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; @@ -29,14 +30,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "base(pos_expr_raw_string)" - * @note This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "base" */ @@ -54,6 +47,15 @@ protected: * lies within boundaries of the Sentence. Empty string set otherwise. */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "base(pos_expr_raw_string)" + * @note This version does not require tagset, but may be inclomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/strset/getorth.cpp b/libwccl/ops/functions/strset/getorth.cpp index 3bddb13..666b9f3 100644 --- a/libwccl/ops/functions/strset/getorth.cpp +++ b/libwccl/ops/functions/strset/getorth.cpp @@ -9,8 +9,9 @@ std::string GetOrth::to_string(const Corpus2::Tagset& tagset) const return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_, "[", "]"); } -std::string GetOrth::to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *pos_expr_, "[", "]"); +std::ostream& GetOrth::write_to(std::ostream& os) const +{ + return os << raw_name() << "[" << *pos_expr_ << "]"; } GetOrth::BaseRetValPtr GetOrth::apply_internal(const FunExecContext& context) const diff --git a/libwccl/ops/functions/strset/getorth.h b/libwccl/ops/functions/strset/getorth.h index 5a1e670..2308629 100644 --- a/libwccl/ops/functions/strset/getorth.h +++ b/libwccl/ops/functions/strset/getorth.h @@ -14,7 +14,8 @@ namespace Wccl { * Returns empty string set if position pointed outside of * the sentence boundaries. */ -class GetOrth : public Function<StrSet> { +class GetOrth : public Function<StrSet> +{ public: typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; @@ -30,14 +31,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "orth(pos_expr_string)" - * @note This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "orth" */ @@ -56,6 +49,15 @@ protected: * lies within boundaries of the Sentence. Empty string set otherwise. */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "orth(pos_expr_string)" + * @note This version does not require tagset, but may be inclomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/strset/tolower.cpp b/libwccl/ops/functions/strset/tolower.cpp index 696371b..36d54cf 100644 --- a/libwccl/ops/functions/strset/tolower.cpp +++ b/libwccl/ops/functions/strset/tolower.cpp @@ -8,11 +8,13 @@ std::string ToLower::to_string(const Corpus2::Tagset& tagset) const return UnaryFunctionFormatter::to_string(tagset, *this, *strset_expr_); } -std::string ToLower::to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *strset_expr_); +std::ostream& ToLower::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" << *strset_expr_ << ")"; } -ToLower::BaseRetValPtr ToLower::apply_internal(const FunExecContext& context) const { +ToLower::BaseRetValPtr ToLower::apply_internal(const FunExecContext& context) const +{ const boost::shared_ptr<const StrSet >& set = strset_expr_->apply(context); boost::shared_ptr<StrSet > l_set = boost::make_shared<StrSet>(); //TODO: should tolower be a method of StrSet as well? diff --git a/libwccl/ops/functions/strset/tolower.h b/libwccl/ops/functions/strset/tolower.h index bacbd91..f46e568 100644 --- a/libwccl/ops/functions/strset/tolower.h +++ b/libwccl/ops/functions/strset/tolower.h @@ -10,7 +10,8 @@ namespace Wccl { * Operator that takes a set of strings and returns a new * set with corresponding values in lower case form */ -class ToLower : public Function<StrSet> { +class ToLower : public Function<StrSet> +{ public: typedef boost::shared_ptr<Function<StrSet> > StrSetFunctionPtr; @@ -26,14 +27,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "lower(strset_expr_raw_string)" - * @note This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "lower" */ @@ -49,6 +42,15 @@ protected: * with all strings in lower case form */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "lower(strset_expr_raw_string)" + * @note This version does not require tagset, but may be inclomplete + * and/or contain internal info. + * @returns Stream writte to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/strset/toupper.cpp b/libwccl/ops/functions/strset/toupper.cpp index 1a9f4b5..09c8c8e 100644 --- a/libwccl/ops/functions/strset/toupper.cpp +++ b/libwccl/ops/functions/strset/toupper.cpp @@ -8,11 +8,13 @@ std::string ToUpper::to_string(const Corpus2::Tagset& tagset) const return UnaryFunctionFormatter::to_string(tagset, *this, *strset_expr_); } -std::string ToUpper::to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *strset_expr_); +std::ostream& ToUpper::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" << *strset_expr_ << ")"; } -ToUpper::BaseRetValPtr ToUpper::apply_internal(const FunExecContext& context) const { +ToUpper::BaseRetValPtr ToUpper::apply_internal(const FunExecContext& context) const +{ const boost::shared_ptr<const StrSet >& set = strset_expr_->apply(context); boost::shared_ptr<StrSet > u_set = boost::make_shared<StrSet>(); //TODO: should tolower be a method of StrSet as well? diff --git a/libwccl/ops/functions/strset/toupper.h b/libwccl/ops/functions/strset/toupper.h index c39d7a1..0009ba1 100644 --- a/libwccl/ops/functions/strset/toupper.h +++ b/libwccl/ops/functions/strset/toupper.h @@ -26,14 +26,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "upper(strset_expr_raw_string)" - * @note This version does not require tagset, but may be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns Name of the function: "upper" */ @@ -48,6 +40,15 @@ protected: * with all strings in upper case form */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "upper(strset_expr_raw_string)" + * @note This version does not require tagset, but may be inclomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/tset/getsymbols.cpp b/libwccl/ops/functions/tset/getsymbols.cpp index 4d2902c..2fa0ccd 100644 --- a/libwccl/ops/functions/tset/getsymbols.cpp +++ b/libwccl/ops/functions/tset/getsymbols.cpp @@ -9,8 +9,9 @@ std::string GetSymbols::to_string(const Corpus2::Tagset& tagset) const return UnaryFunctionFormatter::to_string(tagset, *this, *pos_expr_, "[", "]"); } -std::string GetSymbols::to_raw_string() const { - return UnaryFunctionFormatter::to_raw_string(*this, *pos_expr_, "[", "]"); +std::ostream& GetSymbols::write_to(std::ostream& os) const +{ + return os << raw_name() << "[" << *pos_expr_ << "]"; } std::string GetSymbols::name(const Corpus2::Tagset &tagset) const diff --git a/libwccl/ops/functions/tset/getsymbols.h b/libwccl/ops/functions/tset/getsymbols.h index df12472..a62300d 100644 --- a/libwccl/ops/functions/tset/getsymbols.h +++ b/libwccl/ops/functions/tset/getsymbols.h @@ -10,7 +10,8 @@ namespace Wccl { /** * Operator that gets tagset symbols from a token at given position. */ -class GetSymbols : public Function<TSet> { +class GetSymbols : public Function<TSet> +{ public: typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; @@ -26,14 +27,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "raw_attribute_name[pos_expr_raw_string]" - * @note This version does not require tagset, but will be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - std::string raw_name() const; std::string name(const Corpus2::Tagset& tagset) const; @@ -43,7 +36,6 @@ protected: const PosFunctionPtr pos_expr_; - /** * Gets a position from the argument expression, then gets the * word at that position from the Sentence in the SentenceContext, @@ -53,6 +45,15 @@ protected: * lies within boundaries of the Sentence. Empty Tset otherwise. */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "raw_attribute_name[pos_expr_raw_string]" + * @note This version does not require tagset, but will be inclomplete + * and/or contain internal info. + * @returns Stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/tset/getsymbolsinrange.cpp b/libwccl/ops/functions/tset/getsymbolsinrange.cpp index 406700c..fd49f56 100644 --- a/libwccl/ops/functions/tset/getsymbolsinrange.cpp +++ b/libwccl/ops/functions/tset/getsymbolsinrange.cpp @@ -15,16 +15,14 @@ std::string GetSymbolsInRange::to_string(const Corpus2::Tagset& tagset) const return ss.str(); } -std::string GetSymbolsInRange::to_raw_string() const { - std::stringstream ss; - ss << raw_name() << "(" - << mask_.raw_dump() << ", " - << rbegin_expr_->to_raw_string() << ", " - << rend_expr_->to_raw_string() << ")"; - return ss.str(); +std::ostream& GetSymbolsInRange::write_to(std::ostream& os) const +{ + return os << raw_name() << "(" + << mask_.raw_dump() << ", " + << *rbegin_expr_ << ", " + << *rend_expr_ << ")"; } - GetSymbolsInRange::BaseRetValPtr GetSymbolsInRange::apply_internal(const FunExecContext& context) const { const boost::shared_ptr<const Position>& range_begin = rbegin_expr_->apply(context); diff --git a/libwccl/ops/functions/tset/getsymbolsinrange.h b/libwccl/ops/functions/tset/getsymbolsinrange.h index aed881c..911e103 100644 --- a/libwccl/ops/functions/tset/getsymbolsinrange.h +++ b/libwccl/ops/functions/tset/getsymbolsinrange.h @@ -10,7 +10,8 @@ namespace Wccl { /** * Operator that gets tagset symbols from tokens in given range. */ -class GetSymbolsInRange : public Function<TSet> { +class GetSymbolsInRange : public Function<TSet> +{ public: typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; @@ -32,14 +33,6 @@ public: */ std::string to_string(const Corpus2::Tagset& tagset) const; - /** - * @returns String representation of the function in the form of: - * "range(raw_tagset_symbol, range_begin_expr_raw, range_end_expr_raw) - * @note This version does not require tagset, but will be inclomplete - * and/or contain internal info. - */ - std::string to_raw_string() const; - /** * @returns The operator name, "range" */ @@ -71,6 +64,15 @@ protected: * the mask, if the range is valid. An empty Tset otherwise. */ BaseRetValPtr apply_internal(const FunExecContext& context) const; + + /** + * Writes raw string representation of the function in the form of: + * "range(raw_tagset_symbol, range_begin_expr_raw, range_end_expr_raw) + * @note This version does not require tagset, but will be inclomplete + * and/or contain internal info. + * @returns The stream written to. + */ + std::ostream& write_to(std::ostream& ostream) const; }; } /* end ns Wccl */ diff --git a/libwccl/ops/functions/vargetter.h b/libwccl/ops/functions/vargetter.h index 4ed7986..53d1d33 100644 --- a/libwccl/ops/functions/vargetter.h +++ b/libwccl/ops/functions/vargetter.h @@ -9,7 +9,8 @@ namespace Wccl { * Operator that returns value of a variable of given type T */ template<class T> -class VarGetter : public Function<T> { +class VarGetter : public Function<T> +{ public: VarGetter(const VariableAccessor<T>& var_acc) : var_acc_(var_acc) @@ -27,31 +28,43 @@ public: * @returns String representation of the variable getter which is * equal to string representation of the underlying variable. */ - std::string to_raw_string() const { - return T::var_repr(var_acc_.get_name()); - } + std::string to_string(const Corpus2::Tagset&) const; +protected: /** - * @returns String representation of the variable getter which is + * Writes string representation of the variable getter which is * equal to string representation of the underlying variable. + * @returns Stream written to */ - std::string to_string(const Corpus2::Tagset&) const { - return T::var_repr(var_acc_.get_name()); - } + std::ostream& write_to(std::ostream& ostream) const; -protected: - typedef FunctionBase::BaseRetValPtr BaseRetValPtr; /** * Return value held by the underlying variable. */ - BaseRetValPtr apply_internal(const FunExecContext& context) const { - return context.variables()->get_fast(var_acc_); - } + FunctionBase::BaseRetValPtr apply_internal(const FunExecContext& context) const; private: const VariableAccessor<T> var_acc_; }; +// +// ----- Implementation ----- +// + +template <class T> +FunctionBase::BaseRetValPtr VarGetter<T>::apply_internal(const FunExecContext& context) const { + return context.variables()->get_fast(var_acc_); +} + +template <class T> +std::string VarGetter<T>::to_string(const Corpus2::Tagset &) const { + return T::var_repr(var_acc_.get_name()); +} + +template <class T> +std::ostream& VarGetter<T>::write_to(std::ostream& ostream) const { + return ostream << T::var_repr(var_acc_.get_name()); +} } /* end ns Wccl */ diff --git a/libwccl/ops/operator.h b/libwccl/ops/operator.h index 9ce8be5..625d205 100644 --- a/libwccl/ops/operator.h +++ b/libwccl/ops/operator.h @@ -202,10 +202,9 @@ public: std::string to_string(const Corpus2::Tagset& tagset) const; - std::string to_raw_string() const; - protected: Operator* clone_internal() const; + std::ostream& write_to(std::ostream& ostream) const; private: boost::shared_ptr<const Function<T> > function_body_; @@ -339,9 +338,9 @@ std::string Operator<T>::to_string(const Corpus2::Tagset &tagset) const { return function_body_->to_string(tagset); } -template <class T> inline -std::string Operator<T>::to_raw_string() const { - return function_body_->to_raw_string(); +template<class T> +std::ostream& Operator<T>::write_to(std::ostream& os) const { + return os << *function_body_; } } /* end ns Wccl */ -- GitLab