package iitb.cfilt.cpost.vgi;

import java.util.Vector;
import java.util.*;
import iitb.cfilt.cpost.dmstemmer.*;

public class VGpropertiesdm {
	MAResult mar;
	MAResult marNext;
	//VerbGroupIdentifierRuleReader vgir;
	boolean noVmSufixes;	
	boolean noAuxSufixes;	
	boolean noNextVmSufixes;
	boolean noNextAuxSufixes;	
	boolean noVmRoots;	
	boolean noAuxRoots;	
	boolean noNextVmRoots;	
	boolean noNextAuxRoots;
	Vector<String> vmSuffixes;
	Vector<String> auxSuffixes;
	Vector<String> nextVmSuffixes;
	Vector<String> nextAuxSuffixes;
	Vector<String> vmRoots;
	Vector<String> auxRoots;
	Vector<String> nextVmRoots;
	Vector<String> nextAuxRoots;
	boolean hasAdverbialVMSuffix;
	boolean hasAdverbialAuxSuffix;
	boolean hasAdverbialVMSuffixWord;
	boolean hasAdverbialAuxSuffixWord;
	boolean hasMustEndVMSuffix;
	boolean hasMustEndAuxSuffix;
	boolean isQualified;
	boolean isMustEndWord;
	boolean hasNotNullStemmerOutput;
	boolean isNextAuxAllowedAfterThisVM;
	boolean isNextAuxAllowedAfterThisAux;
	boolean isCanEndToken;
	boolean isCoVerb;
	boolean isOnlyParticle;


