package iitb.cfilt.cpost.vgi;

import java.util.*;
import java.io.*;
import iitb.cfilt.cpost.ConfigReader;

public class VerbGroupIdentifierRuleReader {
	
	public HashMap<String,Vector<String>> vghash = new HashMap<String,Vector<String>>(); // vghash is not being used now in dm implementation
	public HashMap<String,Vector<String>> auxhash = new HashMap<String,Vector<String>>();
	public HashMap<String,Vector<String>> vmSuffixNextTokensHash = new HashMap<String,Vector<String>>();
	public HashMap<String,Vector<String>> coVerbsHash = new HashMap<String,Vector<String>>();
	public HashMap<String,Vector<String>> vmSuffixNextRootsHash = new HashMap<String,Vector<String>>();
	public Vector<String> mustEndSuffixes = new Vector<String>();
	public Vector<String> mustEndWords = new Vector<String>();
	public Vector<String> adverbialWords = new Vector<String>();
	public Vector<String> adverbialSuffixes = new Vector<String>();
	public HashMap<String, String> adverbialSuffix_Words = new HashMap<String, String>();
	public Vector<String> tenseCases = new Vector<String>();
	public Vector<String> possPRPs = new Vector<String>();
	public HashMap<String, HashMap<String, Vector<String>>> auxSuffixNextTokenRuleHash = new HashMap<String, HashMap<String, Vector<String>>>();
	public HashMap<String, HashMap<String, Vector<String>>> auxSuffixNextRootRuleHash = new HashMap<String, HashMap<String, Vector<String>>>();
	
