Skip to content
Snippets Groups Projects
lexicon.cpp 1.09 KiB
Newer Older
#include <libwccl/lexicon/lexicon.h>
#include <libpwrutils/util.h>
#include <libwccl/exception.h>
#include <libpwrutils/foreach.h>

#include <boost/make_shared.hpp>

namespace Wccl {

const UnicodeString& Lexicon::translate(const icu_44::UnicodeString &key) const
{
	static UnicodeString empty;
	map_t::const_iterator i = map_.find(key);
	if (i == map_.end()) {
		return empty;
	}
	return i->second;
}

boost::shared_ptr<StrSet> Lexicon::translate(const StrSet& set) const
{
	boost::shared_ptr<StrSet> ret_set = boost::make_shared<StrSet>();
	foreach(const UnicodeString& s, set.get_value()) {
		const UnicodeString& v = translate(s);
		if (!v.isEmpty()) {
			ret_set->insert(v);
		}
	}
	return ret_set;
}

void Lexicon::insert(const UnicodeString& key, const UnicodeString& value)
{
	if (has_key(key)) {
		throw InvalidArgument("key", PwrNlp::to_utf8(key) + " - entry already added.");
	}
	if (key.isEmpty()) {
		throw InvalidArgument("key", "Cannot add an empty string.");
	}
	if (value.isEmpty()) {
		throw InvalidArgument("value", "Cannot add an empty string.");
	}
	map_[key] = value;
}

} /* end ns Wccl */