	public VGpropertiesdm() {
		// TODO Auto-generated constructor stub
		mar = null;
		marNext = null;
		//vgir = new VerbGroupIdentifierRuleReader();
		noVmSufixes = false;	
		noAuxSufixes = false;	
		noNextVmSufixes = false;
		noNextAuxSufixes = false;	
		noVmRoots = false;	
		noAuxRoots = false;	
		noNextVmRoots = false;	
		noNextAuxRoots = false;
		vmSuffixes = new Vector<String>();
		auxSuffixes = new Vector<String>();
		nextVmSuffixes = new Vector<String>();
		nextAuxSuffixes = new Vector<String>();
		vmRoots = new Vector<String>();
		auxRoots = new Vector<String>();
		nextVmRoots = new Vector<String>();
		nextAuxRoots = new Vector<String>();
		hasAdverbialVMSuffix = false;
		hasAdverbialAuxSuffix = false;
		hasAdverbialVMSuffixWord = false;
		hasAdverbialAuxSuffixWord = false;
		hasMustEndVMSuffix = false;
		hasMustEndAuxSuffix = false;
		isQualified = false;
		isMustEndWord = false;
		hasNotNullStemmerOutput = false;
		isNextAuxAllowedAfterThisVM = false;
		isNextAuxAllowedAfterThisAux = false;
		isCanEndToken = false;
		isCoVerb = false;
		isOnlyParticle = false;
	}
	VGpropertiesdm(MAResult mar, MAResult marNext, VerbGroupIdentifierRuleReader vgir){
		this.mar = mar;
		this.marNext = marNext;
		isOnlyParticle = isOnlyParticle(mar);//mat.getAmbiguityScheme().equalsIgnoreCase("particle");
		//this.vgir = vgir;
		vmSuffixes = getVMSuffixes(this.mar);
		noVmSufixes = vmSuffixes.isEmpty();
		if(!noVmSufixes){
			noVmSufixes = true;
			for(int suf = 0; suf<vmSuffixes.size(); suf++ ){
				if(!vmSuffixes.get(suf).isEmpty()){
					noVmSufixes = false;
					break;
				}
				
			}
		}
		auxSuffixes = getAuxSuffixes(this.mar);
		noAuxSufixes = auxSuffixes.isEmpty();
		if(!noAuxSufixes){
			noAuxSufixes = true;
			for(int suf = 0; suf<auxSuffixes.size(); suf++ ){
				if(!auxSuffixes.get(suf).isEmpty()){
					noAuxSufixes = false;
					break;
				}
			}
		}
		nextVmSuffixes = getVMSuffixes(this.marNext);
		noNextVmSufixes = nextVmSuffixes.isEmpty();
		if(!noNextVmSufixes){
			noNextVmSufixes = true;
			for(int suf = 0; suf<nextVmSuffixes.size(); suf++ ){
				if(!nextVmSuffixes.get(suf).isEmpty()){
					noNextVmSufixes = false;
					break;
				}
			}
		}
		nextAuxSuffixes = getAuxSuffixes(this.marNext);
		noNextAuxSufixes = nextAuxSuffixes.isEmpty();
		noNextAuxSufixes = nextAuxSuffixes.isEmpty();
		if(!noNextAuxSufixes){
			noNextAuxSufixes = true;
			for(int suf = 0; suf<nextAuxSuffixes.size(); suf++ ){
				if(!nextAuxSuffixes.get(suf).isEmpty()){
					noNextAuxSufixes = false;
					break;
				}
			}
		}
		vmRoots = getVMRoots(this.mar);
		noVmRoots = vmRoots.isEmpty();
		auxRoots = getAuxRoots(this.mar);
		noAuxRoots = auxRoots.isEmpty();
		nextVmRoots = getVMRoots(this.marNext);
		noNextVmRoots = nextVmRoots.isEmpty();
		nextAuxRoots = getAuxRoots(this.marNext);
		noNextAuxRoots = nextAuxRoots.isEmpty();
		hasNotNullStemmerOutput =  (this.mar.getMorphOutputs().size() > 0);
		hasAdverbialVMSuffix = false;
		for(int suf=0; suf<vmSuffixes.size(); suf++){
			if(vgir.adverbialSuffixes.contains(vmSuffixes.get(suf))){
				hasAdverbialVMSuffix = true;
				break;
			}
		}			
		hasAdverbialAuxSuffix = false;
		for(int suf=0; suf<auxSuffixes.size(); suf++){
			if(vgir.adverbialSuffixes.contains(auxSuffixes.get(suf))){
				hasAdverbialAuxSuffix = true;
				break;
			}
		}
		hasAdverbialVMSuffixWord = false;
		for(int suf=0; suf<vmSuffixes.size(); suf++){
			if(vgir.adverbialSuffix_Words.containsKey(vmSuffixes.get(suf))){
				if(vgir.adverbialSuffix_Words.get(vmSuffixes.get(suf)).contains(this.marNext.getToken())){
					hasAdverbialVMSuffixWord = true;
					break;
				}
			}
		}			
		hasAdverbialAuxSuffixWord = false;
		for(int suf=0; suf<auxSuffixes.size(); suf++){
			if(vgir.adverbialSuffix_Words.containsKey(auxSuffixes.get(suf))){
				if(vgir.adverbialSuffix_Words.get(auxSuffixes.get(suf)).contains(this.marNext.getToken())){
					hasAdverbialAuxSuffixWord = true;
					break;
				}
			}
		}
		hasMustEndVMSuffix = false;
		for(int suf=0; suf<vmSuffixes.size(); suf++){
			if(vgir.mustEndSuffixes.contains(vmSuffixes.get(suf))){
				hasAdverbialVMSuffix = true;
				break;
			}
		}			
		hasMustEndAuxSuffix = false;
		for(int suf=0; suf<auxSuffixes.size(); suf++){
			if(vgir.mustEndSuffixes.contains(auxSuffixes.get(suf))){
				hasAdverbialAuxSuffix = true;
				break;
			}
		}
		isQualified = isVerb(this.mar) || isVerbAux(this.mar);
		/*if(vgir==null){
			System.out.println("here it is");
		}
		System.out.println(this.mat.getToken());
		for(int itr = 0; itr<vgir.mustEndWords.size(); itr++){
			System.out.println(vgir.mustEndWords.get(itr));
		}*/
		if(vgir.mustEndWords!=null && !vgir.mustEndWords.isEmpty()){
			if( vgir.mustEndWords.contains(this.mar.getToken())){
				isMustEndWord = true;
			}
		}
		
		isNextAuxAllowedAfterThisVM = false;
		for(int suf=0; suf<vmSuffixes.size(); suf++){
			String suffix = vmSuffixes.get(suf);
			Set<String> suffixes = vgir.vmSuffixNextTokensHash.keySet();
			Iterator it = suffixes.iterator();
		    while (it.hasNext()) {		        
		        String suffixKey = (String) it.next();
		        //System.out.println("Here : " + suffixKey);
		        if(suffix.equals(suffixKey)){
		        	if(vgir.vmSuffixNextTokensHash.get(suffixKey).contains(this.marNext.getToken())){
						isNextAuxAllowedAfterThisVM = true;
						break;
					}
		        }
		    }
		    if(isNextAuxAllowedAfterThisVM){
		    	break;
		    }
			/*if(vgir.vmSuffixNextTokensHash.containsKey(suffix) &&  vgir.vmSuffixNextTokensHash.get(suffix).contains(this.matNext.getToken())){
				isNextAuxAllowedAfterThisVM = true;
				break;vgir.vmSuffixNextRootsHash.get(suffixKey).get(0).equals(root)
			}*/		    		    
			suffixes = vgir.vmSuffixNextRootsHash.keySet();
			it = suffixes.iterator();
		    while (it.hasNext()) {		        
		        String suffixKey = (String) it.next();		        
		        if(suffix.equals(suffixKey)){//(suffix.endsWith(suffixKey)){
		        	for(int rt = 0; rt<nextAuxRoots.size(); rt++){
						String root = nextAuxRoots.get(rt);
						//System.out.println(suffixKey + "\t" + root + "\t" + vgir.vmSuffixNextRootsHash.get(suffixKey).toString());
						if(vgir.vmSuffixNextRootsHash.get(suffixKey).contains(root.trim())){							
							isNextAuxAllowedAfterThisVM = true;
							break;
						}
					}
		        	if(isNextAuxAllowedAfterThisVM){
						break;
					}
		        }
		        if(isNextAuxAllowedAfterThisVM){
			    	break;
			    }
		    }		    
			/*for(int rt = 0; rt<nextAuxRoots.size(); rt++){
				String root = nextAuxRoots.get(rt);
				if(vgir.vmSuffixNextRootsHash.containsKey(suffix) && vgir.vmSuffixNextRootsHash.get(suffix).contains(root)){
					isNextAuxAllowedAfterThisVM = true;
					break;
				}
			}*/
		}
		
		isNextAuxAllowedAfterThisAux = false;
		for(int rt = 0; rt<auxRoots.size(); rt++){
			HashMap<String, Vector<String>> nextRootHash = vgir.auxSuffixNextRootRuleHash.get(auxRoots.get(rt));
			HashMap<String, Vector<String>> nextTokenHash = vgir.auxSuffixNextTokenRuleHash.get(auxRoots.get(rt));
			String suffix = "";
			if(auxSuffixes.get(rt).isEmpty()){
				suffix = "Null";
			}
			else{
				suffix = auxSuffixes.get(rt);
			}
			boolean is = false; 
			if(nextRootHash != null){
				for(int nrt = 0; nrt<nextAuxRoots.size(); nrt++){
					if(nextRootHash.containsKey(suffix) && nextRootHash.get(suffix).contains(nextAuxRoots.get(nrt))){
						is = true;
						break;
					}
				}
			}
			if(nextTokenHash != null){				
				if(nextTokenHash.containsKey(suffix) && nextTokenHash.get(suffix).contains(marNext.getToken())){
					is = true;
				}
			}
			if(is){
				isNextAuxAllowedAfterThisAux = true;
				break;
			}
		}	
		
		isCanEndToken = false;
		for(int rt = 0; rt<auxRoots.size(); rt++){
			HashMap<String, Vector<String>> nextRootHash = vgir.auxSuffixNextRootRuleHash.get(auxRoots.get(rt));
			HashMap<String, Vector<String>> nextTokenHash = vgir.auxSuffixNextTokenRuleHash.get(auxRoots.get(rt));
			String suffix = "";
			if(auxSuffixes.get(rt).isEmpty()){
				suffix = "Null";
			}
			else{
				suffix = auxSuffixes.get(rt);
			}
			if(nextTokenHash != null && nextTokenHash.containsKey(suffix) && nextTokenHash.get(suffix).contains("Null")){
				isCanEndToken = true;
				break;
			}
		}
		
		isCoVerb = false;
		if(vgir.coVerbsHash.containsKey(mar.getToken())){
			for(int rt=0; rt<nextVmRoots.size(); rt++){
				if(vgir.coVerbsHash.get(mar.getToken()).contains(nextVmRoots.get(rt) + "ना")){
					isCoVerb = true;
					break;
				}
			}
			if(!isCoVerb){
				for(int rt=0; rt<nextAuxRoots.size(); rt++){
					if(vgir.coVerbsHash.get(mar.getToken()).contains(nextAuxRoots.get(rt) + "ना")){
						isCoVerb = true;
						break;
					}
				}
			}
		}
	}
	
