package ITUannotation;

import java.util.ArrayList;

public class CollocationFinder {
	private ArrayList<String> TDKList;
	private ArrayList<ColElement> CollsFound;
	private String Sentence;

	public CollocationFinder(ArrayList<String> tDKList, String theSentence) {
		// TODO Auto-generated constructor stub
		TDKList=tDKList;
		CollsFound=new ArrayList<ColElement>();
		Sentence=theSentence;
	}

	public ArrayList<ColElement> findCols(String theSentence) {
		int wnumoftempCol=0;
		String foundCol="";
		// TODO Auto-generated method stub
		for(int i=0;i<TDKList.size();i++)
		{
			String temp=TDKList.get(i);
			if(theSentence.contains(temp))
			{
				ColElement CE=new ColElement(temp,getWordNumofCol(temp));
				CE.setFirstWordsIndexInSentence(getWordIndexofCol(CE));
				if(CollsFound.size()==0)
				{
					CollsFound.add(CE);
				}
				else{
					int size=CollsFound.size();
					int containedSTRindex=-1;
					for(int j=0;j<size;j++)
					{
						if(temp.contains(CollsFound.get(j).getContentOfCol()))
							containedSTRindex=j;
							//CollsFound.set(j, temp);
						//else
							//CollsFound.add(temp);
					}
					if(containedSTRindex==-1)
						CollsFound.add(CE);
					else
						CollsFound.set(containedSTRindex, CE);
						
				}
				/*wnumoftempCol=getWordNumofCol(temp);
				if(getWordNumofCol(foundCol)<wnumoftempCol)
				{
					foundCol=temp;
				}
				if(getWordNumofCol(foundCol)==wnumoftempCol&&temp.length()>foundCol.length())
				{
						foundCol=temp;
				}*/
				}
		}
		return CollsFound;
	}
	private int getWordNumofCol(String tempCol)
	{
		String wordArray[]=tempCol.split(" ");
		return wordArray.length;
	}
	public/*private*/ int getWordIndexofCol(ColElement Col)
	{
		String tempCol=Col.getContentOfCol();
		int WnumOfCol=Col.getWordNumofCol();
		String wordsOfCol[]=tempCol.split(" ");
		String wordsOfSentence[]=Sentence.split(" ");
		
		for(int j=0;j<wordsOfSentence.length;j++)
		{wordsOfSentence[j].replaceAll(" ", "");}
		
		for(int j=0;j<wordsOfCol.length;j++)
		{wordsOfCol[j].replaceAll(" ", "");}
		
		int index=-1;
		int fitWordNum=0;
		if (wordsOfCol.length>0)
		{
			for(int j=0;j<wordsOfSentence.length;j++)
			{
				String tmp1=wordsOfCol[0];
				String tmp2=wordsOfSentence[j];
				if(tmp2.contains(tmp1))//wordsOfCol[0].compareTo(wordsOfSentence[j])==0)//.contentEquals(wordsOfSentence[j]))//.contentEquals(wordsOfSentence[j]))
				{
					fitWordNum=0;
					for(int k=0;k<WnumOfCol;k++)
					{
						if(wordsOfSentence[j+k].contains(wordsOfCol[0+k]))
						{
							fitWordNum++;
						}
							
					}
					if(fitWordNum==WnumOfCol)
						index=j;
				}
			}
		}
		return index;
		}

	public  ArrayList<Integer> getIndexes(String theSentence) {
		 ArrayList<Integer>indexes=new ArrayList<Integer>();
		//int[]arr=new int[];
		 for(int k=0;k<CollsFound.size();k++)
		 {
			 System.out.println(CollsFound.get(k).getContentOfCol()+", "+CollsFound.get(k).getWordNumofCol());
			 
		 }
		// TODO Auto-generated method stub
		return null;
	}

}