	public VerbGroupIdentifierRuleReader()
	{
		//System.out.println("working..");
		// vghash is not being used now in dm implementation
		try
		{
			BufferedReader bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.VGRuleFile")), "UTF8"));
			String line = "";
	
			while (line != null)
			{				
				line = bsrr.readLine();
				if(line != null)
				{
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//"))
					{
						String lineComponents[] = line.split(",");
						if(lineComponents.length == 2)
						{
							String verbSuffix = lineComponents[0].trim();
							String nextVerb = lineComponents[1].trim();
							
							if(vghash.containsKey(verbSuffix))
							{
								Vector<String> tmp = vghash.get(verbSuffix);
								tmp.add(nextVerb);
								vghash.put(verbSuffix,tmp);
							}
							else
							{
								Vector<String> tmp = new Vector<String>();
								tmp.add(nextVerb);
								vghash.put(verbSuffix,tmp);
							}
						}
					}
				}
			}
			
			bsrr.close();
			
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.CoVerbsFile")), "UTF8"));
			line = "";
	
			while (line != null)
			{				
				line = bsrr.readLine();
				if(line != null)
				{
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//"))
					{
						String lineComponents[] = line.split(" ");
						if(lineComponents.length == 2)
						{
							String first = lineComponents[0].trim();
							String second = lineComponents[1].trim();
							if(second.endsWith("ना")){
								second = second.replaceAll("ना$", "");
							}
							
							if(coVerbsHash.containsKey(first))
							{
								Vector<String> tmp = coVerbsHash.get(first);
								tmp.add(second);
								coVerbsHash.put(first,tmp);
							}
							else
							{
								Vector<String> tmp = new Vector<String>();
								tmp.add(second);
								coVerbsHash.put(first,tmp);
							}
						}
					}
				}
			}
			bsrr.close();
			
			boolean next_is_token = true;
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.VMSuffixRuleFile")), "UTF8"));
			//System.out.println("Reading my file..");
			line="";
			while (line != null)
			{
				//System.out.println("Reading my line..");
				line = bsrr.readLine();
				if(line != null)
				{
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//"))
					{
						//System.out.println(line);
						if(line.equalsIgnoreCase("Next-Tokens")){					
							next_is_token = true;
						}
						else if(line.equalsIgnoreCase("Next-Roots")){
							next_is_token = false;
						}
						else{
							String lineComponents[] = line.split(",");
							//System.out.println(lineComponents.length);
							if(lineComponents.length == 2)
							{								
								String verbSuffix = lineComponents[0].trim();
								String[] nextVerbs = lineComponents[1].trim().split(" ");
								//System.out.println(verbSuffix + "," + lineComponents[1]);
								if(next_is_token){
									if(vmSuffixNextTokensHash.containsKey(verbSuffix))
									{
										Vector<String> tmp = vmSuffixNextTokensHash.get(verbSuffix);
										for(int i=0; i<nextVerbs.length; i++){
											if(nextVerbs[i]!="" && nextVerbs[i]!=" ")
												tmp.add(nextVerbs[i]);
										}
										vmSuffixNextTokensHash.put(verbSuffix,tmp);
									}
									else
									{
										Vector<String> tmp = new Vector<String>();
										for(int i=0; i<nextVerbs.length; i++){
											if(nextVerbs[i]!="" && nextVerbs[i]!=" ")
												tmp.add(nextVerbs[i]);
										}
										vmSuffixNextTokensHash.put(verbSuffix,tmp);
									}
								}
								else{
									if(vmSuffixNextRootsHash.containsKey(verbSuffix))
									{
										Vector<String> tmp = vmSuffixNextRootsHash.get(verbSuffix);
										for(int i=0; i<nextVerbs.length; i++){
											if(nextVerbs[i]!="" && nextVerbs[i]!=" ")
												tmp.add(nextVerbs[i]);
										}
										vmSuffixNextRootsHash.put(verbSuffix,tmp);
									}
									else
									{
										Vector<String> tmp = new Vector<String>();
										for(int i=0; i<nextVerbs.length; i++){
											if(nextVerbs[i]!="" && nextVerbs[i]!=" ")
												tmp.add(nextVerbs[i]);
										}
										vmSuffixNextRootsHash.put(verbSuffix,tmp);
									}
								}
							}
						}
					}
				}
			}
			
			bsrr.close();
			
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.VGMustEndFile")), "UTF8"));
			line = "";
			boolean suffix = true;
			while (line != null){
				line = bsrr.readLine();
				if(line != null){
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//")){
						if(line.equalsIgnoreCase("Suffixes")){
							suffix = true;
						}
						else if(line.equalsIgnoreCase("Words")){
							suffix = false;
						}
						else{
							if(suffix){
								mustEndSuffixes.add(line);
							}
							else{
								//System.out.println("Added :" + line + ": Done");
								mustEndWords.add(line);
							}
						}
					}
				}
			}
			
			
			bsrr.close();
			
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.AuxSuffixNextAuxFile")), "UTF8"));
			line = "";
			next_is_token = true;
			while (line != null){
				line = bsrr.readLine();
				if(line != null){
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//")){						
						if(line.equalsIgnoreCase("Next-is-Token")){		
							next_is_token = true;
						}
						else if(line.equalsIgnoreCase("Next-is-Root")){
							next_is_token = false;
						}
						else{
							HashMap<String, HashMap <String, Vector<String>>> tmpHash;
							if(next_is_token){
								tmpHash = auxSuffixNextTokenRuleHash;
							}
							else{
								tmpHash = auxSuffixNextRootRuleHash;
							}
							String[] lineComponents = line.trim().split("/");
							String auxList = lineComponents[0].trim();
							String suffixList = lineComponents[1].trim();
							String nextAuxList = "Null";
							//System.out.println(lineComponents.length);			
							boolean nextAux = false;
							if(lineComponents.length == 3){
								nextAux = true;
								nextAuxList = lineComponents[2].trim();
							}
							//System.out.println(auxList + "\t" + suffixList + "\t" + nextAuxList);
							String[] aux = auxList.trim().split(",");
							String[] suffixes = suffixList.trim().split(",");
							Vector<String> next_aux = new Vector<String>();
							if(nextAux){
								String nextaux[] = nextAuxList.trim().split(",");
								for(int k=0; k<nextaux.length; k++){
									if(nextaux[k]!="" && nextaux[k].trim()!=" ")
										next_aux.add(nextaux[k].trim());
								}
							}
							for(int i = 0; i < aux.length; i++){
								String curAux = aux[i].trim();
								
								if(tmpHash.containsKey(curAux)){
									HashMap<String, Vector<String>> aux_suffixRuleHash = tmpHash.get(curAux);
									for(int j = 0; j < suffixes.length; j++){
										String curSuffix = suffixes[j].trim();
										if(curSuffix!="" && curSuffix!=" "){
											if(aux_suffixRuleHash.containsKey(curSuffix)){						
												Vector<String> curNextAuxList = aux_suffixRuleHash.get(curSuffix);						
												curNextAuxList.addAll(next_aux);					
											}
											else{
												aux_suffixRuleHash.put(curSuffix, next_aux);
											}
										}
									}
								}
								else{
									HashMap<String, Vector<String>> aux_suffixRuleHash = new HashMap<String, Vector<String>>();
									for(int j = 0; j < suffixes.length; j++){
										String curSuffix = suffixes[j].trim();
										if(curSuffix!="" && curSuffix!=" "){
											if(aux_suffixRuleHash.containsKey(curSuffix)){						
												Vector<String> curNextAuxList = aux_suffixRuleHash.get(curSuffix);						
												curNextAuxList.addAll(next_aux);					
											}
											else{
												aux_suffixRuleHash.put(curSuffix, next_aux);
											}
										}					
									}
									tmpHash.put(curAux, aux_suffixRuleHash);
								}
							}
						}
					}
				}
			}	
			