	boolean hasSuffix(StemmerResult srt) {
		boolean retval = false;
		retval = !(srt.getSuffix().isEmpty());
		return retval;
	}

	boolean isVerb(StemmerResult srt) {
		boolean retval = false;
		retval = srt.getCategory().equals("verb");
		return retval;
	}

	boolean isVerb(MAResult mar) {
		boolean retval = false;
		retval = mar.getAmbiguityScheme().contains("*verb*") || mar.getAmbiguityScheme().contains("*verb") 
			|| mar.getAmbiguityScheme().contains("verb*") || mar.getAmbiguityScheme().equals("verb")
			|| mar.getAmbiguityScheme().startsWith("verb*");
		return retval;
	}

	boolean isOnlyParticle(MAResult mar) {
		boolean retval = false;
		retval = mar.getAmbiguityScheme().equals("particle");
		return retval;
	}
	
	boolean isVerbAux(MAResult mar) {
		boolean retval = false;
		retval = mar.getAmbiguityScheme().contains("verb_aux");
		return retval;
	}

	boolean isVerbAux(StemmerResult srt) {
		boolean retval = false;
		retval = srt.getCategory().equals("verb_aux");
		return retval;
	}
	
	boolean isOnlyVM(MAResult mar) {
		boolean retval = false;
		retval = mar.getAmbiguityScheme().equals("verb");
		return retval;
	}

