/*
    Copyright (C) 2012 Bartosz Broda, Jacek Bochenek, Łukasz Biłeńkij, 
    Damian Jaworski, Wojciech Mazur, Dominik Piasecki, Maciej Piasecki, 
    Radosław Ramocki
    
    This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.

    This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. 

    See the LICENSE and COPYING files for more details.
*/

#ifndef LIBWSDREADER_WSDREADER_H
#define LIBWSDREADER_WSDREADER_H

#include <libcorpus2/io/reader.h>

#include "wsd.h"

namespace Corpus2 {


class WSDReader: public TokenReader
{
public:
	/**
	  * \param filename corpus filename (WSD file is given in options)
	  */
	WSDReader(const Tagset& tagset, const std::string& filename);

	~WSDReader();

	/// retrieves whole sentence, finds WSDs, and return tokens
	Token* get_next_token();

	/// the prefered mode for this reader
	Sentence::Ptr get_next_sentence();

	/**
	  * retrieves chunk with inner reader and then searches for WSDs within
	  * sentences.
	  */
	boost::shared_ptr<Chunk> get_next_chunk();
	/**
	  * setting an "inner:..." option is equal to an immediate creation of an inner reader.
	  * If a filename set in ctor is not valid, setting the "inner" option results in error.
	  *
	 **/
	void set_option(const std::string& option);

	/**
	 * Option inspector. Should echo the option if it is set, return
	 * an empty string otheriwse, and "unknown" if the option is invalid.
	 */
	std::string get_option(const std::string& option) const;

	/**
	 * Check if the reader is valid, should throw if not. Called after
	 * all set_options during factory reader creation.
	 */
	virtual void validate();

	static bool registered;

protected:
	Sentence::Ptr process_sentence(Wccl::SentenceContext & sc);

private:
	std::string exec(const char* cmd);
	std::set<std::string> split_list(std::string list);

	WSDIndex wsd_index_;
	/// ptr to inner reader doing the real work of reading a corpus
	TokenReaderPtr inner_reader_;
	/// path for inner reader
	std::string inner_filename_;
	/// inner reader option
	size_t token_index;
	/// contains last processed sentence
	Sentence::Ptr currentSentence;
	/// contains last processed chunk
	boost::shared_ptr<Chunk> currentChunk;
	/// list of disambiguated words
	std::set<std::string> disamb_words;
	FILE *pipeout_fp;

	std::string work_dir;
	std::string classifier_conf;
	std::string weka_server;
	std::string models_dir;
};

} // ns Corpus2

#endif // LIBWSDREADER_WSDREADER_H
