package iitb.cfilt.cpost.ngi;

import iitb.cfilt.cpost.dmstemmer.*;
import iitb.cfilt.cpost.ConfigReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;

public class NounGroupIdentifier3 
{
	Vector<NgiResult> ngioutput;
	int ngiState;
		
	public NounGroupIdentifier3()
	{
		ngioutput = new Vector<NgiResult>();
		ngiState = 0; // Signifies that noun group has not begun
	}
	
	public void nounGroupIdentify(Vector<MAResult> maresultv, Vector<String> assignedtags)
	{
		Hashtable<String,String> ht = new Hashtable<String,String>();
		try 
		{
			BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("HMM.tagCat")), "UTF8"));
			String line = "";
			while((line = bf.readLine()) != null)
			{
				if(line.length() != 0)
				{
					String[] cols = line.split("\t");					
					String oldtag = cols[2].trim();
					String newtag = cols[0].trim();
					ht.put(oldtag, newtag);
				}
			}
			bf.close();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		} 
		Enumeration olt = ht.keys();
		for(int i=0;i<maresultv.size();i++)
			ngioutput.add(new NgiResult(maresultv.get(i),assignedtags.get(i)));
		int i = maresultv.size()-1;
		String noungroup = "";
		while((i>=0) && ((ngiState==0) || (ngiState==3)))
		{
			ngiState = 0;
			if(ngioutput.get(i).getset()==4)
			{
				ngiState = 1; // Signifies that oblique noun group has started 
				noungroup = noungroup + ngioutput.get(i).getToken();
			}
			else if(ngioutput.get(i).getset()==3)
			{
				ngiState = 2; // Signigies that direct noun group has started
				noungroup = noungroup + ngioutput.get(i).getToken();
			}
			else if(ngioutput.get(i).getassignedTag().equals("DEM"))
			{
				ngioutput.get(i).setfinalTag("PRP");
				ngioutput.get(i).setchanged(true);
				ngiState = 2;
				noungroup = noungroup + ngioutput.get(i).getToken();
			}
			else
			{
				if(i>0)
					i--;
				else
					break;
			}
			
			if(ngiState==1)
			{
				if(i>0)
					i--;
				else
					break;
				if(ngioutput.get(i).getset()!=3)
				{
					if(ngioutput.get(i).getpostags().contains("noun"))
					{
						ngioutput.get(i).setfinalTag("NN");
						ngioutput.get(i).setchanged(true);
						noungroup = ngioutput.get(i).getToken() + " " + noungroup;
					}
					else
					{
						ngiState = 0;
						noungroup = "";
						Vector<String> pt = ngioutput.get(i+1).getpostags();
						while(olt.hasMoreElements())
						{
							String tag = (String)olt.nextElement();
							if(pt.contains(tag))
							{
								ngioutput.get(i+1).setfinalTag(ht.get(tag));
								ngioutput.get(i+1).setchanged(true);
								break;
							}
						}
						if(i>=0)
							continue;
						else
							break;
					}
				}
				else
					noungroup = ngioutput.get(i).getToken() + " " + noungroup;
			}
			
			if((ngiState==1) || (ngiState==2))
			{
				if(i>0)
					i--;
				else
					break;
				while(((ngioutput.get(i).getset()==2) || (ngioutput.get(i).getset()==1)) && (i>=0))
				{
					noungroup = ngioutput.get(i).getToken() + " " + noungroup;
					if(i>0)
						i--;
					else
						break;
				}
				if(i<=0)
					ngiState = 4;
				else
					ngiState = 3;	
				System.out.println("Noungroup = " + noungroup);
				noungroup = "";
			}
		}
	}
	public void printfinaltags()
	{
		for (int i=0;i<ngioutput.size();i++)
		{
			NgiResult ngiresult = ngioutput.get(i);
			ngiresult.printresults();
		}
	}
	
	public Vector<NgiResult> getngiresults()
	{
		return ngioutput;
	}
	
	public static void main(String[] args)
	{
		ConfigReader.read(args[0]);
		NewStemmer dmstemmer = new NewStemmer();
		String sentence = "राम खाना खाता है .";
		String tokens[] = sentence.split(" ");
		List<String> lis = Arrays.asList(tokens);
		List<String> list = lis;
		Vector<String> tokenList = new Vector<String>(list);
		Vector<MAResult> mrv = new Vector<MAResult>();	
		for(int temp=0;temp<tokenList.size();temp++)
		{
			if(tokenList.get(temp)==null)
				continue;
			MAResult Mar = dmstemmer.stem(tokenList.get(temp));
			mrv.add(Mar);
		}
		Vector<String> assignedtags = new Vector<String>();
		assignedtags.add("NN");
		assignedtags.add("NN");
		assignedtags.add("VM");
		assignedtags.add("VAUX");
		assignedtags.add("SYM");
		NounGroupIdentifier3 ngi = new NounGroupIdentifier3();
		ngi.nounGroupIdentify(mrv,assignedtags);
	}
}
