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

Fix MatchVector::first_token to ignore negative values.

It was looking for minimum value and Position::Nowhere is
the smallest you can get value-wise (min_int). So if there was
an empty Match in a vector, first_token for the vector would
always end up returning Position::Nowhere, which was wrong
(empty submatches should've been ignored and Position::Nowhere
returned only if there were no non-empty alternatives).
parent 672fb3aa
No related merge requests found
......@@ -27,12 +27,21 @@ Position MatchVector::first_token(const boost::shared_ptr<Corpus2::AnnotatedSent
if (matches_.empty()) {
return Position(Position::Nowhere);
} else {
// Negative positions are invalid, including specials like Nowhere,
// so we can't just find minimum value but minimum *non-negative* value.
// Note: yes, the code assumes the special values like Nowhere are indeed negative.
Position p = matches_.front()->first_token(s);
for (size_t i = 1; i < matches_.size(); ++i) {
size_t i = 1;
while ((p.get_value() < 0) && (i < matches_.size())) {
p = matches_[i]->first_token(s);
++i;
}
while (i < matches_.size()) {
Position c = matches_[i]->first_token(s);
if (c.get_value() < p.get_value()) {
if ((c.get_value() >= 0) && (c.get_value() < p.get_value())) {
p = c;
}
++i;
}
return p;
}
......
......@@ -98,6 +98,18 @@ BOOST_AUTO_TEST_CASE(matchvector_first_last)
m1->append(boost::make_shared<TokenMatch>(1));
BOOST_CHECK_EQUAL(m2.first_token(ptr).get_value(), 1);
BOOST_CHECK_EQUAL(m2.last_token(ptr).get_value(), 7);
Wccl::MatchVector m3;
BOOST_CHECK_EQUAL(m3.to_raw_string(), "MATCH()");
BOOST_CHECK_EQUAL(m3.first_token(ptr).get_value(), Wccl::Position::Nowhere);
BOOST_CHECK_EQUAL(m3.last_token(ptr).get_value(), Wccl::Position::Nowhere);
m3.append(boost::make_shared<MatchVector>());
BOOST_CHECK_EQUAL(m3.to_raw_string(), "MATCH(MATCH())");
BOOST_CHECK_EQUAL(m3.first_token(ptr).get_value(), Wccl::Position::Nowhere);
BOOST_CHECK_EQUAL(m3.last_token(ptr).get_value(), Wccl::Position::Nowhere);
m3.append(boost::make_shared<TokenMatch>(1));
BOOST_CHECK_EQUAL(m3.to_raw_string(), "MATCH(MATCH(),TOK[1])");
BOOST_CHECK_EQUAL(m3.first_token(ptr).get_value(), 1);
BOOST_CHECK_EQUAL(m3.last_token(ptr).get_value(), 1);
}
BOOST_AUTO_TEST_CASE(varmatch)
......
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