package ITUannotation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;




public class Parser 
{
	
	private String TypetoParse;
	private String input;

	public Parser(String inputstr, String WhatamIParsing)
	{
		// TODO Auto-generated constructor stub
		input=inputstr;
		TypetoParse=WhatamIParsing;
	}
	public ArrayList <PossibleMorphAnalysOfWords> getPossibleMorphofWords(String input)
	{
	
		ArrayList<String> wordsofsent = new ArrayList<String>();
		ArrayList<PossibleMorphAnalysOfWords> Words = new ArrayList<PossibleMorphAnalysOfWords>();
		ArrayList<WordElements> Wordelementlist;
		//input="\n"+input;
		if(input.isEmpty())
		{
			return Words;

		}
		else
		{
			input=input.substring(1);
			PossibleMorphAnalysOfWords morph;//= new getPossibleMorphofWords()
			String[]result=input.split("\n\n");
			int wordnum=result.length;
			int numof_possibleexplanations=0;
			for(int i=0;i<wordnum;i++)
			{
				Wordelementlist = new ArrayList<WordElements>();
				String temp=result[i];
				
				int wordend=temp.indexOf("\t");
				String word=temp.substring(0, wordend);
				wordsofsent.add(word);
				//added for unanalyzed punctuations
				String punctuations=".,!?()\"\'";
				if(punctuations.contains(word)){
					if(temp.contains("+?")){//Morph analyzer didn't produce any result.
						temp=word+"\t"+word+"\t+Punc\n";
					}
				}
				//yeni
				String ExplArray[]=temp.split("\n");
				for(int k=0;k<ExplArray.length;k++)
				{
					wordend=temp.indexOf("\t");
					 String exp=ExplArray[k].substring(wordend);//herbir acıklamayı alıyor kok+exp var içinde
					 exp=exp+"\n";
					 if(!exp.startsWith("\t"))
						 exp="\t"+exp;
					 Pattern kalip4=Pattern.compile("\t.+?\n");
					 Matcher esl4=kalip4.matcher(exp);
					 String kok,acklama;
					 kok=acklama="";
						
						if (esl4.find())
						{
							String expl=esl4.group();
							expl=expl.substring(1);
							String[]rootandexp=expl.split("\t");
							if(rootandexp.length<2)
							{
								System.out.println(expl);
								int plus=expl.indexOf("+");
								kok=expl.substring(0,plus);
								acklama=expl.substring(plus);
							}
							else{
								kok=rootandexp[0];
								acklama=rootandexp[1];
			/*					acklama=acklama.replaceAll("\n", "");
								acklama=format_exp(acklama,kok);
								numof_possibleexplanations++;
								
		//						System.out.println(expl);
		//						System.out.println(kok);
		//						System.out.println(acklama);
								WordElements we= new WordElements(kok, acklama, numof_possibleexplanations);
								Wordelementlist.add(we);*/
							}
							acklama=acklama.replaceAll("\n", "");
							acklama=format_exp(acklama,kok);
							numof_possibleexplanations++;
							
	//						System.out.println(expl);
	//						System.out.println(kok);
	//						System.out.println(acklama);
							WordElements we= new WordElements(kok, acklama, numof_possibleexplanations);
							Wordelementlist.add(we);
						}
				}				
				//yeni
				
				
				int numofpossibleexp_ofword=numof_possibleexplanations;
				PossibleMorphAnalysOfWords wordread= new PossibleMorphAnalysOfWords(numofpossibleexp_ofword,Wordelementlist, word);
			
				numof_possibleexplanations=0;
				Words.add(wordread);
				
				
			}
			
			int num=Words.size();

			return Words;
		}
	}
	private String format_exp(String acklama, String kok) {
		
		//String FormattedAcklama;
		
		if(acklama.contains("^DB"))
		{ 
			String OK="";
			String[] IGarr;
			String delimiter="\\^DB";
			IGarr=acklama.split(delimiter);
			
			//String temp="",IGparts;
			for(int i=0;i<IGarr.length;i++)
			{
				String temp,IGparts;
				IGparts=IGarr[i];
				IGparts.substring(1);
				if(i==0)
				{
					temp="(" +Integer.toString(i+1)+ ",\"" +kok+IGparts+"\")";
					OK=OK+temp;
				}
				else
				{
					IGparts=IGparts.substring(1);
					temp="("+Integer.toString(i+1)+",\""+IGparts+"\")";
					OK=OK+temp;
				}
				
			}
			
			int i=3;
			return OK;
		}
		else
		{
			String tmp="(1,\""+kok+acklama+"\")";
			return tmp;}
		
		// TODO Auto-generated method stub
		
	}
}




