#include <libwccl/values/annotationmatch.h>

namespace Wccl {

std::string AnnotationMatch::to_raw_string() const
{
	return "ANN[" + position_.to_raw_string() + "," + channel_ + "]";
}

Position AnnotationMatch::first_token(const boost::shared_ptr<Corpus2::AnnotatedSentence>& s) const
{
	size_t fpos = position_.get_value();
	const Corpus2::AnnotationChannel& chan = s->get_channel(channel_);
	int seg = chan.get_segment_at(fpos);
	if (seg > 0) {
		for (size_t i = 0; i < fpos; ++i) {
			if (chan.get_segment_at(i) == seg) {
				return Position(i);
			}
		}
		return position_;
	} else {
		return Position();
	}
}

Position AnnotationMatch::last_token(const boost::shared_ptr<Corpus2::AnnotatedSentence>& s) const
{
	size_t fpos = position_.get_value();
	const Corpus2::AnnotationChannel& chan = s->get_channel(channel_);
	int seg = chan.get_segment_at(fpos);
	if (seg > 0) {
		for (size_t i = s->size() - 1; i > fpos; --i) {
			if (chan.get_segment_at(i) == seg) {
				return Position(i);
			}
		}
		return position_;
	} else {
		return Position();
	}
}


} /* end ns Wccl */