function convertQPquery(query){
	var qtext = query;
	var exp;

	qtext = cleanQuery(qtext);

	// 연산자 앞 뒤로 공백을 추가
	qtext = qtext.replace(/[&]/g, " & ");
	qtext = qtext.replace(/[|]/g, " | ");
	qtext = qtext.replace(/[!]/g, " ! ");

	exp = /[\s]/g;
	var arr_qtext = qtext.split(exp);

	qtext = "";
	for( var i = 0 ; i < arr_qtext.length ; i++ ){
		if( arr_qtext[i] == "!" ){
			qtext += "<not>";
		}
		else if( arr_qtext[i] == "&" ){
			qtext += "<and>";
		}
		else if( arr_qtext[i] == "|" ){
			qtext +=  "<or>";
		}
		else if( arr_qtext[i] != "" ){
			qtext = qtext + "`" + arr_qtext[i] + "`";
		}
	}

	return qtext;
}

function convertQPBigramQuery(query){
	var qtext = query;
	var exp;

	qtext = cleanQuery(qtext);

	// 연산자 앞 뒤로 공백을 추가
	qtext = qtext.replace(/[&]/g, " & ");
	qtext = qtext.replace(/[|]/g, " | ");
	qtext = qtext.replace(/[!]/g, " ! ");

	exp = /[\s]/g;
	var arr_qtext = qtext.split(exp);

	qtext = "";
	for( var i = 0 ; i < arr_qtext.length ; i++ ){
		if( arr_qtext[i] == "!" ){
			qtext += "<not>";
		}
		else if( arr_qtext[i] == "&" ){
			qtext += "<and>";
		}
		else if( arr_qtext[i] == "|" ){
			qtext +=  "<or>";
		}
		else if( arr_qtext[i] != "" ){
			qtext = qtext + "`<bigram>" + arr_qtext[i] + "`";
		}
	}

	return qtext;
}

