/*
    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 "RWFLinFilteredWeight.h"

namespace smartcomparator
{
	namespace weights
	{
		RWFLinFilteredWeight::RWFLinFilteredWeight(Matrices::SuperMatrixPtr pMatrix,
				RankBuilderPtr pRankbuilder, double treshold)
			: RWFLinWeight(pMatrix, pRankbuilder, treshold )
		{
		}

		RWFLinFilteredWeight::~RWFLinFilteredWeight()
		{
		}

		void RWFLinFilteredWeight::DoWeight(size_t rowindex, Matrices::CompressedVector *v, std::set<size_t> &filteredIndices,
				std::multimap<double, size_t> & val_to_col_no)
		{
			if(!m_bInitalized)
				Initalize();

			std::map<size_t, double>::iterator i = v->GetValues()->begin();

			for( size_t relation = 0; relation < m_vOperatorIndices.size() ; relation++ )
			{
				size_t lastIndex = (m_vOperatorIndices)[relation];

				double st_r_st = m_pAnyWordForRelationsWithAnyWord[relation];
				double w_r_st = m_pWordForRelationWithAnyWord[relation][rowindex];

				// could (should?) iterate over filteredIndices, and do removing of I<=0 after the loop...
				// but weighting is usualy done at the beginning of the loop
				while( i != v->GetValues()->end() && i->first < lastIndex)
				{
					if(filteredIndices.find(i->first) == filteredIndices.end() )
					{
						i++;
						continue;
					}

					if( IsFiltered(rowindex, i->first) )
					{
						i++;
						continue;
					}

					double w_r_w = i->second;

					double st_r_w = m_pAnyWordForRelationWithWord[i->first];

					double a = w_r_w * st_r_st;
					double b = w_r_st * st_r_w;

					double div = a/b;

					if(div <= 0 )
					{
						i++;
						continue;
					}

					double I = log(div);

					if( I < m_dMiFilter)
					{
						i++;
						continue;
					}

					val_to_col_no.insert( std::pair<double, size_t> (I, i->first) );
					i++;
				}
			}
		}//Weight

		bool RWFLinFilteredWeight::IsFiltered(size_t row_index, size_t column_index)
		{
			Matrices::FeatureVectorPtr pColumnFeatures = m_pMatrix->GetColumnFeatures();
			size_t column_tf = (*pColumnFeatures)[column_index].GetTermFrequency();
			Matrices::CompressedVectorPtr pRow = m_pMatrix->GetRow(row_index);
			double value = (*pRow)[ row_index ];
			if(value > 5 && (column_tf - value) > 5)
				return true;
			return false;
		}//void IsFiltered(size_t row_index, size_t column_index)

	}//namespace weights

}//namespace smartcomparator

