Skip to content
Snippets Groups Projects
Commit ed5f2256 authored by Adam Wardynski's avatar Adam Wardynski
Browse files

TokenCondition - wrapper for L0 predicates, matches single token under "$_"

parent 78575d6e
Branches
No related merge requests found
......@@ -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
......
#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 */
#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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment