/*
    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.
*/

#include <iostream>
#include <poliqarp_reader/PoliqarpReader.h>
#include <poliqarp_reader/PoliqarpSentencer.h>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>

int main(int argc, char *argv[])
{
	//return poliqmain(argc, argv);
	if(argc < 2)
	{
		std::cerr << "Usage: " << argv[0] << " CORPUS THRESHOLD\n";
		exit(1);
	}

	using namespace boost::posix_time;
	PoliqarpTools::CPoliqarpReader rdr(argv[1], false, false);
	PoliqarpTools::CPoliqarpSentencer sentr(&rdr);
	int max_len = boost::lexical_cast<int>(argv[2]);
	std::stringstream query;
	std::cerr << "Compiling...";
	query << "[]{0, 1000} within s";
	if(!rdr.CompileQuery(query.str().c_str() ) )
		throw std::runtime_error("Failed to compile query");

	size_t max = rdr.GetCorpusSize();
	std::cerr << "GetCorpusSize: " << (int)max << "\n";

	int tokens = 0;
	int sents = 0;
	std::cerr << "Starting...\n";
	ptime start = microsec_clock::local_time();
	Corpus::CSentence *sent;

	while((sent = sentr.GetNext()) != NULL)
	{
		if (sent->Size() < max_len)
		{
			continue;
		}

		Corpus::CToken *tok;
		bool first = true;
		for(int pos = 0; pos < sent->Size(); ++pos)
		{
			tok = (*sent)[pos];
			if(!first && !tok->NS) std::cout << " ";
			std::cout << tok->GetOrth();
			first = false;
		}

		std::cout << "\n\n";

		tokens += sent->Size();
		sents += 1;

		PoliqarpTools::CPoliqarpSentencer::Free(sent);

		if(sents % 100000 == 0)
		{
			ptime now = microsec_clock::local_time();
			time_duration len = now - start;
			long len_ms = len.total_milliseconds();
			long eta_ms = len_ms * (max - tokens) / tokens;
			std::cerr << "Processed " << tokens << " / " << max
					<< " Elapsed: " << milliseconds(len_ms) 	<< " ETA: " << to_simple_string( milliseconds(eta_ms) )
					<< "            \n";
			std::cerr.flush();
		}//if len > 5000
	}

	return 0;
}
