/**
 * 
 */
package kr.ac.kaist.swrc.jhannanum.module.tagger;

/**
 * @author Sangwon Park (hudoni@world.kaist.ac.kr), CILab, SWRC, Kaist
 *
 */
public class PhraseTag {
	private static String temps;
	private static char[] tag = new char[5];

	public static String phtag(String str) {
		String res = "";

		String f1 = "";
		String f2 = "";
		String f3 = "";
		String f4 = "";
		String e1 = "";
		String e2 = "";
		String e3 = "";
		String e4 = "";

		int i=0,j=0,n=0;

		tag[0] = '.';
		tag[1] = '.';
		tag[2] = '\0';

		try {
			while (i<str.length() && str.charAt(i) == ' ')
				i++;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while (i < str.length()) {
			if (str.charAt(i) == ' ' || str.charAt(i) == '\n') {
				if(i > 0 && str.charAt(i-1) != ' ' ){
					if( n == 0 ) f1 = temps;
					else if( n == 1 ) f2 = temps;
					else if( n == 2 ) f3 = temps;
					else if( n == 3 ) f4 = temps;
					if( n > 2)
						e4 = e3; 
					if( n > 1) 
						e3 = e2; 
					if( n > 0) 
						e2 = e1; 
					e1 = temps;
					n++;
					temps = "";
				}
			} else {
				temps += str.charAt(i);
			}
			i++;
		}

		/* 내가 추가한 부분?*/
		if (i == str.length()) {
			if(i > 0 && str.charAt(i-1) != ' ') {
				if( n == 0 ) f1 = temps;
				else if( n == 1 ) f2 = temps;
				else if( n == 2 ) f3 = temps;
				else if( n == 3 ) f4 = temps;
				if( n > 2)
					e4 = e3;
				if( n > 1) 
					e3 = e2;
				if( n > 0) 
					e2 = e1;
				e1 = temps;
				n++;
				temps = "";
			}
		}


		if (f1.startsWith( "ma") ) {
			if (0<f2.length() && f2.charAt(0) == 'p')
				tag[0] = 'P';
			else if(0<f2.length() &&  f2.charAt(0) == 'x')
				tag[0] = 'P';
			else if( f2.equals("jcp") )
				tag[0] = 'P';
			else 
				tag[0] = 'A';
		}	
		else if(0< f1.length() && f1.charAt(0) == 'e'){	
			if( 2<f1.length() && (f1.charAt(2) == 'c' || f1.charAt(2) == 's'))
				tag[0] = 'C';
		}
		else if( 0<f1.length() && f1.charAt(0) == 'f'){
			tag[0] = 'N';
			/*
			   if( (strcmp(f1,"f")==0 && strcmp(f2,"xsn")==0)
			   ||(strcmp(f1,"f")==0 && strncmp(f2,"nc",2)==0)
			   ||(strcmp(f1,"mmd")==0&&strncmp(f2,"nc",2)==0)  )
			   tag[0] = 'N'; 
			   else if( strcmp(f1,"f")==0 && strcmp(f1,"etm")==0)
			   tag[0] = 'M';
			 */
		}
		else if( 0<f1.length() && f1.charAt(0) == 'i'){
			if( 0<f2.length() && f2.charAt(0) == 'j' )
				tag[0] = 'N';
			else 
				tag[0] = 'I';
		}
		else if( 1<f1.length() && f1.charAt(0) == 'm' && f1.charAt(1) != 'a'){
			if( 0<e1.length() && e1.charAt(0) == 'j' )
				tag[0] = 'N'; 
			else if( 0<f2.length() && f2.charAt(0) == 'n')
				tag[0] = 'N';
			else if( 0<f2.length() && f2.charAt(0) == 'p')
				tag[0] = 'P';
			else tag[0] = 'M';
		}
		else if( 0<f1.length() && f1.charAt(0) == 'n'){
			if( 0<f2.length() && f2.charAt(0) == 'x' ){
				if( 2<f2.length() && (f2.charAt(2) == 'v' || f2.charAt(2) == 'm')){
					if( (2<f3.length() && f3.charAt(2) == 'n') || (2<f4.length() && f4.charAt(2) == 'n' )) /* exn */
						tag[0] = 'N';
					else 
						tag[0] = 'P';
				}
				else if( 2<f2.length() && f2.charAt(2) == 'n')
					tag[0] = 'N';
			}
			else if( 0<f2.length() && f2.charAt(0) == 'p'){
				if( (2<f3.length() && f3.charAt(2) == 'n') || (2<f4.length() && f4.charAt(2) == 'n' ))
					tag[0] = 'N';
				else
					tag[0] = 'P';
			}
			else
				tag[0] = 'N';
		}
		else if( 0<f1.length() && f1.charAt(0) == 'p') {
			if(f2.equals("xsa"))
				tag[0] = 'A';
			else if( !f2.equals( "etn") || (0<f3.length() && f3.charAt(0) == 'n')) /* for exm exn */
				tag[0] = 'N';
			else 
				tag[0] = 'P';
		}
		else if( 0<f1.length() && f1.charAt(0) == 's') {
			if( f1.equals("su")  || (f2.length() > 0 && f2.charAt(0) == 'j')) 
				tag[0] = 'N';
			else if( (f2.length() > 0 && f2.charAt(0) == 'n') || (0<e1.length() && e1.charAt(0) == 'j'))
				tag[0] = 'N';
			else tag[0] = 'S';

			if( 1<f1.length() && f1.charAt(1) == 'f' || (f2.length() > 0 && f2.charAt(0) == 's'))
				tag[1] = 'F';
		}
		else if( f1.equals("xsn") || f1.equals("xp") )
			tag[0] = 'N';

		if( 0<e1.length() && e1.charAt(0) == 'e'){
			if( e1.equals("ecc") || e1.equals("ecs") 
					|| e1.equals("ecx") )
				tag[1] = 'C';
			else if( e1.equals("ef") )
				tag[1] = 'F';
			else if( e1.equals("etm") ) 
				tag[1] = 'M';
			else if( e1.equals("etn") )
				tag[1] = 'N';
		}
		else if( 0<e1.length() && e1.charAt(0) == 'j' ){
			/*
			   if( 0<f1.length() && f1.charAt(0) == 'a') 
			   tag[0] = 'N';
			 */
			if( e1.equals("jcv") ) 
				tag[0] = 'I';
			else if( e1.startsWith("jx") ){
				if( tag[0] == 'A' ) 
					tag[1] = 'J';
				else  
					tag[1] = 'X';
			}
			else if( e1.equals("jcj") ){
				if( tag[0] == 'A' ) 
					tag[1] = 'J';
				else  
					tag[1] = 'Y';
			}
			else if( e1.equals("jca") ) 
				tag[1] = 'A';
			else if( e1.equals("jcm") ){
				if( tag[0] == 'A' ) 
					tag[1] = 'J';
				else  
					tag[1] = 'M';
			}
			else if( e1.startsWith("jc") )
				tag[1] = 'J';
		}
		else if( 0<e1.length() && (e1.charAt(0) == 'm' && e1.charAt(1) != 'a' ) )
			tag[1] = 'M';
		else if( 0<e1.length() && e1.charAt(0) == 'n')
			tag[0] = 'N';
		else if( e1.equals("mag") )
			tag[1] = 'A';
		else if( 0<e1.length() && e1.charAt(0) == 'x' ){
			if( e1.equals("xsa") )
				tag[1] = 'A';
		}	

		if( tag[0] == tag[1] ) tag[1] = '.';
		else if( tag[0] == '.' ){	
			tag[0] = tag[1];
			tag[1] = '.';
		}

		tag[2] = '\0';
		/*******************************************
		  새로운 Rule 추가 .... 후처리.. 94 9 9 
		 *****************************************/
		if( tag[0] == 'A' ){
			if( tag[1] == 'M')
				tag[0] = 'N';
		}
		else if( tag[0] == 'M' ){
			if( tag[1] == 'A' )
				tag[0] = 'A';
			else if( tag[1] == 'F')
				tag[0] = 'N';
			else if( tag[1] == 'C')
				tag[0] = 'N';
		}
		else if( tag[0] == 'I' ){
			if( tag[1] == 'M' || tag[1] == 'J')
				tag[0] = 'N';
			else if( tag[1] == 'C')
				tag[0] = 'P';
			else if( tag[1] == 'F')
				tag[0] = 'N';
		}

		/*        if(PHRASE_TAG == 0 && strcmp(tag,"SS") != 0 && strcmp(tag,"SF") != 0){
				  tag[0] = 'A';
				  tag[1] = 'C';
				  }  PHRASE_TAG를 사용하는 부분은 제외 */

		if( tag[0] == tag[1] )
			tag[1] = '.';
		
		for (int idx = 0; idx < tag.length; idx++) {
			res += tag[idx];
		}

		//
		//		//printf("f1:%s f2:%s f3:%s f4:%s e1:%s e2:%s e3:%s e4:%s\n", f1, f2, f3, f4, e1, e2, e3, e4);
		//
		//		return(tag);
		return res;
	}
}