diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index 500aa127ec58f9bd2926a3e9e040583f24cbb17c..09413f379d181af8e7b1063c49e81826354e0e9a 100644 --- a/libwccl/CMakeLists.txt +++ b/libwccl/CMakeLists.txt @@ -54,6 +54,7 @@ SET(libwccl_STAT_SRC ops/functions/tset/catfilter.cpp ops/functions/tset/getsymbols.cpp ops/functions/tset/getsymbolsinrange.cpp + ops/match/conditions/tokencondition.cpp ops/rulesequence.cpp ops/tagaction.cpp ops/tagactions/delete.cpp diff --git a/libwccl/ops/match/conditions/tokencondition.cpp b/libwccl/ops/match/conditions/tokencondition.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c0b0f2ae0bc5ae80acff45f13f209f63131489 --- /dev/null +++ b/libwccl/ops/match/conditions/tokencondition.cpp @@ -0,0 +1,41 @@ +#include <libwccl/ops/match/conditions/tokencondition.h> +#include <libwccl/values/tokenmatch.h> + +namespace Wccl { + + +TokenCondition::TokenCondition(const boost::shared_ptr<Function<Bool> >& predicate) + : _predicate(predicate) +{ +} + +std::string TokenCondition::name() const +{ + return _predicate->raw_name(); +} + +std::string TokenCondition::to_string(const Corpus2::Tagset& tagset) const +{ + return _predicate->to_string(tagset); +} + +MatchResult TokenCondition::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const +{ + if (_predicate->apply(context)->get_value()) { + boost::shared_ptr<Match> match = boost::shared_ptr<Match>(new TokenMatch( + context.sentence_context().get_abs_position(*iter_pos))); + // increase the $_ variable by one after successful token match + iter_pos->set_value(iter_pos->get_value() + 1); + return MatchResult(true, match); + } + else { + return MatchResult(); + } +} + +std::ostream& TokenCondition::write_to(std::ostream& os) const +{ + return os << *_predicate; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/match/conditions/tokencondition.h b/libwccl/ops/match/conditions/tokencondition.h new file mode 100644 index 0000000000000000000000000000000000000000..94f0bf2ddcbaf8fde817f96c848b65905e8d4742 --- /dev/null +++ b/libwccl/ops/match/conditions/tokencondition.h @@ -0,0 +1,47 @@ +#ifndef LIBWCCL_OPS_MATCH_CONDITIONS_TOKENCODITION_H +#define LIBWCCL_OPS_MATCH_CONDITIONS_TOKENCODITION_H + +#include <libwccl/ops/match/matchcondition.h> +#include <libwccl/ops/function.h> + +namespace Wccl { + +/** + * Class for conditions of MatchOperator that operate on tokens, + * wrapping WCCL Predicates + */ +class TokenCondition : public MatchCondition +{ +public: + TokenCondition(const boost::shared_ptr<Function<Bool> >& predicate); + /** + * @returns Name of the condition. + */ + std::string name() const; + /** + * Applies the condition to the given execution context. + * If match is found, the current iter Position "$_" is increased + * by one (the size of token match, which is always one). + */ + MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const; + + /** + * @returns String representation of the Action + */ + std::string to_string(const Corpus2::Tagset& tagset) const; + +protected: + /** + * Writes string representation of the TokenCondition 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; +private: + const boost::shared_ptr< const Function<Bool> > _predicate; +}; + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_MATCH_CONDITIONS_TOKENCODITION_H