Skip to content
Snippets Groups Projects
Commit 11e590fa authored by ilor's avatar ilor
Browse files

Add ambiguous(tset|strset|position) and singular(tset) operators

parent 8b85ba87
Branches
No related merge requests found
...@@ -38,12 +38,14 @@ SET(libwccl_STAT_SRC ...@@ -38,12 +38,14 @@ SET(libwccl_STAT_SRC
ops/functions/bool/iterations/only.cpp ops/functions/bool/iterations/only.cpp
ops/functions/bool/iterations/rightlook.cpp ops/functions/bool/iterations/rightlook.cpp
ops/functions/bool/predicate.cpp ops/functions/bool/predicate.cpp
ops/functions/bool/predicates/ambiguous.cpp
ops/functions/bool/predicates/and.cpp ops/functions/bool/predicates/and.cpp
ops/functions/bool/predicates/ann.cpp ops/functions/bool/predicates/ann.cpp
ops/functions/bool/predicates/annsub.cpp ops/functions/bool/predicates/annsub.cpp
ops/functions/bool/predicates/debug.cpp ops/functions/bool/predicates/debug.cpp
ops/functions/bool/predicates/isinside.cpp ops/functions/bool/predicates/isinside.cpp
ops/functions/bool/predicates/isoutside.cpp ops/functions/bool/predicates/isoutside.cpp
ops/functions/bool/predicates/issingular.cpp
ops/functions/bool/predicates/logicalpredicate.cpp ops/functions/bool/predicates/logicalpredicate.cpp
ops/functions/bool/predicates/nor.cpp ops/functions/bool/predicates/nor.cpp
ops/functions/bool/predicates/or.cpp ops/functions/bool/predicates/or.cpp
......
#include <libwccl/ops/functions/bool/predicates/ambiguous.h>
namespace Wccl {
} /* end ns Wccl */
#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_AMBIGUOUS_H
#define LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_AMBIGUOUS_H
#include <libwccl/ops/functions/bool/predicate.h>
#include <libwccl/values/position.h>
#include <libwccl/ops/formatters.h>
namespace Wccl {
class StrSet;
class TSet;
class Position;
/**
* Predicate that checks for ambiguity of a set type (size > 1).
* Can also check for ambiguity of a token at a position --- equivalent
* to checking if there is more than one possible lexeme.
*/
template <class T>
class IsAmbiguous : public Predicate
{
BOOST_MPL_ASSERT(( boost::mpl::count<boost::mpl::list<
StrSet, TSet, Position>, T> ));
public:
typedef boost::shared_ptr<Function<T> > ArgFunctionPtr;
IsAmbiguous(const ArgFunctionPtr& arg_expr)
: arg_expr_(arg_expr)
{
BOOST_ASSERT(arg_expr_);
}
/**
* @returns String representation of the function
*/
std::string to_string(const Corpus2::Tagset& tagset) const;
/**
* @returns Name of the function
*/
std::string raw_name() const {
return "ambiguous";
}
protected:
const ArgFunctionPtr arg_expr_;
/**
* Take value of argument and return True if it is empty, False otherwise.
*/
BaseRetValPtr apply_internal(const FunExecContext& context) const;
/**
* Writes raw string representation of the function
* @note This version does not require tagset but may be incomplete
* and/or contain internal info.
* @returns Stream written to.
*/
std::ostream& write_to(std::ostream& ostream) const;
};
//
// ----- Implementation -----
//
template <class T>
inline FunctionBase::BaseRetValPtr IsAmbiguous<T>::apply_internal(const FunExecContext& context) const {
return Predicate::evaluate(this->arg_expr_->apply(context)->size() > 1, context);
}
template <>
inline FunctionBase::BaseRetValPtr IsAmbiguous<Position>::apply_internal(const FunExecContext& context) const {
const SentenceContext& sc = context.sentence_context();
const boost::shared_ptr<const Position>& pos = arg_expr_->apply(context);
if (sc.is_outside(*pos)) {
return Predicate::False(context);
}
const Corpus2::Token* tok = sc.at(*pos);
return Predicate::evaluate(tok->lexemes().size() > 1, context);
}
template <class T>
inline std::string IsAmbiguous<T>::to_string(const Corpus2::Tagset &tagset) const {
return UnaryFunctionFormatter::to_string(tagset, *this, *arg_expr_);
}
template <class T>
inline std::ostream& IsAmbiguous<T>::write_to(std::ostream &os) const {
return os << this->raw_name() << "(" << *this->arg_expr_ << ")";
}
} /* end ns Wccl */
#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_AMBIGUOUS_H
#include <libwccl/ops/functions/bool/predicates/issingular.h>
#include <libwccl/ops/formatters.h>
namespace Wccl {
FunctionBase::BaseRetValPtr IsSingular::apply_internal(const FunExecContext& context) const {
const boost::shared_ptr<const TSet>& tag = arg_expr_->apply(context);
return Predicate::evaluate(tagset_.tag_is_singular(tag->get_value()), context);
}
std::string IsSingular::to_string(const Corpus2::Tagset &tagset) const {
return UnaryFunctionFormatter::to_string(tagset, *this, *arg_expr_);
}
std::ostream& IsSingular::write_to(std::ostream &os) const {
return os << this->raw_name() << "(" << *this->arg_expr_ << ")";
}
} /* end ns Wccl */
#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_ISSINGULAR_H
#define LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_ISSINGULAR_H
#include <libwccl/ops/functions/bool/predicate.h>
#include <libwccl/values/tset.h>
namespace Wccl {
/**
* Predicate that checks for tag singularity: at most one value
* chosen for each attribute.
*/
class IsSingular : public Predicate
{
public:
typedef boost::shared_ptr<Function<TSet> > ArgFunctionPtr;
IsSingular(const ArgFunctionPtr& arg_expr, const Corpus2::Tagset& tagset)
: arg_expr_(arg_expr), tagset_(tagset)
{
BOOST_ASSERT(arg_expr_);
}
/**
* @returns String representation of the function
*/
std::string to_string(const Corpus2::Tagset& tagset) const;
/**
* @returns Name of the function
*/
std::string raw_name() const {
return "singular";
}
protected:
const ArgFunctionPtr arg_expr_;
const Corpus2::Tagset& tagset_;
/**
* Take value of argument and return True if it is empty, False otherwise.
*/
BaseRetValPtr apply_internal(const FunExecContext& context) const;
/**
* Writes raw string representation of the function
* @note This version does not require tagset but may be incomplete
* and/or contain internal info.
* @returns Stream written to.
*/
std::ostream& write_to(std::ostream& ostream) const;
};
} /* end ns Wccl */
#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_PREDICATES_ISSINGULAR_H
...@@ -29,6 +29,8 @@ header { ...@@ -29,6 +29,8 @@ header {
#include <libwccl/ops/functions/bool/varsetter.h> #include <libwccl/ops/functions/bool/varsetter.h>
#include <libwccl/ops/functions/bool/predicates/debug.h> #include <libwccl/ops/functions/bool/predicates/debug.h>
#include <libwccl/ops/functions/bool/predicates/ambiguous.h>
#include <libwccl/ops/functions/bool/predicates/issingular.h>
#include <libwccl/ops/functions/bool/predicates/or.h> #include <libwccl/ops/functions/bool/predicates/or.h>
#include <libwccl/ops/functions/bool/predicates/nor.h> #include <libwccl/ops/functions/bool/predicates/nor.h>
#include <libwccl/ops/functions/bool/predicates/and.h> #include <libwccl/ops/functions/bool/predicates/and.h>
...@@ -1161,6 +1163,9 @@ bool_operator ...@@ -1161,6 +1163,9 @@ bool_operator
// annotation // annotation
| ret = bool_ann [scope] | ret = bool_ann [scope]
| ret = bool_annsub [scope] | ret = bool_annsub [scope]
// singular/amb
| ret = bool_ambiguous [scope]
| ret = bool_singular [scope]
// debug operators // debug operators
| ret = debug_print_operator [scope] | ret = debug_print_operator [scope]
// //
...@@ -1462,6 +1467,58 @@ bool_annsub ...@@ -1462,6 +1467,58 @@ bool_annsub
} }
; ;
// ----------------------------------------------------------------------------
// Ambiguity checking operator
bool_ambiguous
[ParsingScope& scope]
returns [boost::shared_ptr<Function<Bool> > ret]
{
boost::shared_ptr<Function<TSet> > tf;
boost::shared_ptr<Function<StrSet> > sf;
boost::shared_ptr<Function<Position> > pf;
}
: "ambiguous" LPAREN
(
(position_operator [scope]) =>
(
pf = position_operator [scope] {
ret.reset(new IsAmbiguous<Position>(pf));
}
)
|
(symset_operator [scope]) =>
(
tf = symset_operator [scope] {
ret.reset(new IsAmbiguous<TSet>(tf));
}
)
|
(strset_operator [scope]) =>
(
sf = strset_operator [scope] {
ret.reset(new IsAmbiguous<StrSet>(sf));
}
)
)
RPAREN
;
// ----------------------------------------------------------------------------
// Tag singularity checking operator
bool_singular
[ParsingScope& scope]
returns [boost::shared_ptr<Function<Bool> > ret]
{
boost::shared_ptr< Function<TSet> > v;
}
: "singular" LPAREN
v = symset_operator [scope]
RPAREN
{
ret.reset(new IsSingular(v, scope.tagset()));
}
;
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Debug printing: // Debug printing:
debug_print_operator debug_print_operator
......
...@@ -62,6 +62,10 @@ public: ...@@ -62,6 +62,10 @@ public:
return tag_.is_null(); return tag_.is_null();
} }
size_t size() const {
return tag_.pos_count() + PwrNlp::count_bits_set(tag_.get_values());
}
/** /**
* @return true if each tagset symbol from this set exists in the other set * @return true if each tagset symbol from this set exists in the other set
* (note that an empty set is a subset of anything) * (note that an empty set is a subset of anything)
......
---
singular({sg,pl})
False
---
singular({})
True
---
singular({praet,subst})
False
---
singular({subst,nom,sg,ter,imperf})
True
---
singular(cas)
False
---
ambiguous({})
False
---
ambiguous([])
False
---
ambiguous({subst})
False
---
ambiguous({subst,sg})
True
---
ambiguous("aaa")
False
---
ambiguous(["a", "b"])
True
---
ambiguous(cas)
True
---
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