package iitb.cfilt.cpost.crfpp;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Vector;
import java.util.List;
import java.util.Arrays;
import java.util.Hashtable;
import iitb.cfilt.cpost.*;
import iitb.cfilt.cpost.dmstemmer.*;
//import iitb.cfilt.cpost.test.TestVGIdm;
import iitb.cfilt.cpost.vgi.*;

public class TestVGIdm {
	private static NewStemmer dmstemmer;
	private static VerbGroup9 vb;
	private static MAResult Mar;
	private static Vector<MAResult> mrv;
		
	public TestVGIdm(){
		dmstemmer = new NewStemmer();
		vb = new VerbGroup9();	
		mrv = new Vector<MAResult>();
	}
	
	@SuppressWarnings("unchecked")
	public static void doVGI_forFile(String inFile){
		try{
			BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "UTF8"));
			String line = "";
			int [] vgi;
			while((line = bf.readLine()) != null) {
				if(line.length() != 0) {
					String tokens[] = line.split(" ");
					List l = Arrays.asList(tokens);
					Vector<String> tokenList = new Vector<String>(l);
					vgi = new int[tokenList.size()];
					for(int temp=0;temp<tokenList.size();temp++)
					{
						if(!(tokenList.get(temp).contentEquals(" ") || tokenList.get(temp).contentEquals("")))
						{
							Mar = dmstemmer.stem(tokenList.get(temp));
							mrv.add(Mar);
						}
					}
					vgi = vb.identifyVerbGroups1(mrv);
					System.out.println("");
					for (int i = 0; i < vgi.length; i++) {
						System.out.print(tokens[i] + "- " + vgi[i] + "\t");
					}
					System.out.println("");
				}
			}
		} catch(Exception e){
			//System.out.println(e)
			e.printStackTrace();
		}		
	}
	
	@SuppressWarnings({ "unchecked", "unchecked", "unchecked" })
	public int[] doVGI_forSentence( String sentence, boolean flagWithTags ){
		String rawSentence = "";
		if(flagWithTags){
			String[] taggedWords = sentence.split("\\]");
			String token = "";
			for(int i=0;i<taggedWords.length;i++)
			{
				if(taggedWords[i].contains("_"))
				{
					//System.out.println("word.."+taggedWords[i]+"..done");
					String[] taggedWordComponents = taggedWords[i].split("_\\[");
					if(taggedWordComponents[0].trim()!="")
					 token = taggedWordComponents[0].trim();
					else token=".";
				}
				if(token != "")
					rawSentence = rawSentence + " " + token;
			}
		}
		else
			rawSentence = sentence;
		
		int [] retArr = new int[rawSentence.length()];
		String tokens[] = rawSentence.split(" ");
		List l = Arrays.asList(tokens);
		Vector<String> tokenList = new Vector<String>(l);
		for(int temp=0;temp<tokenList.size();temp++)
		{
			if(tokenList.get(temp)==null)
				continue;
			Mar = dmstemmer.stem(tokenList.get(temp));
			mrv.add(Mar);
		}
		retArr = vb.identifyVerbGroups1(mrv);
		return retArr;
	}
	
	public int[] doVGI_forSentence(Vector<MAResult> maresultv)
	{
		TestVGIdm.mrv = maresultv;
		int [] retArr = new int[maresultv.size()];
		retArr = vb.identifyVerbGroups1(mrv);
		return retArr;
	}
	@SuppressWarnings("unchecked")
	public int[] doVGI_forSentence(Vector<MAResult> maresultv, Vector<String> tags)
	{
		try
		{
			Vector<MAResult> maresultvec = maresultv;
			MAResult maresult;
			String tag;
			Hashtable ht = new Hashtable();
			BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("Tagset.tagMappingFilename")), "UTF8"));
			String line = "";
			while((line = bf.readLine()) != null)
			{
				String[] tagset = line.split("-");
				ht.put(tagset[1].trim(),tagset[0].trim());
			}
			bf.close();
			for(int i=0;i<maresultv.size();i++)
			{
				maresult = maresultvec.get(i);
				tag = tags.get(i);
				for(int j=0;j<maresult.getMorphOutputs().size();j++)
				{
					if(!ht.get(maresult.getMorphOutputs().get(j).getStemmerResult().getCategory()).toString().equals(tag))
					{
						maresult.getMorphOutputs().remove(j);
						j--;
					}
				}
				mrv.add(maresult);
			}
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			e.printStackTrace();
		}
		int [] retArr = new int[maresultv.size()];
		retArr = vb.identifyVerbGroups1(mrv);
		return retArr;
	}
	
	public static void main(String args[])
	{
		ConfigReader.read(args[0]);
		dmstemmer = new NewStemmer();
		vb = new VerbGroup9();	
		doVGI_forFile(args[1]);
		//CreateFile(args[1].trim(),args[2].trim(),args[3].trim(),args[4].trim());
		
	}
}
