diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index 25eb988fef67ab1c409fbf0a2adca7f8f89ba992..f09edd862fd8df77623bee3ce52b52a94b83769e 100644 --- a/libwccl/CMakeLists.txt +++ b/libwccl/CMakeLists.txt @@ -28,6 +28,8 @@ endif(WIN32) SET(libwccl_STAT_SRC exception.cpp ops/action.cpp + ops/actions/delete.cpp + ops/actions/select.cpp ops/formatters.cpp ops/functions/bool/iteration.cpp ops/functions/bool/iterations/atleast.cpp diff --git a/libwccl/ops/actions/delete.cpp b/libwccl/ops/actions/delete.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6deb0518053790a7e66deb7824d0888fec1bb856 --- /dev/null +++ b/libwccl/ops/actions/delete.cpp @@ -0,0 +1,29 @@ +#include <libwccl/ops/actions/delete.h> +#include <libwccl/ops/functions/constant.h> +#include <sstream> + +namespace Wccl { + +Bool Delete::execute(const ActionExecContext& context) const +{ + Bool changed(false); + //@todo: implement + return changed; +} + +std::string Delete::to_string(const Corpus2::Tagset& tagset) const +{ + std::ostringstream os; + os << name() << "(" << pos_->to_string(tagset) << ", " + << condition_->to_string(tagset) << ")"; + return os.str(); +} + +std::ostream& Delete::write_to(std::ostream& os) const +{ + os << name() << "(" << *pos_ << ", " << *condition_ << ")"; + return os; +} + + +} /* end ns Wccl */ diff --git a/libwccl/ops/actions/delete.h b/libwccl/ops/actions/delete.h new file mode 100644 index 0000000000000000000000000000000000000000..376877b7b86c9107c0a74ca6f104e56eeca05ab4 --- /dev/null +++ b/libwccl/ops/actions/delete.h @@ -0,0 +1,69 @@ +#ifndef LIBWCCL_OPS_ACTIONS_DELETE_H +#define LIBWCCL_OPS_ACTIONS_DELETE_H + +#include <libwccl/ops/action.h> +#include <libwccl/values/position.h> +#include <libwccl/values/bool.h> +#include <libwccl/ops/function.h> + +namespace Wccl { + +/** + * Action to delete lexemes that meet a condition + * (unless no lexemes would be left, in which case + * token is left alone with no changes) + */ +class Delete : public Action +{ +public: + typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; + typedef boost::shared_ptr<Function<Bool> > BoolFunctionPtr; + + Delete(const BoolFunctionPtr& condition, const PosFunctionPtr& pos = detail::CurrentPos()) + : pos_(pos), + condition_(condition) + { + BOOST_ASSERT(pos_); + BOOST_ASSERT(condition_); + } + + /** + * @returns Name of the function. + */ + std::string name() const { + return "delete"; + } + + /** + * @returns String representation of the Action + */ + std::string to_string(const Corpus2::Tagset& tagset) const; + +protected: + /** + * Writes string representation of the Action to + * an output stream. + * @returns Stream written to. + * @note May be incomplete and/or containt internal info. + */ + std::ostream& write_to(std::ostream& ostream) const; + + /** + * Executes the Action on given context: it deletes + * all lexemes of the token at given position that meet + * the condition, unless all of them meet the condition. + * In such case no action is commited to avoid a token + * that would otherwise have no lexemes remaining. + * Also, no action is done if position points outside sentence. + * @returns True if there were any changes made; False otherwise + */ + Bool execute(const ActionExecContext &context) const; + +private: + const PosFunctionPtr pos_; + const BoolFunctionPtr condition_; +}; + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_ACTIONS_DELETE_H diff --git a/libwccl/ops/actions/select.cpp b/libwccl/ops/actions/select.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2e44ba4eb38d388698e60fd4f4ab661017732e --- /dev/null +++ b/libwccl/ops/actions/select.cpp @@ -0,0 +1,29 @@ +#include <libwccl/ops/actions/select.h> +#include <libwccl/ops/functions/constant.h> +#include <sstream> + +namespace Wccl { + +Bool Select::execute(const ActionExecContext& context) const +{ + Bool changed(false); + //@todo: implement + return changed; +} + +std::string Select::to_string(const Corpus2::Tagset& tagset) const +{ + std::ostringstream os; + os << name() << "(" << pos_->to_string(tagset) << ", " + << condition_->to_string(tagset) << ")"; + return os.str(); +} + +std::ostream& Select::write_to(std::ostream& os) const +{ + os << name() << "(" << *pos_ << ", " << *condition_ << ")"; + return os; +} + + +} /* end ns Wccl */ diff --git a/libwccl/ops/actions/select.h b/libwccl/ops/actions/select.h new file mode 100644 index 0000000000000000000000000000000000000000..df08cfee7f4179388d7d913db6df258d71ab4147 --- /dev/null +++ b/libwccl/ops/actions/select.h @@ -0,0 +1,69 @@ +#ifndef LIBWCCL_OPS_ACTIONS_SELECT_H +#define LIBWCCL_OPS_ACTIONS_SELECT_H + +#include <libwccl/ops/action.h> +#include <libwccl/values/position.h> +#include <libwccl/values/bool.h> +#include <libwccl/ops/function.h> + +namespace Wccl { + +/** + * Action to keep only lexemes that meet a condition + * (unless no lexemes would be left, in which case + * token is left alone with no changes) + */ +class Select : public Action +{ +public: + typedef boost::shared_ptr<Function<Position> > PosFunctionPtr; + typedef boost::shared_ptr<Function<Bool> > BoolFunctionPtr; + + Select(const BoolFunctionPtr& condition, const PosFunctionPtr& pos = detail::CurrentPos()) + : pos_(pos), + condition_(condition) + { + BOOST_ASSERT(pos_); + BOOST_ASSERT(condition_); + } + + /** + * @returns Name of the function. + */ + std::string name() const { + return "select"; + } + + /** + * @returns String representation of the Action + */ + std::string to_string(const Corpus2::Tagset& tagset) const; + +protected: + /** + * Writes string representation of the Action to + * an output stream. + * @returns Stream written to. + * @note May be incomplete and/or containt internal info. + */ + std::ostream& write_to(std::ostream& ostream) const; + + /** + * Executes the Action on given context: it deletes + * all lexemes of the token at given position that do not meet + * the condition, unless none of them meets the condition. + * In such case no action is commited to avoid a token + * that would otherwise have no lexemes remaining. + * Also, no action is done if position points outside sentence. + * @returns True if there were any changes made; False otherwise + */ + Bool execute(const ActionExecContext &context) const; + +private: + const PosFunctionPtr pos_; + const BoolFunctionPtr condition_; +}; + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_ACTIONS_SELECT_H