From 65031756ba86d22bda2d4a88b37a53ccabdf8903 Mon Sep 17 00:00:00 2001 From: Adam Wardynski <award@.(B-4.4.46a)> Date: Wed, 12 Jan 2011 15:31:53 +0100 Subject: [PATCH] Delete and Select actions (WIP, not implemented). --- libwccl/CMakeLists.txt | 2 + libwccl/ops/actions/delete.cpp | 29 ++++++++++++++ libwccl/ops/actions/delete.h | 69 ++++++++++++++++++++++++++++++++++ libwccl/ops/actions/select.cpp | 29 ++++++++++++++ libwccl/ops/actions/select.h | 69 ++++++++++++++++++++++++++++++++++ 5 files changed, 198 insertions(+) create mode 100644 libwccl/ops/actions/delete.cpp create mode 100644 libwccl/ops/actions/delete.h create mode 100644 libwccl/ops/actions/select.cpp create mode 100644 libwccl/ops/actions/select.h diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index 25eb988..f09edd8 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 0000000..6deb051 --- /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 0000000..376877b --- /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 0000000..4b2e44b --- /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 0000000..df08cfe --- /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 -- GitLab