import java.io.*;
import java.util.*;
import java.lang.Runtime;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
// Neha
@SuppressWarnings("serial")
public class Tst extends HttpServlet {
	@SuppressWarnings("static-access")
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		try {
			HttpSession session = req.getSession(true);
			res.setContentType("text/html; charset=UTF-8");
			PrintWriter out = res.getWriter();
			// PrintStream pout = new PrintStream(System.out, true, "UTF-8");

			out.print("<HTML><LINK REL=StyleSheet HREF=style.css TYPE=text/css MEDIA=screen><link rel = stylesheet type = text/css href = newstyle.css><HEAD><TITLE>Web Tagger Output</TITLE><meta http-equiv="
							+ (char) 34
							+ "Content-Type"
							+ (char) 34
							+ " content="
							+ (char) 34
							+ "text/html; charset=utf-8"
							+ (char) 34
							+ "> </HEAD><BODY><!--H1>Hello World</H1-->");
			java.lang.Runtime rt = java.lang.Runtime.getRuntime();
			req.setCharacterEncoding("UTF-8");
			String input = req.getParameter("text");
			// String from = req.getParameter("from_group");
			String tagged = "plain";//req.getParameter("tag_group");
			//String report = req.getParameter("report_group");
			String VGI    = req.getParameter("VGI_group");
			String NGI    = req.getParameter("NGI_group");
			String flagVGI = "false";
			String flagNGI = "false";
			String flagAfterVGI = "false";
			if(VGI.equals("before")){
				flagVGI = "true";
			}
			if(VGI.equals("after")){
				flagAfterVGI = "true";
			}
			if(VGI.equals("both")){
				flagAfterVGI = "true";
				flagVGI = "true";
			}
			if(NGI.equals("yes")){
				flagNGI = "true";
			}
			//out.println("Input : " + input + "\tVGI : " + VGI + "<BR>");
			// START : Neha //
			boolean stemmer=false, morphological=false, tags=false, dm=false, dmtags=false;
			if (req.getParameter("stemmer") != null && req.getParameter("stemmer").trim().length() != 0) {
 				if(req.getParameter("stemmer").trim().equals("Stemmer"))
 					stemmer=true;
  			}
  			if (req.getParameter("morphological") != null && req.getParameter("morphological").trim().length() != 0) {
 				if(req.getParameter("morphological").trim().equals("Morphological"))
 					morphological=true;
  			}
  			if (req.getParameter("tags") != null && req.getParameter("tags").trim().length() != 0) {
  				if(req.getParameter("tags").trim().equals("Tags"))
 					tags=true;
  			}
			if (req.getParameter("dm") != null && req.getParameter("dm").trim().length() != 0) {
 				if(req.getParameter("dm").trim().equals("dm"))
 					dm=true;
  			}
			if (req.getParameter("dmtags") != null && req.getParameter("dmtags").trim().length() != 0) {
 				if(req.getParameter("dmtags").trim().equals("dmtags"))
 					dmtags=true;
  			}

			String sessionid = session.getId().toString();
			 try{
    				(new File("/home/mtech/"+sessionid)).mkdir();
    				}catch (Exception e){//Catch exception if any
      					System.err.println("Error: " + e.getMessage());
    			}			

			String file = null;
			String fileName = null;
			file = "/home/mtech/"+sessionid+"/DataFile";

			//file = "sessionFile";
			fileName = file;
			String toWrite = "";
			String sentence = null;
			//if (tagged.equals("plain")) {
				String tmp = input;// .trim();
				String parts[] = tmp.split("\\.");
				for(int s=0; s<parts.length; s++){
					sentence = parts[s].trim().concat(" .");
					sentence = sentence.replaceAll(" ", "_[ NN ] ");
					sentence = sentence.replaceAll("\\.", "._[ SYM ]");
					toWrite = toWrite + sentence; 
				}
			//} else {
			//	toWrite = input.trim();
			//}
			toWrite = toWrite.replaceAll("\\._[ SYM ]", "._[ SYM ] ");

			BufferedWriter outWrite = new BufferedWriter(new
			OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
			outWrite.write(toWrite);
			outWrite.close();
			// }
			
			String model = "/home/mtech/System/Models/Hindi/model_All_D";
			String modeldm = "/home/mtech/System/Models/Hindi/model_1607_dm";
			String flag = "false";			
			String dir = "/home/mtech/"+sessionid;
			
			if(stemmer||morphological||tags)
			{
				Process p1 = rt.getRuntime().exec(
					"/home/mtech/System/Scripts/tag.sh " + file + " " + model + " " + flag
							+ " " + flagVGI + " " + flagAfterVGI + " " + dir + " " + 	stemmer + " " + morphological + " " + tags);
				p1.waitFor();
			}
			
			String filedm = file + "dm";
			Process p2 = rt.getRuntime().exec("cp " + file + " " + filedm);
			p2.waitFor();

			Process pDM = rt.getRuntime().exec(
					"/home/mtech/System/Scripts/tagDM.sh " + filedm + " " + modeldm + " " + flag
							+ " " + flagVGI + " " + flagAfterVGI + " " + dir + " " + flagNGI + " " + dm + " " + dmtags);
			pDM.waitFor();
			
			if(stemmer){
			out.print("<center><h2>"+ "Stemmer Output </h2></center>"); 
			res.setCharacterEncoding("UTF-8");
			BufferedReader br_stemmer = new BufferedReader(new InputStreamReader(
					new FileInputStream(file+"_new_stemmer"),"UTF8"));
			String line_stemmer = null;
			while ((line_stemmer = br_stemmer.readLine()) != null) 
			{
				out.print(line_stemmer+"<br>");
			}
			br_stemmer.close(); }
			if(morphological){
			out.print("<center><h2>"+ "Morphological Output</h2></center>"); 
			res.setCharacterEncoding("UTF-8");
			BufferedReader br_morpho = new BufferedReader(new InputStreamReader(
					new FileInputStream(file+"_new_morpho"),"UTF8"));
			String line_morpho = null;
			out.print("<center>");
			while ((line_morpho = br_morpho.readLine()) != null) 
			{
				if(!line_morpho.equals(""))
				{
					String abc[] = line_morpho.split("\\+");
					String xyz[] = abc[0].split("=");
					out.println(xyz[1]);
					for(int i=1;i<xyz.length;i++)
					{
						String xyz1[] = abc[i].split("=");
						if(!(xyz1[1].equals("null")))
							out.println("(" + xyz1[1] + ") ");
						
					}
				}
				
			}
			out.print("</center>");
			br_morpho.close(); }
			
			if(tags){	
			out.print("<center><h2>"+ "Tagged Data</h2></center>"); 
			res.setCharacterEncoding("UTF-8");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file + "_final"),"UTF8"));
			String line = null;
			out.print("<center>");
			while ((line = br.readLine()) != null) 
			{
				out.print(line + "<br>");
				
			}
			out.print("</center>");
			br.close();}
			//out.println("Dm : " + dm);
			if(dm){
				
			out.print("<center><h2>"+ "DM Analysis</h2></center>"); 
			res.setCharacterEncoding("UTF-8");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file + "dm_dm"),"UTF8"));
			String line = null;
			out.print("<pre>");
			while ((line = br.readLine()) != null) 
			{
				if((line.length()>5) && (line.substring(0,5).contentEquals("Token")))
					out.print("<br><b>" + line + "</b><br>");
				else
					out.print(line + "<br>");
			}
			out.print("</pre>");
			br.close();
			}
			if(dmtags){
				
			out.print("<center><h2>"+ "Tagged Data using DM Stemmer</h2></center>"); 
			res.setCharacterEncoding("UTF-8");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(file + "dm_final"),"UTF8"));
			String line = null;
			out.print("<center>");
			while ((line = br.readLine()) != null) 
			{
				out.print(line + "<br>");
			}
			out.print("</center>");
			br.close();
			}
						
			out.print("</BODY></HTML>");			
			out.close();
			//Process p2 = rt.getRuntime().exec("rm -R " + dir);
			//p2.waitFor();

		} catch (Exception ex) { // }
			ex.printStackTrace();
		}
	}

	public void doPost(HttpServletRequest req, HttpServletResponse rsp)
			throws ServletException, IOException {
		doGet(req, rsp);
	}

}