			bsrr.close();
			
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.TenseCasesFile")), "UTF8"));
			line = "";
			//boolean suffix = true;
			while (line != null){
				line = bsrr.readLine();
				if(line != null){
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//")){
							tenseCases.add(line);
					}
				}
			}
			
			bsrr.close();
			
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.PossPRPsFile")), "UTF8"));
			line = "";
			//boolean suffix = true;
			while (line != null){
				line = bsrr.readLine();
				if(line != null){
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//")){
						//System.out.println("Adding .. " + line);
						possPRPs.add(line);
					}
				}
			}	
			
			
			bsrr.close();
			bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.VGAdverbials")), "UTF8"));
			line = "";
			int ptrn=0;
			while (line != null){
				line = bsrr.readLine();
				if(line != null){
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//")){
						if(line.equalsIgnoreCase("Suffix-Word")){
							ptrn = 0;
						}
						if(line.equalsIgnoreCase("Suffixes")){
							ptrn = 1;
						}
						else if(line.equalsIgnoreCase("Words")){
							ptrn = 2;
						}
						else{
							switch( ptrn ){
							case 0:
								//adverbialSuffix_Words.add(line);
								String lineComponents[] = line.split(",");
								if(lineComponents.length == 2)
								{
									String verbSuffix = lineComponents[0].trim();
									String nextVerb = lineComponents[1].trim();
									adverbialSuffix_Words.put(verbSuffix, nextVerb);
								}
								break;
							case 1:						
								adverbialSuffixes.add(line);
								break;
							case 2:
								adverbialWords.add(line);
								break;
							}
						}
					}
				}
			}
						
			bsrr.close();
			
			// auxhash is not being used now in dm implementation
			/*bsrr = new BufferedReader(new InputStreamReader(new FileInputStream(ConfigReader.get("VGI.AuxFile")), "UTF8"));
			line = "";
			
			while (line != null)
			{
				line = bsrr.readLine();
				if(line != null)
				{
					line = line.trim();
					if(line.length()!=0 && !line.startsWith("//"))
					{
						String lineComponents[] = line.split(",");
						String aux = lineComponents[0].trim();
	//					if(aux == "null"){
		//					aux = "";
			//			}
						String nextAux = lineComponents[1].trim();
						
						if(auxhash.containsKey(aux))
						{
							Vector<String> tmp = auxhash.get(aux);
							tmp.add(nextAux);
							auxhash.put(aux,tmp);
						}
						else
						{
							Vector<String> tmp = new Vector<String>();
							tmp.add(nextAux);
							auxhash.put(aux,tmp);
						}
					}
				}
			}*/
		}
		catch(Exception e)
		{
			System.out.println(e.toString());
			e.printStackTrace();
		}
	}
	
	public Vector<String> getNextVerbs(String verbSuffix)
	{
		Vector<String> retval = new Vector<String>();
		
		retval = vghash.get(verbSuffix);
		
		return(retval);
	}
	
	public Vector<String> getNextAuxVerbs(String rootVerb)
	{
		Vector<String> retval = new Vector<String>();
		
		retval = auxhash.get(rootVerb);
		
		return(retval);
	}
	
	public String getNextAdverbial(String adverbSuffix)
	{
		String retval = new String();
		
		retval = adverbialSuffix_Words.get(adverbSuffix);
		
		return(retval);
	}
	
	public Vector<String> getNextTokensForMainSuffix(String mainSuffix){
		Vector<String> retval = new Vector<String>();
		
		retval = vmSuffixNextTokensHash.get(mainSuffix);
		
		return(retval);
	}
	
	public Vector<String> getNextRootsForMainSuffix(String mainSuffix){
		Vector<String> retval = new Vector<String>();
		
		retval = vmSuffixNextRootsHash.get(mainSuffix);
		
		return(retval);
	}
	
	public boolean hasSuffix(String verbSuffix)
	{
		return(vghash.containsKey(verbSuffix.trim()));
	}
	
	public boolean hasAdverbialSuffix_Word(String adverbSuffix)
	{
		return(adverbialSuffix_Words.containsKey(adverbSuffix.trim()));
	}
	
	public boolean hasRootVerb(String rootVerb)
	{
		return(auxhash.containsKey(rootVerb.trim()));
	}
	public boolean hasMainSuffixForNextTokens(String mainSufix){
		return vmSuffixNextTokensHash.containsKey(mainSufix.trim());
	}
	public boolean hasMainSuffixForNextRoots(String mainSufix){
		return vmSuffixNextRootsHash.containsKey(mainSufix.trim());
	}
	
	
	public static void main(String args[]) {
		ConfigReader.read(args[0]);
		//VerbGroupIdentifierRuleReader vgir = new VerbGroupIdentifierRuleReader();
		String str = "true";
		//boolean b = str.;
		
	}
	
	
}
