diff --git a/libwccl/ops/functions/bool/predicates/isinside.cpp b/libwccl/ops/functions/bool/predicates/isinside.cpp index a525d0bd03322e96bde0dd2021955b9cb6b298ae..a4a593a90c1816fed738e0b164d22fcce5694052 100644 --- a/libwccl/ops/functions/bool/predicates/isinside.cpp +++ b/libwccl/ops/functions/bool/predicates/isinside.cpp @@ -6,7 +6,7 @@ namespace Wccl { IsInside::BaseRetValPtr IsInside::apply_internal(const FunExecContext& context) const { const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); - return Predicate::evaluate(pos->is_inside(context.sentence_context()), context); + return Predicate::evaluate(context.sentence_context().is_inside(*pos), context); } std::string IsInside::to_string(const Corpus2::Tagset& tagset) const diff --git a/libwccl/ops/functions/bool/predicates/isoutside.cpp b/libwccl/ops/functions/bool/predicates/isoutside.cpp index d6228a667f5878c35712b5a0d4bb2edf62362310..5f6389e3086d244c3fb9cb89a5d067edbb2090f6 100644 --- a/libwccl/ops/functions/bool/predicates/isoutside.cpp +++ b/libwccl/ops/functions/bool/predicates/isoutside.cpp @@ -6,7 +6,7 @@ namespace Wccl { IsOutside::BaseRetValPtr IsOutside::apply_internal(const FunExecContext& context) const { const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); - return Predicate::evaluate(pos->is_outside(context.sentence_context()), context); + return Predicate::evaluate(context.sentence_context().is_outside(*pos), context); } std::string IsOutside::to_string(const Corpus2::Tagset& tagset) const diff --git a/libwccl/ops/functions/bool/predicates/pointagreement.cpp b/libwccl/ops/functions/bool/predicates/pointagreement.cpp index 8395083a564f092ba40acb392471041591613a0a..6104f5e754c02cdcd1c5fd6e6b3b1053a8b8ede2 100644 --- a/libwccl/ops/functions/bool/predicates/pointagreement.cpp +++ b/libwccl/ops/functions/bool/predicates/pointagreement.cpp @@ -26,11 +26,11 @@ PointAgreement::BaseRetValPtr PointAgreement::apply_internal(const FunExecContex const SentenceContext& sc = context.sentence_context(); const boost::shared_ptr<const Position>& pos1 = pos1_expr_->apply(context); - if (pos1->is_outside(sc)) { + if (sc.is_outside(*pos1)) { return Predicate::False(context); } const boost::shared_ptr<const Position>& pos2 = pos2_expr_->apply(context); - if (pos2->is_outside(sc)) { + if (sc.is_outside(*pos2)) { return Predicate::False(context); } diff --git a/libwccl/ops/functions/strset/getlemmas.cpp b/libwccl/ops/functions/strset/getlemmas.cpp index 750b677529c4367862745b253924c62e8a66305b..51539438b050b2aac67c558bc925b2aae7b756d2 100644 --- a/libwccl/ops/functions/strset/getlemmas.cpp +++ b/libwccl/ops/functions/strset/getlemmas.cpp @@ -18,7 +18,7 @@ GetLemmas::BaseRetValPtr GetLemmas::apply_internal(const FunExecContext& context { const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); const SentenceContext& sc = context.sentence_context(); - if(pos->is_outside(sc) || !sc.is_current_inside()) { + if (sc.is_outside(*pos)) { return detail::DefaultFunction<StrSet>()->apply(context); } boost::shared_ptr<StrSet> u_set = boost::make_shared<StrSet>(); diff --git a/libwccl/ops/functions/strset/getorth.cpp b/libwccl/ops/functions/strset/getorth.cpp index 666b9f345274d03253cf840cc78d636e454ca376..33371c4e4e9e77ab045d9fc17645f8f04a24f75e 100644 --- a/libwccl/ops/functions/strset/getorth.cpp +++ b/libwccl/ops/functions/strset/getorth.cpp @@ -18,7 +18,7 @@ GetOrth::BaseRetValPtr GetOrth::apply_internal(const FunExecContext& context) co { const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); const SentenceContext& sc = context.sentence_context(); - if(pos->is_outside(sc) || !sc.is_current_inside()) { + if (sc.is_outside(*pos)) { return detail::DefaultFunction<StrSet>()->apply(context); } boost::shared_ptr<StrSet> u_set = boost::make_shared<StrSet>(); diff --git a/libwccl/ops/functions/tset/catfilter.cpp b/libwccl/ops/functions/tset/catfilter.cpp index d16faefc3dfc87132161e905d80fb253cbf8ba77..b5e1b24e94c1f6d78a0509ad12a9d62d07bca493 100644 --- a/libwccl/ops/functions/tset/catfilter.cpp +++ b/libwccl/ops/functions/tset/catfilter.cpp @@ -25,7 +25,7 @@ CatFilter::BaseRetValPtr CatFilter::apply_internal(const FunExecContext& context { const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); const SentenceContext& sc = context.sentence_context(); - if (pos->is_outside(sc)) { + if (sc.is_outside(*pos)) { return detail::DefaultFunction<TSet>()->apply(context); } diff --git a/libwccl/ops/functions/tset/getsymbols.cpp b/libwccl/ops/functions/tset/getsymbols.cpp index 5137ca2aa3451786c087802be6fddd2c686f1763..d23f6ec3140f6b5972a5f7282bd1ce0cb32c2e88 100644 --- a/libwccl/ops/functions/tset/getsymbols.cpp +++ b/libwccl/ops/functions/tset/getsymbols.cpp @@ -28,7 +28,7 @@ GetSymbols::BaseRetValPtr GetSymbols::apply_internal(const FunExecContext& conte { const boost::shared_ptr<const Position>& pos = pos_expr_->apply(context); const SentenceContext& sc = context.sentence_context(); - if(pos->is_outside(sc) || !sc.is_current_inside()) { + if(sc.is_outside(*pos)) { return detail::DefaultFunction<TSet>()->apply(context); } diff --git a/libwccl/ops/parsedexpression.h b/libwccl/ops/parsedexpression.h index b3b007a63ae124f7b17cbb3a9b57b4fc01faf3a5..5a41d0c8d2ce31e2ea4d7190ace451761b3efe0f 100644 --- a/libwccl/ops/parsedexpression.h +++ b/libwccl/ops/parsedexpression.h @@ -139,18 +139,19 @@ public: */ boost::shared_ptr<ParsedExpression> clone_clean_ptr() const; + /** - * Dup all variables to error stream. + * @returns String with representation of all held variables, + * in form of "variable = value" pairs, each pair on one line. */ - void dumpvariables(const Corpus2::Tagset& tagset) - { - typedef std::pair<std::string, boost::shared_ptr<Wccl::Value> > v_t; + std::string variables_string(const Corpus2::Tagset& tagset) const; - foreach (const v_t& v, variables_->get_all<Wccl::Value>()) { - std::cerr << v.second->make_var_repr(v.first) << "=" - << v.second->to_string(tagset) << "\n"; - } - } + /** + * Dump all variables to an output stream, in form of + * "variable = value" pairs, each pair on another line. + * @returns Stream written to. + */ + std::ostream& dump_variables(std::ostream& ostream, const Corpus2::Tagset& tagset) const; protected: explicit ParsedExpression(const Variables& variables); @@ -231,6 +232,25 @@ boost::shared_ptr<ParsedExpression> ParsedExpression::clone_clean_ptr() const { return copy; } +inline +std::ostream& ParsedExpression::dump_variables( + std::ostream &ostream, + const Corpus2::Tagset &tagset) const { + typedef std::pair<std::string, boost::shared_ptr<Wccl::Value> > v_t; + foreach (const v_t& v, variables_->get_all<Wccl::Value>()) { + ostream << v.second->make_var_repr(v.first) << "=" + << v.second->to_string(tagset) << "\n"; + } + return ostream; +} + +inline +std::string ParsedExpression::variables_string(const Corpus2::Tagset &tagset) const { + std::ostringstream oss; + dump_variables(oss, tagset); + return oss.str(); +} + } /* end ns Wccl */ #endif // LIBWCCL_OPS_PARSEDEXPRESSION_H diff --git a/libwccl/sentencecontext.h b/libwccl/sentencecontext.h index 7451d78c17bacf179c454ae02af0906a1a5028f5..1fecb3f84739e3424090afcb684f66d5c7ee2440 100644 --- a/libwccl/sentencecontext.h +++ b/libwccl/sentencecontext.h @@ -57,6 +57,21 @@ public: return abs_pos >= 0 && abs_pos < size(); } + /// Checks if the the given position is valid (in sentence bounds) + bool is_inside(const Position& pos) const { + return is_inside(get_abs_position(pos)); + } + + /// Checks if the the given absolute position is outside sentence, including nowhere + bool is_outside(int abs_pos) const { + return !is_inside(abs_pos); + } + + /// Checks if the the given position is outside sentence, including nowhere + bool is_outside(const Position& pos) const { + return !is_inside(get_abs_position(pos)); + } + /// Position setter void set_position(int new_position) { position_ = new_position; diff --git a/libwccl/values/position.cpp b/libwccl/values/position.cpp index 8b816c0c9ded32138cf6e925a241cfafc66cb171..41a5abaa7a1546941e55f0e5f4fd7acc99465a28 100644 --- a/libwccl/values/position.cpp +++ b/libwccl/values/position.cpp @@ -27,12 +27,6 @@ std::string Position::var_repr(const std::string &var_name) return ss.str(); } -bool Position::is_inside(const SentenceContext& context) const -{ - int abs_position = context.get_abs_position(*this); - return context.is_inside(abs_position); -} - bool Position::equals(const Position& other, const SentenceContext& context) const { return context.get_abs_position(*this) == context.get_abs_position(other); diff --git a/libwccl/values/position.h b/libwccl/values/position.h index 43984feb87455650376b389c9bb3c71007a5dd35..943f2805931d5c3f402655c755020e03ad71a222 100644 --- a/libwccl/values/position.h +++ b/libwccl/values/position.h @@ -48,17 +48,6 @@ public: /// Value override std::string to_raw_string() const; - /** - * @returns True if Position is within bounds of a sentence, false otherwise. - */ - bool is_inside(const SentenceContext& context) const; - - /** - * @returns True if Position is outside of bounds of a sentence, false otherwise. - */ - bool is_outside(const SentenceContext& context) const { - return !is_inside(context); - } /** * @returns True if underlying position values are equal, false otherwise. * @note This version does not take into account sentence context, only diff --git a/tests/getlemmas.cpp b/tests/getlemmas.cpp index 3c92eb2967fe76579a0cb7d1185a3cf2d3638138..568c687c677efa66161602633e23f35c2672e7e2 100644 --- a/tests/getlemmas.cpp +++ b/tests/getlemmas.cpp @@ -97,7 +97,7 @@ BOOST_FIXTURE_TEST_CASE(lemmas_begin, LemmasPredFix) sc.advance(); BOOST_CHECK(lemmas.apply(cx)->equals(first_lemmas)); sc.advance(); - BOOST_CHECK(lemmas.apply(cx)->equals(empty_set)); + BOOST_CHECK(lemmas.apply(cx)->equals(first_lemmas)); } BOOST_FIXTURE_TEST_CASE(lemmas_end, LemmasPredFix) @@ -107,7 +107,7 @@ BOOST_FIXTURE_TEST_CASE(lemmas_end, LemmasPredFix) sc.advance(); BOOST_CHECK(lemmas.apply(cx)->equals(second_lemmas)); sc.advance(); - BOOST_CHECK(lemmas.apply(cx)->equals(empty_set)); + BOOST_CHECK(lemmas.apply(cx)->equals(second_lemmas)); } BOOST_FIXTURE_TEST_CASE(lemmas_zero, LemmasPredFix) @@ -137,6 +137,8 @@ BOOST_FIXTURE_TEST_CASE(lemmas_minus_one, LemmasPredFix) sc.advance(); BOOST_CHECK(lemmas.apply(cx)->equals(first_lemmas)); sc.advance(); + BOOST_CHECK(lemmas.apply(cx)->equals(second_lemmas)); + sc.advance(); BOOST_CHECK(lemmas.apply(cx)->equals(empty_set)); } //------ to_string test cases ------- diff --git a/tests/getorth.cpp b/tests/getorth.cpp index 947e9bb5e1e16f217efb5513828079df0846ad8a..49e2b4ad71cf41e4b065c41cf91222cfaa69f359 100644 --- a/tests/getorth.cpp +++ b/tests/getorth.cpp @@ -96,7 +96,7 @@ BOOST_FIXTURE_TEST_CASE(orth_begin, OrthPredFix) sc.advance(); BOOST_CHECK(orth.apply(cx)->equals(first_orth)); sc.advance(); - BOOST_CHECK(orth.apply(cx)->equals(empty_set)); + BOOST_CHECK(orth.apply(cx)->equals(first_orth)); sc.goto_start(); } @@ -107,7 +107,7 @@ BOOST_FIXTURE_TEST_CASE(orth_end, OrthPredFix) sc.advance(); BOOST_CHECK(orth.apply(cx)->equals(second_orth)); sc.advance(); - BOOST_CHECK(orth.apply(cx)->equals(empty_set)); + BOOST_CHECK(orth.apply(cx)->equals(second_orth)); sc.goto_start(); } @@ -140,6 +140,8 @@ BOOST_FIXTURE_TEST_CASE(orth_minus_one, OrthPredFix) sc.advance(); BOOST_CHECK(orth.apply(cx)->equals(first_orth)); sc.advance(); + BOOST_CHECK(orth.apply(cx)->equals(second_orth)); + sc.advance(); BOOST_CHECK(orth.apply(cx)->equals(empty_set)); sc.goto_start(); } diff --git a/wcclparser/main.cpp b/wcclparser/main.cpp index fdd2e40b5162ac2a8edf467e9987189f834f6ecb..cd00f57417d75e599740c1ae907d9655ac2c86c0 100644 --- a/wcclparser/main.cpp +++ b/wcclparser/main.cpp @@ -126,7 +126,7 @@ bool process_line(const std::string& line, Wccl::Parser& parser, << retVal->to_string(parser.tagset()) << std::endl; if (dump_variables) { - retOp->dumpvariables(parser.tagset()); + retOp->dump_variables(std::cerr, parser.tagset()); } } else { std::cerr << "Problem while parsing -- " @@ -210,6 +210,7 @@ int main(int argc, char** argv) boost::shared_ptr<Corpus2::Sentence> sentence; if (sentence_load.empty()) { sentence.reset(new Corpus2::Sentence); + sentence->append(new Corpus2::Token("", PwrNlp::Whitespace::ManySpaces)); } else { std::ifstream ifs(sentence_load.c_str()); if (ifs.good()) {