#include "wcrft2service.h"

#include <nlp_service/taskmanager.h>

namespace {
	bool str2bool(const std::string str)
	{
		if(str == "true" || str == "True" || str == "TRUE")
			return true;
		return false;
	}
}

namespace wcrft2service {

Wcrft2Service::Wcrft2Service(const std::string& config_path, std::string logfile_path,
							nlp_service::logging_lvl log_lvl, bool run_as_daemon)
: nlp_service::Service(config_path, logfile_path, log_lvl, run_as_daemon)
{
	std::string tagger_conf = config.get("tool.tagger_config", "nkjp_e2-morfeusz2");
	std::string tagger_model = config.get("tool.tagger_model", "model_nkjp10_wcrft_e2");
	this->tagger = new Wcrft::Tagger(tagger_conf, tagger_model);
	this->tagger->load_model();
}

Wcrft2Service::~Wcrft2Service()
{
	delete this->tagger;
}

void Wcrft2Service::process(std::string task_path, nlp_service::Service::task_config task_options, std::string output_path) const
{
	std::string task_token = nlp_service::TaskManager::get_task_token(task_path);

	bool ambiguity = task_options["ambiguity"].empty() ? false : str2bool(task_options["ambiguity"]);
	bool chunks = task_options["chunks"].empty() ? false : str2bool(task_options["chunks"]);
	this->tagger->switch_preserve_ambiguity(ambiguity);
	this->tagger->switch_paragraph_processing(chunks);

	std::string input_format = task_options["input_format"].empty() ? "txt" : task_options["input_format"];
	std::string output_format = task_options["output_format"].empty() ? "ccl" : task_options["output_format"];
	this->logger->task_trace(task_token, "Variables set.");

	this->logger->task_debug(task_token, "Tagging input from file " + task_path);
	this->tagger->tag_input(task_path, input_format, output_path, output_format);
	this->logger->task_debug(task_token, "Input from file " + task_path + " tagged");
}

}	// end namespace wcrft2service
