/*
    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 "../comparator_stdheaders.h"
#include "NoiseFilter.h"

namespace smartcomparator
{
	namespace filters
	{

		NoiseKFilter::NoiseKFilter(Matrices::FeatureVectorPtr cf, size_t k) 
			: FilterFeature(cf),
			m_stK(k)
		{
		}

		NoiseKFilter::NoiseKFilter(Matrices::FeatureVectorPtr cf, double retain_ratio)
			: FilterFeature(cf)
		{
			m_stK = int(retain_ratio * cf->size());
		}



		NoiseKFilter::~NoiseKFilter(void)
		{
		}

		void NoiseKFilter::Filter(std::set<size_t> &out,const std::set<size_t> *nzind)
		{
			std::multimap<double, size_t> index;

			for(std::set<size_t>::const_iterator i = nzind->begin(); i != nzind->end(); i++)
			{
				index.insert( std::pair<double, size_t> ( (*m_pColumnFeatures)[*i].GetEntropy(), *i) );
			}


			size_t i = 0;

			for(std::multimap<double, size_t>::iterator iter = index.begin(); iter != index.end() 
				&& i < m_stK; iter++, i++)
			{
				if(iter->first > 0 )
					out.insert( iter->second);

			}
		}


		NoiseKTFFilter::NoiseKTFFilter(Matrices::FeatureVectorPtr cf, size_t k, size_t tf) 
			: FilterFeature(cf),
			m_stK(k), 
			m_stMinTf(tf)
		{
		}

		NoiseKTFFilter::NoiseKTFFilter(Matrices::FeatureVectorPtr cf, double retain_ratio, size_t tf)
			: FilterFeature(cf),
			m_stMinTf(tf)
		{
			m_stK = int(retain_ratio * cf->size());
		}



		NoiseKTFFilter::~NoiseKTFFilter(void)
		{
		}

		void NoiseKTFFilter::Filter(std::set<size_t> &out,const std::set<size_t> *nzind)
		{
			std::multimap<double, size_t> index;

			for(std::set<size_t>::const_iterator i = nzind->begin(); i != nzind->end(); i++)
			{
				if( (*m_pColumnFeatures)[*i].GetTermFrequency() < m_stMinTf)
					continue;
				index.insert( std::pair<double, size_t> ( (*m_pColumnFeatures)[*i].GetEntropy(), *i) );
			}

			size_t i = 0;
			for(std::multimap<double, size_t>::iterator iter = index.begin(); iter != index.end() 
				&& i < m_stK; iter++, i++)
			{
				if(iter->first > 0 
					&& (*m_pColumnFeatures)[iter->second].GetTermFrequency() > m_stMinTf)

					out.insert( iter->second);
			}
		}
	}
}