function cleanUpQuery(query) {
	var qtext = query;
	var exp;

	// 특수문자를 기준으로 split 하여 분리한 다음
	// 분리된 단어가 and or not 인지 판별하여 기호로 치환하여 나중에 한꺼번에 처리한다
	exp = /[.!|!/\-+\'\"~`?*&^%$#@,=;:\\\]\[}{)(\s><_]/g ;
	var arr_qtext = qtext.split(exp);

	qtext = "";
	for (var i = 0; i < arr_qtext.length; i++) {
		arr_qtext[i] = arr_qtext[i].replace(/(^or$|^and$|^not$)/i, " ");

		// 다시 하나의 string으로 연결한다.
		qtext += arr_qtext[i] + " ";
	}

	// 다시 중복 스페이스를 압축
	exp = /[\s]+/g;
	qtext = qtext.replace(exp, " ");

	// 마지막 스페이스 제거
	exp = /[\s]+$/g;
	qtext = qtext.replace(exp, "");

	return qtext;
}

function cleanQuery(query) {
	var qtext = query;
	var exp;

	// 특수문자를 기준으로 split 하여 분리한 다음
	// 분리된 단어가 and or not 인지 판별하여 기호로 치환하여 나중에 한꺼번에 처리한다
	exp = /[.!/\-+\'\"~`?*^%$#@,=;:\\\]\[}{)(\s><_]/g ;
	var arr_qtext = qtext.split(exp);

	qtext = "";
	for (var i = 0; i < arr_qtext.length; i++) {
		arr_qtext[i] = arr_qtext[i].replace(/^or$/i, "|");
		arr_qtext[i] = arr_qtext[i].replace(/^and$/i, "&");
		arr_qtext[i] = arr_qtext[i].replace(/^not$/i, "!");


		// 다시 하나의 string으로 연결한다.
		qtext += arr_qtext[i] + " ";
	}
	

	// 뒤에 따라나오는 !(not) |(or) &(and) 연산자 제거
	exp = /[!&|\s]+$/g;
	qtext = qtext.replace(exp, "");


	// 앞에 나오는 & | 연산자 제거
	exp = /^[&|][&!|\s]+/g;
	qtext = qtext.replace(exp, "");
	exp = /^[!][!&|]+[!&|\s]*/g;
	qtext = qtext.replace(exp, "");
	exp = /^[&|\s]+/g;
	qtext = qtext.replace(exp, "");

	// ! (not) 연산자 앞 뒤 공백 압축
	exp = /[\s]*[!][\s]*/g ;
	qtext = qtext.replace(exp, "!");

	// & (and) 연산자 앞 뒤 공백 압축
	exp = /[\s]*[&][\s]*/g ;
	qtext = qtext.replace(exp, "&");

	// | (or) 연산자 앞 뒤 공백 압축
	exp = /[\s]*[|][\s]*/g ;
	qtext = qtext.replace(exp, "|");

	//  잘못된 중복 연산자 and 로 치환  ( |! ( <or><not> )은 유효)
	exp = /([&!][&!|]+|[|][&|]+|[|][!][!|&]+|[\s]+)/g;
	qtext = qtext.replace(exp, "&");

	return qtext;
}

function cleanQueryBeauty(query) {
	var qtext = query;
	var exp;

	qtext = cleanQuery(qtext);

	// 예쁘게 보이기 하기 위해서 연산자 주위로 공백 추가
	exp = /[!]/g;
	qtext = qtext.replace(exp, " ! ");
	exp = /[&]/g;
	qtext = qtext.replace(exp, " & ");
	exp = /[|]/g;
	qtext = qtext.replace(exp, " | ");
	exp = /^[\s]+/g;
	qtext = qtext.replace(exp, "");


	return qtext;
}

/*
  ! : <not> 연산자
  ' ' : <and> 연산자
  | : <or> 연산자
  중복이 되거나 잘못된 연산자의 연속은 and로 치환
*/
function convertQuery(query) {
	var qtext = query;
	var exp;

	// 특수문자를 기준으로 split 하여 분리한 다음
	// 분리된 단어가 and or not 인지 판별하여 기호로 치환하여 나중에 한꺼번에 처리한다
	exp = /[.!/\-+\'\"~`?*^%$#@,=;:\\\]\[}{)(\s><_]/g ;

	var arr_qtext = qtext.split(exp);

	qtext = "";
	for (var i = 0; i < arr_qtext.length; i++) {
		arr_qtext[i] = arr_qtext[i].replace(/^or$/i, "|");
		arr_qtext[i] = arr_qtext[i].replace(/^and$/i, "&");
		arr_qtext[i] = arr_qtext[i].replace(/^not$/i, "!");


		// 다시 하나의 string으로 연결한다.
		qtext += arr_qtext[i] + " ";
	}
	

	// 뒤에 따라나오는 !(not) |(or) &(and) 연산자 제거
	exp = /[!&|\s]+$/g;
	qtext = qtext.replace(exp, "");


	// 앞에 나오는 & | 연산자 제거
	exp = /^[&|][&!|\s]+/g;
	qtext = qtext.replace(exp, "");
	exp = /^[!][!&|]+[!&|\s]*/g;
	qtext = qtext.replace(exp, "");
	exp = /^[&|\s]+/g;
	qtext = qtext.replace(exp, "");

	// ! (not) 연산자 앞 뒤 공백 압축
	exp = /[\s]*[!][\s]*/g ;
	qtext = qtext.replace(exp, "!");

	// & (and) 연산자 앞 뒤 공백 압축
	exp = /[\s]*[&][\s]*/g ;
	qtext = qtext.replace(exp, "&");

	// | (or) 연산자 앞 뒤 공백 압축
	exp = /[\s]*[|][\s]*/g ;
	qtext = qtext.replace(exp, "|");

	//  잘못된 중복 연산자 and 로 치환  ( |! ( <or><not> )은 유효)
	exp = /([&!][&!|]+|[|][&|]+|[|][!][!|&]+|[\s]+)/g;
	qtext = qtext.replace(exp, "&");


	// AND
	exp = /[&]/g;
	qtext = qtext.replace(exp, "<and>");
	
	// OR
	exp = /[|]/g;
	qtext = qtext.replace(exp, "<or>");
	
	// NOT
	exp = /[!]/g;
	qtext = qtext.replace(exp, "<not>");
	
	return qtext;
}