	boolean isOnlyVAux(MAResult mar) {
		boolean retval = false;
		retval = mar.getAmbiguityScheme().equals("verb_aux");
		return retval;
	}
	
	boolean isOnlyVerb(MAResult mar) {
		boolean retval = false;
		retval = isOnlyVM(mar) || isOnlyVAux(mar) || mar.getAmbiguityScheme().equals("verb*verb_aux") || mar.getAmbiguityScheme().equals("verb_aux*verb");
		return retval;
	}
	
	boolean agree(StemmerResult srt, MAResult mar) {
		boolean retval = false;
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getSuffix().contentEquals(
					srt.getSuffix())) {
				retval = true;
				break;
			}
		}
		return retval;
	}
	
	Vector<String> getAuxRoots(MAResult mar){
		Vector <String> retval= new Vector<String>();
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb_aux")) {
				retval.add(mar.getMorphOutputs().get(i).getStemmerResult().getRoot());
				//break;
			}
		}
		return retval;
	}
	
	Vector <String> getVMRoots(MAResult mar){
		Vector <String> retval= new Vector<String>();
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb")) {
				retval.add(mar.getMorphOutputs().get(i).getStemmerResult().getRoot());
				//break;
			}
		}
		return retval;
	}
	
	Vector <String> getAuxSuffixes(MAResult mar){
		Vector <String> retval= new Vector<String>();
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb_aux")) {
				if(!mar.getMorphOutputs().get(i).getStemmerResult().getSuffix().contentEquals("Null")){
					retval.add(mar.getMorphOutputs().get(i).getStemmerResult().getSuffix());
				}
				else{
					retval.add("");
				}				
			}
		}
		return retval;
	}
	
	Vector<String> getVMSuffixes(MAResult mar){
		Vector <String> retval= new Vector<String>();
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb")) {
				if(!mar.getMorphOutputs().get(i).getStemmerResult().getSuffix().contentEquals("Null")){
					retval.add(mar.getMorphOutputs().get(i).getStemmerResult().getSuffix());
					//break;
				}	
			}
		}
		return retval;
	}
	
	String getAuxRoot(MAResult mar){
		String retval= "";
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb_aux")) {
				retval = mar.getMorphOutputs().get(i).getStemmerResult().getRoot();
				break;
			}
		}
		return retval;
	}
	
	String getVMRoot(MAResult mar){
		String retval= "";
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb")) {
				retval = mar.getMorphOutputs().get(i).getStemmerResult().getRoot();
				break;
			}
		}
		return retval;
	}
	
	String getAuxSuffix(MAResult mar){
		String retval= "";
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb_aux")) {
				if(!mar.getMorphOutputs().get(i).getStemmerResult().getSuffix().contentEquals("Null")){
					retval = mar.getMorphOutputs().get(i).getStemmerResult().getSuffix();
					break;
				}
			}
		}
		return retval;
	}
	
	String getVMSuffix(MAResult mar){
		String retval= "";
		for (int i = 0; i < mar.getMorphOutputs().size(); i++) {
			if (mar.getMorphOutputs().get(i).getStemmerResult().getCategory().equals("verb")) {
				if(!mar.getMorphOutputs().get(i).getStemmerResult().getSuffix().contentEquals("Null")){
					retval = mar.getMorphOutputs().get(i).getStemmerResult().getSuffix();
					break;
				}	
			}
		}
		return retval;
	}
	
	

}
