#!/usr/bin/perl
#use strict;
use locale;
@snt_array;
#use encoding "utf-8";

open(FILE, "tagset.postag") || die;
binmode(FILE,":utf8");
while($line=<FILE>){
    my $line=~s/\n//;
    $postag{$line}++;
}
close FILE;
my @list;

    my $filename=$ARGV[0];
    
    $num=0;
    $ipname="generatedxmlfromconll.xml";
    
    
    open(newFILE, ">$ipname") || die;
    binmode(newFILE,":utf8");

    local *FILE;
    open(FILE, $filename) || die("File Could not be opened");
    binmode(FILE,":utf8");
    close newFILE;
    open(newFILE, ">>$ipname") || die;
    binmode(newFILE,":utf8");
    $line =<FILE>;
    	$ix=0;	
	if ($line=~ m/<\/[S]/){
	    $line=~ s/<\/[S]//;
	}
	$slen=takesentence();
	print newFILE "\<\?xml version=\"1.0\" encoding=\"UTF-8\" \?\>\n";
	print newFILE "\<Set sentences=\"1\"\>\n";
	print newFILE "\<S No=\"1\"\>\n";	
	#FOR REINDEXING
	$ind=1;	
	    for($i=1;$i<=$slen;$i++){
		$wig=1;
		$lineor="\<W IX\=\"";
		$lineor.=$ind;
		$lineor.="\" LEM\=\"\" MORPH\=\" \" IG\=\'\[\(1\,\"";
		$lineor.=$lemma[$i]."+".$postags[$i];
		if($dpostags[$i] ne $postags[$i]){
		    $lineor.="+".$dpostags[$i];
		}
		if($feats[$i] ne "_"){
		    $feats[$i]=~s/\|/\+/g;
		    $lineor.="+".$feats[$i];
		}
		while($lex[$i] eq "_"){		    
		    $i++;
		    $wig++;
		    $lineor.="\"\)\(".$wig."\,\"";
		    $lineor.=$postags[$i];
		    if($dpostags[$i] ne $postags[$i]){
			$lineor.="+".$dpostags[$i];
		    }
		    if($feats[$i] ne "_"){
			$feats[$i]=~s/\|/\+/g;
			$lineor.="+".$feats[$i];
		    }
		}
		
		$lineor.="\"\)\]\' REL\=\"\[";
		if($heads[$i]==0){
		    $lineor.=",( )]\"\> ".$lex[$i]." \<\/W\>";  #degismeli
		}
		else{
		  $rel=find_firstIG($heads[$i]);
		  $dif=$heads[$i]-$rel+1;
		  $lineor.=$wordindices[$heads[$i]].",".$dif.",(".$deprel[$i].")]\"\> ".$lex[$i]." \<\/W\>";  #degismeli
		}
		
		$ind++;
		print newFILE $lineor;
		print newFILE "\n";
	    }	    
	
	print newFILE "\<\/S\>\n\<\/\Set\>\n";	
    close newFILE;
    #print "fine";
    #$key = getc(STDIN);




sub converttags{
    if($columns[5]=~/^Inf1/){	
	$columns[5]=~s/Inf1/Inf/;
    }
    elsif($columns[5]=~/^Inf2/){	
	$columns[5]=~s/Inf2/Inf/;
    }
    elsif($columns[5]=~/^Inf3/){	
	$columns[5]=~s/Inf3/Inf/;
    }
    if($columns[5]=~/(FutPart)/ || $columns[5]=~/(PastPart)/ || $columns[5]=~/(PresPart)/  || $columns[5]=~/(Inf)/  ){
	$pt=$1;
	if($columns[4] eq "Adj"){
	    $columns[4]="A".$pt;
	}
	elsif($columns[4] eq "Noun"){
	    $columns[4]="N".$pt;
	}
	if($columns[5]=~/$pt\|/){
	    $columns[5]=~s/$pt\|//;
	
	}
	elsif($columns[5] eq $pt){
	    $columns[5]="_";
	}	
    }
    elsif($columns[3] eq "Pron" && $columns[4] eq "Ques"){
	$columns[4] = "QuesP";
    }
    elsif($columns[3] eq "Pron" && ($columns[5]=~/(Demons)\|/ || $columns[5]=~/(Pers)\|/ || $columns[5]=~/(Reflex)\|/) ){
	$pt=$1;
	$columns[5]=~s/$pt\|//;
	$columns[4] = $pt."P";
    }
    if($columns[4] eq "Pron" && $columns[5]=~/Quant\|/){
	$columns[5]=~s/Quant\|//;
    }
}

sub takesentence{
    undef @connected;
    $ind=0;undef @postags,undef @feats,undef @heads,undef @deprel,undef @igwordset,undef @set;undef @snt;
    #takes the sentence into the data structure
    $wordind=0;
    while ($line!~/^[\s\t]*\n$/){    	
	$ind++;
	$wordind++;
	$wordindices[$ind]=$wordind; 
	@columns=split(/\t/,$line);
	$snt[$ind]=$line;
	$lex[$ind]=$columns[1];$lemma[$ind]=$columns[2];$postags[$ind]=$columns[3];$dpostags[$ind]=$columns[4];$feats[$ind]=$columns[5];
	$heads[$ind]=$columns[6];$deprel[$ind]=$columns[7];
	$firstig=$ind;
	$igwordset[$firstig]{$ind}++;
	while($lex[$ind] eq "_"){
            $connected[$ind]=$ind+1;
	    $line=<FILE>;
	    $ind++;	    
	    push @set,$ind;
	    @columns=split(/\t/,$line);
	    $snt[$ind]=$line;
	    $lex[$ind]=$columns[1];$lemma[$ind]=$columns[2];
	    $postags[$ind]=$columns[3];$dpostags[$ind]=$columns[4];$feats[$ind]=$columns[5];
	    $heads[$ind]=$columns[6];$deprel[$ind]=$columns[7];
	    $igwordset[$firstig]{$ind}++;
	}
	
	$k=@set;
	while ($k!=0){
	    $k--;
	    $inig=pop @set;
	    $igwordset[$inig]=$igwordset[$firstig];
	    $wordindices[$inig]=$wordind;
	}	
	
	$line=<FILE>;
    }
    return $ind;
}
sub find_firstIG{
    my $position=shift;
    my $fposition=$position;
    while(exists($igwordset[$position]{$fposition})){
        $fposition--;
    }
    $fposition++;
    return $fposition;
}