From f886a617cb2ac74f24a782d978d691d8285b50f7 Mon Sep 17 00:00:00 2001 From: Adam Wardynski <award@.(B-4.4.46a)> Date: Fri, 11 Mar 2011 16:26:41 +0100 Subject: [PATCH] MatchOperator - operator for match rules. --- libwccl/CMakeLists.txt | 1 + libwccl/ops/match/matchoperator.cpp | 56 ++++++++++++++++++++++++++ libwccl/ops/match/matchoperator.h | 61 +++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 libwccl/ops/match/matchoperator.cpp create mode 100644 libwccl/ops/match/matchoperator.h diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index 09413f3..f868a40 100644 --- a/libwccl/CMakeLists.txt +++ b/libwccl/CMakeLists.txt @@ -55,6 +55,7 @@ SET(libwccl_STAT_SRC ops/functions/tset/getsymbols.cpp ops/functions/tset/getsymbolsinrange.cpp ops/match/conditions/tokencondition.cpp + ops/match/matchoperator.cpp ops/rulesequence.cpp ops/tagaction.cpp ops/tagactions/delete.cpp diff --git a/libwccl/ops/match/matchoperator.cpp b/libwccl/ops/match/matchoperator.cpp new file mode 100644 index 0000000..f18a8e2 --- /dev/null +++ b/libwccl/ops/match/matchoperator.cpp @@ -0,0 +1,56 @@ +#include <libwccl/ops/match/matchoperator.h> +#include <libpwrutils/foreach.h> +#include <sstream> + +namespace Wccl { + +const char* MatchOperator::IterationPosVarName = "$_"; + +boost::shared_ptr<MatchVector> MatchOperator::execute( + boost::shared_ptr<Position> iter_pos, + const ActionExecContext &context) const +{ + boost::shared_ptr<MatchVector> matches(new MatchVector()); + int orig_iter_pos = iter_pos->get_value(); + + foreach (const boost::shared_ptr<const MatchCondition>& cond, _conditions) { + MatchResult res = cond->apply(iter_pos, context); + if(res.matched()) { + matches->append(res.get_match()); + } else { + matches.reset(new MatchVector()); + iter_pos->set_value(orig_iter_pos + 1); + return matches; + } + } + return matches; +} + +std::string MatchOperator::to_string(const Corpus2::Tagset& tagset) const +{ + std::ostringstream ostream; + ostream << name() << "("; + for(size_t i = 0; i < _conditions.size(); ++i) { + if (i != 0) { + ostream << ", "; + } + ostream << _conditions[i]->to_string(tagset); + } + ostream << ")"; + return ostream.str(); +} + +std::ostream& MatchOperator::write_to(std::ostream &ostream) const +{ + ostream << name() << "("; + for(size_t i = 0; i < _conditions.size(); ++i) { + if (i != 0) { + ostream << ", "; + } + ostream << *_conditions[i]; + } + ostream << ")"; + return ostream; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/match/matchoperator.h b/libwccl/ops/match/matchoperator.h new file mode 100644 index 0000000..348b416 --- /dev/null +++ b/libwccl/ops/match/matchoperator.h @@ -0,0 +1,61 @@ +#ifndef LIBWCCL_OPS_MATCH_MATCHOPERATOR_H +#define LIBWCCL_OPS_MATCH_MATCHOPERATOR_H + +#include <libwccl/ops/match/matchcondition.h> +#include <libwccl/values/matchvector.h> + +namespace Wccl { + +/** + * Operator that realizes "match" functionality for match rules + */ +class MatchOperator : public Expression +{ +public: + static const char* IterationPosVarName; + + MatchOperator(const std::vector< boost::shared_ptr<const MatchCondition> >& conditions) + : _conditions(conditions) { + } + + /** + * @returns Name of the operator. + */ + std::string name() const { + return "match"; + } + + /** + * @returns String representation of the Action + */ + std::string to_string(const Corpus2::Tagset& tagset) const; + +protected: + /** + * Writes string representation of the operator 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 operator on given context. + * @returns Vector of matches corresponding to match conditions, + * if all conditions were met. In such case position $_ is advanced past matched tokens. + * Empty MatchVector is returned if any of conditions was not met. + * In such case Position $_ is advanced by 1. + * @param iter_pos Pointer to the variable $_ from context's Variables + * @param context Execution context - current sentence and Variables to operate on + */ + boost::shared_ptr<MatchVector> execute( + boost::shared_ptr<Position> iter_pos, + const ActionExecContext &context) const; + +private: + const std::vector<boost::shared_ptr<const MatchCondition> > _conditions; +}; + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_MATCH_MATCHOPERATOR_H -- GitLab