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

namespace LexicalUnits
{
	UnitsList::UnitsList()
	{
		// nothing
	}

	void UnitsList::AddUnit(LexicalUnitPtr pUnit)
	{
		std::set<int> bases;
		pUnit->GetPotentialBases(bases);
		m_pvLexicalUnits.push_back(pUnit);
		size_t unit_index = m_pvLexicalUnits.size();
		for(std::set<int>::iterator iter = bases.begin(); iter != bases.end(); iter++)
			m_LexIndex[ *iter ].push_back( unit_index );
	}//	void UnitsList::AddUnit(LexicalUnitPtr pUnit)

	LexicalUnitPtr UnitsList::GetUnitOnCurrentPosition(Corpus::CSentence *sentence)
	{
		Corpus::CToken *pToken = sentence->GetCurrentToken();
		if( pToken == NULL )
			return LexicalUnitPtr();
		
		std::vector<Corpus::CLexem*>& lexems = pToken->GetLexems();
		size_t head_index;
        std::set<size_t> constituents;
		//for each disamb
		for(std::vector<Corpus::CLexem*>::iterator iter = lexems.begin(); iter != lexems.end(); iter++)
		{
			Corpus::CLexem* lex = *iter;
			if( lex->GetDisamb() )
			{
				std::vector<size_t> & potential_units_indices = m_LexIndex[ lex->GetBaseIdx() ];
				// check every potential unit
				for(std::vector<size_t>::iterator id_iter = potential_units_indices.begin();
						id_iter != potential_units_indices.end(); id_iter++)
				{
					// check for match
					if( m_pvLexicalUnits[*id_iter]->CheckCurrentPosition(*sentence, head_index, constituents) )
						return m_pvLexicalUnits[*id_iter];
				}
			}
		}
			
		// nothing foud 
		return LexicalUnitPtr();
	}//LexicalUnitPtr UnitsList::GetUnitOnCurrentPosition(Corpus::CSentence *sentence)

}//namespace LexicalUnits


