import edu.stanford.nlp.tagger.maxent.MaxentTagger;
import java.util.*;

public class StanfordPOSTagger {
	static MaxentTagger taggerInstance = null;
	String [] tokens = null;
	String [] tags = null;
	int tokenCount = 0;
	public StanfordPOSTagger(){
		try{
			taggerInstance = new MaxentTagger("./StanfordTaggerModels/left3words-wsj-0-18.tagger");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public void tagInput(String s){
		try{
			
		String taggedInput =  taggerInstance.tagString(s);
		
		//Parse the tags and tokens
		String [] tmp = taggedInput.split(" ");
		tokenCount = tmp.length;
		tokens = new String [tokenCount];
		tags = new String [tokenCount];
		for (int i=0; i<tokenCount; i++){
			String [] tmp2 = tmp[i].split("/");
			tokens[i] = tmp2[0];
			tags[i] = tmp2[1];
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static ArrayList<String> getNounTokens(String s){
		// retreives specific tokens NN, NNS, NNP, NNPS
		ArrayList<String> tokens = new ArrayList<String>();
		String taggedInput =  taggerInstance.tagString(s);
		//System.out.println(taggedInput);
		//Parse the tags and tokens
		String [] tmp = taggedInput.split(" ");
		for(String i : tmp)
			if(i.endsWith("/NN")||i.endsWith("/NNS")||i.endsWith("/NNP")||i.endsWith("/NNPS"))
				tokens.add(i.split("/")[0]);
		return tokens;
	}
	public static ArrayList<String> getAdjectiveTokens(String s){
		// retreives specific tokens JJ, JJR, JJS
		ArrayList<String> tokens = new ArrayList<String>();
		String taggedInput =  taggerInstance.tagString(s);
		//System.out.println(taggedInput);
		//Parse the tags and tokens
		String [] tmp = taggedInput.split(" ");
		for(String i : tmp)
			if(i.endsWith("/JJ")||i.endsWith("/JJR")||i.endsWith("/JJS"))
				tokens.add(i.split("/")[0]);
		return tokens;
	}
	
	public static ArrayList<String> getVerbTokens(String s){
		// retreives specific tokens JJ, JJR, JJS
		ArrayList<String> tokens = new ArrayList<String>();
		String taggedInput =  taggerInstance.tagString(s);
		//System.out.println(taggedInput);
		//Parse the tags and tokens
		String [] tmp = taggedInput.split(" ");
		for(String i : tmp)
			if(i.endsWith("/VB")||i.endsWith("/VBD")||i.endsWith("/VBG")||i.endsWith("/VBN")||i.endsWith("/VBP")||i.endsWith("/VBZ"))
				tokens.add(i.split("/")[0]);
		return tokens;
	}
	
	public String [] getTokens(){
		return tokens;
	}

	public String [] getTags(){
		return tags;
	}
	public static String getTagsinSequence(String s){
		String result = "";
		String taggedInput =  taggerInstance.tagString(s);
		for(String token : taggedInput.split("[ ]+"))
			result += token.split("/")[1] + " ";
		return result.trim();
	}
	
	public static String printStringArray(String [] array, String separator){
		String result = "";
		for(String s : array)
			result += s + separator;
		return result.substring(0, result.length()-1);
	}
	
	public static void main(String [] args){
			StanfordPOSTagger test = new StanfordPOSTagger();
			/*
			test.tagInput("I am  very good! The chicken was awesome man. Much better than the best kentucky");
			System.out.println(printStringArray(test.getTokens(), "|"));
			System.out.println(printStringArray(test.getTags(), "_"));
			
			System.out.println(test.getSpecificTokens("the swimming pool was nice"));
			System.out.println(test.getSpecificTokens("nice porch and backyard. Also nice men"));
			System.out.println(test.getSpecificTokens("staff were good. matresses were also spongy"));
			System.out.println(test.getSpecificTokens("sheriton was awesome in cleanliness. the curtains were too good"));
			*/
			String s = "I could never hold connection steady for a few hours .";
			test.tagInput("s");
			System.out.println(s);
			System.out.println(getTagsinSequence(s));
			
			System.out.println(test.getAdjectiveTokens("nice porch and backyard. Also nice men"));
			System.out.println(test.getAdjectiveTokens("staff were good. matresses were also spongy"));
			System.out.println(test.getVerbTokens("sheriton was awesome in cleanliness. the curtains were too good"));
			
		
	}
}
