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

MatchOperator - operator for match rules.

parent ed5f2256
Branches
No related merge requests found
......@@ -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
......
#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 */
#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
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