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

static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("SuperMatrix.comparator.weights.TScoreWeight"));

namespace smartcomparator
{
	namespace weights
	{
		TScoreWeight::TScoreWeight(Matrices::SuperMatrixPtr m) 
			: WeightFeature(m){

			W = (double) m_pMatrix->GetTokenCount();

			if(W <= 0 )
			{
				W = 254524624.;
				LOG4CXX_ERROR(logger,"Matrix does not contain information about number of tokens in corpus... Using "
							  << W << " as this value (number of tokens in KIPI 2.0");
				
			}
		}

		TScoreWeight::~TScoreWeight(void)
		{
		}


		void TScoreWeight::Weight(size_t rowIndex, Matrices::CompressedVector *v, 
			std::set<size_t> &filteredIndices) 
		{	
			double TFn = (double) m_pMatrix->GetRowFeatures()->operator[](rowIndex).GetGlobalTermFrequency();

			for(std::set<size_t>::const_iterator i = filteredIndices.begin(); i != filteredIndices.end(); i++)
			{
				double Cnc = (*v)[*i];
				if(Cnc == 0) 
					continue;
				double col_global_tf_i = (*m_pMatrix->GetColumnFeatures())[*i].GetGlobalTermFrequency();
				double part = (TFn* col_global_tf_i) / W;

				double Rz = (Cnc - part) / sqrt(part);

				
				v->SetElement(*i, Rz);
			}

		}
	}
}
